93
1 C Programming Notes www.enosislearning.com C Contents Introduction:........................................................ 9 C Programming Data Types:............................................ 9 Structure of c program:............................................. 10 C Programming Input Output (I/O):...................................11 Example 1: C Output................................................11 Example 2: C Integer Output........................................12 Example 3: C Integer Input/Output..................................12 Little bit on ASCII code:........................................12 Example 5: C ASCII Code............................................12 Example 6: C ASCII Code............................................13 C Programming Operators:............................................ 13 Type of Operator...................................................13 Symbolic Representation............................................13 Arithmetic Operators...............................................13 +, -, *, /, %......................................................13 Increment and decrement operators..................................13 ++,--..............................................................13 Assignment Operators...............................................13 =..................................................................13 Relational Operators...............................................14 ==, > , <, >=, <=, !=..............................................14 Logical Operators................................................14 &&, ||, !.........................................................14 Bitwise operator.................................................14 &, !...............................................................14 Comma Operator...................................................14 ,..................................................................14 Enosis Learning C Notes Page 1

enosislearning.comenosislearning.com/Tutorials/C/CProgramming_FinalNotes…  · Web viewFor example: You want to store the information about person about his/her name, citizenship

  • Upload
    lequynh

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

C Programming Notes

(C Programming Notes) (28) www.enosislearning.com

C ContentsIntroduction:9C Programming Data Types:9Structure of c program:10C Programming Input Output (I/O):11Example 1: C Output11Example 2: C Integer Output12Example 3: C Integer Input/Output12Little bit on ASCII code:12Example 5: C ASCII Code12Example 6: C ASCII Code13C Programming Operators:13Type of Operator13Symbolic Representation13Arithmetic Operators13+, -, *, /, %13Increment and decrement operators13++,--13Assignment Operators13=13Relational Operators14==, > , =, , =, or

p#include stdio.h/ppint main()/pp{/pp int number1, number2;/pp printf("Enter two integers: ");/pp scanf("%d %d", &number1, &number2);/pp/p //checks if two integers are equal./pp if(number1 == number2)/pp {/pp printf("Result: %d = %d",number1,number2);/pp }/pp/p //checks if number1 is greater than number2./pp else if (number1 > number2)

{

printf("Result: %d > %d", number1, number2);

}

// if both test expression is false

else

{

printf("Result: %d < %d",number1, number2);

}

return 0;

}

Output

Enter two integers: 12

23

Result: 12 < 23

C Programming for Loop

Loops are used in programming to repeat a specific block of code.

Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming:

1. for loop

2. while loop

3. do...while loop

for Loop:

The syntax of a for loop is:

for (initializationStatement; testExpression; updateStatement)

{

// codes

}

for loop Flowchart

Example: for loop

#include

int main ()

{

int a;

/* for loop execution */

for( a = 10; a < 20; a = a + 1 )

{

printf("value of a: %d\n", a);

}

return 0;

}

Output:-

value of a: 10value of a: 11value of a: 12value of a: 13value of a: 14value of a: 15value of a: 16value of a: 17value of a: 18value of a: 19

while loop: Syntax

while (testExpression)

{ //codes

}

Example 1: while loop

#include

int main () {

/* local variable definition */

int a = 10;

/* while loop execution */

while( a < 20 ) {

printf("value of a: %d\n", a);

a++;

}

return 0;

}

Output

value of a: 10value of a: 11value of a: 12value of a: 13value of a: 14value of a: 15value of a: 16value of a: 17value of a: 18value of a: 19

do...while loop:

Thedo..whileloop is similar to thewhileloop with one important difference. The body of do...whileloop is executed once, before checking the test expression. Hence, thedo...while loopis executed at least once.

do...while loop Syntaxdo{ // codes}while (testExpression);

How do...while loop works?

The code block (loop body) inside the braces is executed once.

Then, the test expression is evaluated. If the test expression is true, the loop body is executed again. This process goes on until the test expression is evaluated to 0 (false).

When the test expression is false (nonzero), thedo...whileloop is terminated.

Example #2: do...while loop

#include

int main ()

{

/* local variable definition */

int a = 10;

/* do loop execution */

do {

printf("value of a: %d\n", a);

a = a + 1;

}while( a < 20 );

return 0;

}

Output: value of a: 10value of a: 11value of a: 12value of a: 13value of a: 14value of a: 15value of a: 16value of a: 17value of a: 18value of a:19

C Programming break and continue Statement

It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. In such cases,breakandcontinue statements are used.

break Statement

The break statement terminates the loop immediately when it is encountered. The break statement is used with decision making statement such asif...else.

Syntax of break statement

Break;

How break statement works?

Example : break statement

#include

int main()

{

int i;

for(i=0;i