Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Sajib Datta Jan 21, 2014.  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =

Similar presentations


Presentation on theme: "Dr. Sajib Datta Jan 21, 2014.  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height ="— Presentation transcript:

1 Dr. Sajib Datta CSE@UTA Jan 21, 2014

2  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height = 5;  Define - Declare & Initialize ◦ int height = 5; ◦ char name = ‘a’; ◦ double marks= 90.3;

3  Print a variable ◦ printf(“%d”, num); - format specifier, variable ◦ Number matching - the number of specifiers is equal to the number of variables  Type matching ◦ %d for int ◦ %lf for double ◦ %c for char  printf can be used just to print text ◦ printf(“this is just a text”);

4  A C program consists of statements  Some types of statements: ◦ declaration statements ◦ Assignment statements ◦ Function calls ◦ Control statements  Each statement is terminated by a semicolon.  Control statement can change the program flow.

5  Declaration statements are when we declare variables for use. void main() { int a; …… }

6  Have a left side and a right side.  The right side: ◦ a single value, a complicated expression, or a function call ◦ ultimately reduce to a single value, which is then assigned to the variable named on the left side.  Example: ◦ int num; [declaration] ◦ num = 1; ◦ num = num + 10; ◦ num = num + 2; [what is the final value of num?]

7  The basic operators that you have in math are also available in C: ◦ +, ◦ -, ◦ *, ◦ /, ◦ =  WARNING: Difference between operators in C and their math use is integer division.  The fraction resulting is truncated in integer division ◦ integer = integer / integer  Example: ◦ int a = 7, b = 5; ◦ int answer; ◦ answer = a / b;

8 float a = 7.1, b = 5.2; int answer; answer = a / b;

9  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer remainder from integer division.  Example: ◦ int a = 7, b = 3; ◦ int answer; ◦ answer = a % b;

10 The following assignment operators are available in C:  += addition  -= subtraction  *= multiplication  /= division  num = num + 2 same as num+=2;  num = num - 1 same as num-=1;

11  What is the output of ‘x’? ◦ int x; ◦ x = 10; ◦ x += x; ◦ printf(“%d”, x);

12  We discussed: ◦ Omega ◦ Variable  Declaration, assigning a value, initialization ◦ Statements  Conditional statements can change the flow of the code ◦ Operators  +,-,*,% ◦ Data type  What happens when you assign a float/double to a variable of type int

13  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction  For operators with the same precedence, if they share an operand, they are executed according to the order they occur in the statement. (In most cases, from the left to the right, except assignment operation)  Examples: ◦ int num, x; ◦ num = 2; ◦ x = 1 + 3*num/4; What is the value of x?

14  Want an addition operation to take place before division? ◦ int num, x; ◦ num = 2; ◦ x = (1 + 3)*num/4;  What is x now?  int a = 1, b = 4;  b += a+2; is equivalent to b = b + (a + 2)

15  < : is less than  <= : is less than or equal to  == : is equal to  >= : is greater than or equal to  > : is greater than  != : is not equal to  Example: ◦ Relational expression: a > 10  If the relation is true, the value of the expression is 1, otherwise 0.

16  A combination of operators and operands, where operands can be constants, variables, or combinations of the two  4  4+21  a = (b+c)/2  q>4

17  A statement is a complete instruction to the computer  In C, it is indicated by semicolon  An statement is consists of expressions


Download ppt "Dr. Sajib Datta Jan 21, 2014.  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height ="

Similar presentations


Ads by Google