c programming n general information on c n data types n arithmetic operators n relational operators...

21
C Programming General Information on C Data Types Arithmetic Operators Relational Operators if, if-else, for, while by Kulapan Waranyuwat

Upload: emil-lang

Post on 01-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

C Programming

General Information on C Data Types Arithmetic Operators Relational Operators if, if-else, for, while

by Kulapan Waranyuwat

Page 2: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

General Info on C

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.

Page 3: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Data Types

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

Page 4: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Integer

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.

Page 5: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Float

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.

Page 6: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Double

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 */

Page 7: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Char

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!

Page 8: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Operators 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

Page 9: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Keyboard Input

Use scanf() function. See Example 3.

Page 10: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Relational Operators

== 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”

Page 11: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

for loop

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.

Page 12: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

while The general format is:

while( condition ) program statement;

See Example 6.

Page 13: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

if, if-else The general format of if:

if( expression ) program statement;

The general format of if-else:

if( condition 1 ) statement1;else if( condition 2 ) statement2;else if( condition 3 ) statement3;else statement4;

See Example 7.

Page 14: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Compound Relationals 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.

Page 15: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Arrays 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.

Page 16: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Array syntax int employees[3];

employees[0] = 101; employees[1] = 232; employees[2] = 0;

To access an element in the array, use square brackets. name_of_array[i]so that name_of_array[5] refers to the sixth element in an array called name_of_array. In C, array elements start with 0.

Assigning values to an array is done by: x[10] = g; /* x = name of array, 10=index, g=value. */

and assigning array elements to a variable is done by g = x[10]; /* g=variable name, x=array name, 10=index. */

Page 17: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Arrays (cont.) See Example 10.

Page 18: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

Multi-dimensional arrays Multi-dimensioned arrays have two or more index values which specify

the element in the array.multi[i][j]

In the above example, the first index value i specifies a row index, whilst j specifies a column index.

Example:

int m1[10][10]; static int m2[2][2] = { {0,1}, {2,3} };

sum = m1[i][j] + m2[k][l];

See Example 11 for exercises.

Page 19: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

(cont.) { 0, 1 },

{ 2, 3 }

Remember that arrays are split up into row and columns. The first is the row, the second is the column. Looking at the values assigned to m2:

m2[0][0] = 0m2[0][1] = 1m2[1][0] = 2m2[1][1] = 3

Page 20: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

An aside on printf() %c single character

%d signed decimal integer%f floating-point, decimal notation%s character string%p a pointer%% print a percent sign

The format for using printf() is:

printf (Control-string, item1, item2, …);

See previous examples for more on printf().

Page 21: C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat

THE END