C Programming. General Information on C Data Types Arithmetic Operators Relational Operators if, if-else, for, while by Kulapan Waranyuwat. General Info on C.
An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
C is a much more powerful language than AWK. In fact, most of the programs for Unix and the Unix system itself is written in C. C is used to write large software systems, network programming, and it is very fast and efficient. However, it’s probably a more difficult language to learn and master and it takes more time to write C programs than AWK for example due to its complexity.
C is a bit more picky about its data than AWK is. You’ll see what I mean soon.
There are 4 data types in C:integer - intfloating point - floatdouble - doublecharacter - char
These are whole numbers, both positive and negative.
int sum; /* declares an integer variable called sum. */int num = 5; /* declares an integer variable called num and sets it equal to 5.
These are numbers which contain fractional parts, both positive and negative.
float amount; /* declares a floating point called amount */float money = 5.15/* declares a floating point value of 5.15 to variable called money.
These are exponential numbers, both positive and negative.
double bignum;/* declars a double called bignum */double bignum = 31E+5; /* declares bignum as a double and initializes it to 31E+5 */
These are single characters.
char word;/* declares a char called word */char letter = ‘B’;/* declares a char variable called letter and assigns B to it. */
Note: Use single quotes for single character!
operation: operator: expression: value before: value after.
Multiply : * : sum = sum * 2; : 4 : 8
Divide : / : sum = sum / 2; : 4 : 2
Addition : + : sum = sum + 2; : 4 : 6
Subtraction : - : sum = sum -2; : 4 : 2
Increment : ++ : ++sum; : 4 : 5
Decrement : -- : --sum; : 4 : 3
Modulus : % : sum = sum % 3; : 4 : 1
Use scanf() function. See Example 3.
== meaning “equal to”!= meaning “not equal to”< meaning “less than”<= meaning “less than or equal to”> meaning “greater than”>= meaning “greater than or equal to”
The logic of the C for-loop is the same as in AWK. Let’s look at some examples… The general format is:for( start condition; continue condition; re-evaulation ) program statement;
See Example 4.
Look at Example 5 for some exercises.
The general format is:while( condition ) program statement;
See Example 6.
The general format of if: if( expression ) program statement;
LOGICAL AND &&Logical AND requires all conditions to evaluate as TRUE.
LOGICAL OR ||Logical OR will be executed if any ONE of the conditions is TRUE.
LOGICAL NOT !Logical NOT negates a condition.
LOGICAL EOR ^Logical EOR will be executed if either condition is TRUE, but NOT if they are all true.
See Example 8.
Example 9 for more exercises.
An array is a type of data structure that is used to hold multiple variables of the same data type, as mentioned last week in the AWK lecture. It is the same in C.
A simple application:Suppose a company wants to keep track of all its employees by assigning the names of each respective employee with an identification number. If one were to do the following, it would be a very tedious task.int employee1 = id1int employee2 = id2and so on….
Arrays are a good solution to this problem. Arrays will create a “file cabinet” and everything in this cabinet will be of the same type. This makes storing data with arrays, in this specific problem, very practical. So you have a file cabinet with only integer variables all holding some integer value.