Eee0115 1
Eee0115 1
Eee0115 1
Chapter 2: Introduction to C
Programming
C How to Program
1 Deitel & Deitel
2
Yükleniyor…
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } /* end function main */
Welcome to C!
4
Yükleniyor…
6
Welcome to C!
Welcome
to
C!
9
Yükleniyor…
• = assignment operator is used to assign a value to a
variable.
• printf("Sum is %d\n", sum ); is used to print a string of
characters and a value of a variable.
• %d shows that a decimal integer will be printed.
• sum is the variable name to be printed on the screen.
• Calculations can be performed inside printf statements.
12
Memory Concepts
• The variable names actually correspond to locations in
the computer’s memory.
• Each variable has a name, type, size and a value.
• Reading variables does not modify their values.
• When you place a new value to a variable, it overwrites
the old value.
13
Arithmetic
• +, - addition and subtraction
• *, / multiplication and division
• İnteger division produce integer result.
• % operator finds the remainder
• 9%4 returns 1
• Some operators have precedence over other operators
• Multiplication and division have higher precedence than
addition and subtraction
• You may use parenthesis.
14
Arithmetic
Arithmetic Algebraic
C opetration C expression
operator expression
Addition + f+7 f + 7
Subtraction – p–c p - c
Multiplication * bm b * m
Division / x
x y or xor ÷ xy / y
y
Remainder % r mod s r % s
Decision Making
• If the condition given in a if control statement is true,
the body of if statement is executed.
• If the condition given in a if control statement is false,
the body of if statement is not executed.
• 0 is false, non-zero values is true.
16
Decision Making
Equality operators
= == x == y x is equal to y
≠ != x != y x is not equal to y
Relational operators
Decision Making
1 /* Fig. 2.13: fig02_13.c
2 Using if statements, relational
3 operators, and equality operators */
4 #include <stdio.h>
5
6 /* function main begins program execution */
7 int main( void )
8 {
9 int num1; /* first number to be read from user */
10 int num2; /* second number to be read from user */
11
12 printf( "Enter two integers, and I will tell you\n" );
13 printf( "the relationships they satisfy: " );
14
15 scanf( "%d%d", &num1, &num2 ); /* read two integers */
16
17 if ( num1 == num2 ) {
18 printf( "%d is equal to %d\n", num1, num2 );
19 } /* end if */
20
21 if ( num1 != num2 ) {
22 printf( "%d is not equal to %d\n", num1, num2 );
23 } /* end if */
24
25 if ( num1 < num2 ) {
26 printf( "%d is less than %d\n", num1, num2 );
27 } /* end if */
28
18
Decision Making
29 if ( num1 > num2 ) {
30 printf( "%d is greater than %d\n", num1, num2 );
31 } /* end if */
32
33 if ( num1 <= num2 ) {
34 printf( "%d is less than or equal to %d\n", num1, num2 );
35 } /* end if */
36
37 if ( num1 >= num2 ) {
38 printf( "%d is greater than or equal to %d\n", num1, num2 );
39 } /* end if */
40
41 return 0; /* indicate that program ended successfully */
42
43 } /* end function main */
43 } /* end function main */
Decision Making
(continued from previous slide…)
Operators Associativitiy
Operators Associativity
() left to right
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
= right to left