INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C

547 views 33 slides May 10, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C


Slide Content

INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C C. P. Divate

DATA INPUT AND OUTPUT As we know that any c program is made up of 1 or more then 1 function. Likewise it use some functions for input output process. The most common function printf() scanf().

printf() Function printf() function is use to display something on the console or to display the value of some variable on the console. The general syntax for printf() function is as follows printf(<”format string”>,<list of variables>); To print some message on the screen printf(“God is great”); This will print message “God is great” on the screen or console.

printf() Function • To print the value of some variable on the screen Integer Variable : int a=10; print f (“ % d ” ,a); Here %d is format string to print some integer value and a is the integer variable whose value will be printed by printf() function. This will print value of a “10” on the screen.

printf() function To print multiple variable’s value one can use printf() function in following way. int p=1000,n=5; float r=10.5; printf(“amount=%d rate=%f year=%d”,p,r,n); This will print “amount=1000 rate=10.5 year=5” on the screen

scanf() Function sc a n f ( ) f unctio n is u s e t o re a d d a t a fr o m keyboard and to store that data in the variables. Th e ge ne ral synta x f o r sc a n f ( ) f uncti o n i s as follows. scanf(“Format String”,&variable); Here format string is used to define which type of data it is taking as input. th i s f or mat s t r in g c an b e % c f o r ch ar a c te r, %d for integer variable and %f for float variable.

scanf() Function scanf(“Format String”,&variable); Where as variable the name of memory location or name of the variable and & sign is an operator that tells the compiler the address of the variable where we want to store the value.

scanf() Function For Integer Variable : int rollno; printf(“Enter rollno=”); scanf(“%d”,&rollno); Here in scanf() function %d is a format string for integer variable and &rollno will give the address of variable rollno to store the value at variable rollno location.

scanf() Function For Float Variable : float per; printf(“Enter Percentage=”); scanf(“%f”,&per); For Character Variable : char ans; printf(“Enter answer=”); scanf(“%c”,&ans);

Single character input – the getchar function : Single characters can be entered into the computer using the “C” library function getchar. In general terms, a reference to the getchar function is written as. character variable=getchar(); For example char c; c = ge t c h ar ();

Single character output – The putchar function Single character can be displayed (i.e. written out of the computer) using the C library function putchar. In general a reference to the putchar function is written as putchar (character variable); For Example char c=’a’; pu t c h a r ( c);

Control Flow In C Objectives of the module is Ho w t o d i r e c t t h e s e qu e nc e o f e x e cut i on using Decision control Structure H ave an und e r st a n d i n g o f th e ite ra t ive process using Loop Control Structure

Decision Control Structure The if-else statement: The if-else statement is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test Thus , i n i t s s i m p l es t ge n e r al for m, th e s t a te me n t can be written. if(expression) { statement; }

• The general form of an if statement which include the else clause is if(expression) { statement 1; } else { statement 2; } If the expression is true then statement 1 will be executed. Otherwise, statement 2 will be executed. Decision Control Structure

Nested If Else if<exp1> { statement1; } else { if<exp2> { statement2; } }

Nested If Else

/* Demonstration of if statement */ #include<stdio.h> void main( ) { int num ; printf ( "Enter a number :" ) ; scanf ( "%d", &num ) ; if ( num <= 10 ) printf ( “Number is less than 10" ) ; else printf(“Number is greater than 10”); }

Else if ladder if( expression1 ) statement1; else if( expression2 ) statement2; else statement3; For Example if( age < 18 ) p r int f ("Min o r "); else if( age < 65 ) printf("Adult"); else printf( "Senior Citizen"); •

Decision Control Structure The switch statement: cause s a par t ic u lar g ro u p of s t at e m e n ts to be chosen from several available groups. The selection is based upon the current value of an expression that is included within a switch statement.

The general form of switch-case switch(expression) { case expression1: s t a t e m en ts; break; case expression2: s t a t e m en ts; break; case expression3: s t a t e m en ts; break; }

• When switch statement is executed the expression is evaluated and control is transferred directly to the group of statements whose case labels value matches the value of the expression. switch(choice) { case ‘r’ : p r i n t f ( “R E D ” ) ; break; case ‘b’ : printf(“BLUE”); break; default : p r i n t f ( “ E RR O R ” ); break; }

LOOP CONTROL STRUCTURE If we want to perform certain action for no of times or we want to execute same statement or a group of statement repeatedly then we can use different type of loop structure available in C. Basically there are 3 types of loop structure available in C While loop Do..while For loop

While Loop T h e w hil e st a t e m e n t i s u s ed t o c a r r y out looping operations. The general form of the statements i n i t i a li z a t i o n; while(exp) { statement 1; statement 2; increment/ decrement; }

While loop example # in c lud e <s t di o . h > void main () { int digit = 0; whil e ( d i g i t < = 9) { printf(“%d \n”,digit); ++digit ; } }

Do-While Loop Sometimes, however, it is desirable to have a loop with the test for continuation at the end or each pass. This can be accomplished by means of the do-while statement. The general form of do-while statement is do { s tatement 1 ; s tatement 2 ; increment/decrement operator; } while(expression);

Do-While Loop Example #include <stdio.h> void main() { int digit = 0; do { printf(“%d”, digit++); }while(digit<=9); }

For Loop The for statement is another entry controller that provides a more concise loop control structure. The general form of the for loop is : for(initialization; test condition; inc/decrement) { statement 1; statement 2; }

For loop example #i n c l u d e < s t di o . h> void main() { for(x=0; x<9; x++) { printf(“%d”, x); printf(“\n”); } }

Reverse For loop The for statement allows for negative increments. For example, the loop discussed above can be written as follows: for(x=9; x>=0; x--) { printf(“%d”,x); printf(“/n”); }

BREAK STATEMENT The break statement is used to terminate loops or to exit a switch. for(i=1; i<=10; i++) { if(i==5) break; p r i n t f ( “ \n I =% d ” , i); }

CONTINUE STATEMENT T h e co n t i nu e s ta t e m e nt i s u s e d to sk i p o r to b y p a s s some step or iteration of looping structure. for(i=1; i<=10; i++) { if(i<5) continue; p r i ntf (“ \ n i = % d ” , i ); } The output of the above program will be 6,7,8,9,10.

THE GOTO STATEMENT The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of the program. In its general form the goto statement is written as goto label; Where label is an identifier used to label the target statement to which control will be transferred.

goto example void main() { for(i=0;i<5;i++) { printf(“i =%d”,i); if(i==4) goto stop; } stop: printf_s( "Jumped to stop”); }
Tags