
Managing Input and Output Operations in C Programming
Learn how to manage input and output operations in C programming through formatted and unformatted input and output functions such as scanf, printf, getchar, and putchar. Explore examples and understand the syntax for handling data effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
C Programming for Problem Solving Subject Code : 18CPS23 By Mrs. R S Geethanjali, Asst. Professor Dept. of CSE, KSSEM, Bengaluru Department of Computer Science and Engineering K S School of Engineering and Management, Bengaluru-560109
Module-2 MANAGING INPUT AND OUTPUT OPERATIONS
FORMATTED INPUT Formatted input refers to an input data that has been arranged in particular format. Syntax: scanf( controlstring , arg1, arg2, ..,argn); The control string specifies the field form in which data should be entered and the arguments arg1, arg2, ..,argn specifies the address of the location where the data is stored.
The scanf() function can read single character or more than one character using %c or %s. Control string/format specifiers %c (char) Read a single character %d (int) Read a decimal integer %f (float) Read a floating point value %s (string) Read a string scanf()- It s in stdio.h file
Example /*Addition of 2 numbers*/ #include<stdio.h> void main() { int a,b,c; //declaring varaibles printf( Enter the values of a and b ); scanf( %d%d ,&a,&b); // reading a,b values 10 & 20 c=a+b; //addition of a and b printf( %d ,c); // 30 printf( The addition of a and b is %d ,c); // The addition of a and b is 30 printf( The addition of %d and %d is %d , a,b,c); // The addition of 10 and 20 is 30 }
FORMATTED OUTPUT The printf() function is used for printing the captions and results. Syntax: printf( controlstring , arg1, arg2, .., argn); Control string contains three types of items: 1. Characters that will be printed on the screen as they appear. 2. Format specifications that define the output format for display of each item. 3. Escape sequence characters such as \n, \t and \b.
%c %d %s Print a single character Print a decimal integer Print a string
Formatted Input: scanf() Formatted output: printf() Unformatted input: getchar() Unformatted output: putchar()
READING A CHARACTER The simplest way of all input/output operations is reading a character from the standardinput unit (usually keyboard) and writing it to the standardoutput (usually the screen). Reading a single character can be done by using the function getchar() and it does not have any parameter.
Unformatted Input & output Unformatted input getch()- getche()- character i/p getchar()- gets()-string i/p unformatted output putch() putchar() puts()
Syntax: variable_name = getchar( ); Example: char name; name = getchar( ); The getchar() function can also be used to read the characters successively until the return key is pressed.
WRITING A CHARACTER Writing a single character can be done by using the function putchar. Syntax: putchar(variable_name); Example: name= y ; putchar(name);
Decision Making and Branching C language possesses such decision making capabilities by supporting the following statements: 1. If statement 2. Switch statement 3. Conditional operator statement 4. goto statement
DECISION MAKING (Two-Way Selection Statements ) The decision is described to the computer as conditional statement that can be answered TRUE or FALSE. If the answer is TRUE, one or more action statements are executed. If answer is FALSE, the different action or set of actions are executed.
C language provides following two- way selection 1. if statement 2. if else statement 3. Nested if else statement 4. Cascaded if else (also called else-if ladder)
if statement: The general form of simple if statements is shown below. Syntax: if (Expression) { Statement1; //body of if } Statement2;
if..else statement: The if..else statement is an extension of simple if statement. if (Expression) { Statement1; ->true-block or if block } else { Statement2; ->false-block or else block } Statement3;
Example If(a<b) { } else { } printf( A is greater ); printf( B is greater );
Nested if else statement When a series of decisions are involved, we have to use more than one if..else statement in nested form as shown below in the general syntax.
syntax if (boolean_expression_1) { if(nested_expression_1) { nested_expression_1 both are true } else { // If boolean_expression_1 is true but nested_expression_1 is false } } else { if(nested_expression_2) { // If boolean_expression_1 is false but nested_expression_2 is true } else { // If both boolean_expression_1 and nested_expression_2 is false } } // If boolean_expression_1 and
Largest of 3 numbers If(a>b) { if(a>c) { } } else { } printf( a is greater ); else { } printf( c is greater ); if(b>c) { } else { } printf( b is greater ); printf( c is greater );
else-if ladder or cascaded if else if (Expression1) { Statement1; } else if(Expression2) { Statement2; } else if(Expression3) { Statement3; } . . . . else if(Expression n) { Statement n; } else { Statement4; } Next Statement;
Largest of 3 numbers If((a>b)&&(a>c)) { printf( a is greater ); } else if((b>a)&&(b>c)) { printf( b is greater ); } else { printf( c is greater ); }
SWITCH STATEMENT C language provides a multi-way decision statement so that complex else-if statements can be easily replaced by it. C language s multi-way decision statement is called switch.
Syntax switch(choice) { } next statement; case label1: block1; case label2: block2; case label3: block-3; . . . . default: default-block; break; break; break; break;
TERNARY OPERATOR OR CONDITIONAL OPERATOR (?:) Expression1 ? Expression2 : Expression3 Where, Expression1 is Condition Expression2 is Statement Followed if Condition is True Expression3 is Statement Followed if Condition is False
GOTO STATEMENT goto is an unconditional branching statement Syntax goto label; statement1; statement2; . . . Statementn; label: Statements;
Example void main( ) { int a=5, b=7; goto end; a=a+1; b=b+1; end: printf( a=%d b=%d , a,b); }
Decision Making and Looping Definition of Loop: It is a programming structure used to repeatedly carry out a particular instruction/statement condition is true. Each single repetition of the loop is known as an iteration of the loop. until a
Cont Three important components of any loop are: 1. Initialization (example: ctr=1, i=0 etc) 2. Test Condition (example: ctr<=500, i != 0 etc) 3. Updating loop control values (example: ctr=ctr+1, i =i-1)
LOOPS IN C C language provides 3 looping structures namely: 1. while loop 2. do .while loop 3. for loop
while loop It is a pre-test loop (also known as entry controlled loop). while (condition) { statement-block; }
Example #include<stdio.h> void main() { int i=1, sum=0; while (i<=5) { } printf( %d , sum); } sum=sum+i; i=i++;
do. while loop: It is a post-test loop (also called exit controlled loop) it has two keywords do and while. Syntax do { statement-block; } while (condition);
Example #include<stdio.h> void main() { int i=1, sum=0; do { } while (i<=5); printf( %d , sum); } sum=sum+i; i=i++;
for loop It is a pre test loop and also known as entry controlled loop. for keyword is used here. Here, the head of the for loop contains all the three components that is initialization, condition and Updation.
Syntax for( initialization; test-condition; updation) { Statements; }
Example #include<stdio.h> void main( ) { int i, sum=0; for (i=1; i<=10; i++) { sum=sum+i; } printf( %d , sum); }
Nested for loops A for loop inside a for loop is called Nested for loop. for(i=0; i<2; i++) { for(j=0; j<2; j++) { scanf( %d , &a[i][j]) } }