SlideShare a Scribd company logo
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
1) printf()
2) 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;
printf(“%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
• scanf() function is use to read data from
keyboard and to store that data in the variables.
• The general syntax for scanf() function is as
follows.
scanf(“Format String”,&variable);
Here format string is used to define which type
of data it is taking as input.
this format string can be %c for character, %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=getchar();
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’;
putchar(c);
Control Flow In C
• Objectives of the module is
1)How to direct the sequence of execution
using Decision control Structure
2)Have an understanding of the iterative
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, in its simplest general form, the statement
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 )
printf("Minor");
else if( age < 65 )
printf("Adult");
else
printf( "Senior Citizen");
•
Decision Control Structure
• The switch statement:
causes a particular group of statements 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:
statements;
break;
case expression2:
statements;
break;
case expression3:
statements;
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’ :
printf(“RED”);
break;
case ‘b’ :
printf(“BLUE”);
break;
default :
printf(“ERROR”);
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
(1) While loop
(2) Do..while
(3) For loop
While Loop
• The while statement is used to carry out
looping operations.
• The general form of the statements
initialization;
while(exp)
{
statement 1;
statement 2;
increment/ decrement;
}
While loop example
#include<stdio.h>
void main ()
{
int digit = 0;
while(digit<=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
{
statement1;
statement2;
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
#include<stdio.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;
printf(“nI=%d”,i);
}
CONTINUE STATEMENT
• The continue statement is used to skip or to bypass
some step or iteration of looping structure.
for(i=1; i<=10; i++)
{
if(i<5)
continue;
printf(“ni=%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”);
}

More Related Content

PPTX
Inter Thread Communicationn.pptx
PPTX
While , For , Do-While Loop
PPTX
Pointers in C
PPTX
python conditional statement.pptx
PDF
USER DEFINED FUNCTIONS IN C.pdf
PDF
Pseudocode & flowchart examples
PPTX
PPT
Types of operators in C
Inter Thread Communicationn.pptx
While , For , Do-While Loop
Pointers in C
python conditional statement.pptx
USER DEFINED FUNCTIONS IN C.pdf
Pseudocode & flowchart examples
Types of operators in C

What's hot (20)

PPTX
Final keyword in java
PPTX
Control Flow Statements
PPTX
If statements in c programming
PPTX
Control structures in java
PPTX
Exception handling in Java
PPTX
Handling of character strings C programming
PPTX
Classes objects in java
PPTX
Type casting in c programming
PPTX
Access specifier
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PPTX
Control statements in java
PPTX
Pointers in c - Mohammad Salman
PPTX
PDF
VIT351 Software Development VI Unit3
PPTX
Data types
PPT
Function overloading(c++)
PPTX
Super Keyword in Java.pptx
PPTX
C++ concept of Polymorphism
PPTX
Presentation on C Switch Case Statements
Final keyword in java
Control Flow Statements
If statements in c programming
Control structures in java
Exception handling in Java
Handling of character strings C programming
Classes objects in java
Type casting in c programming
Access specifier
CONDITIONAL STATEMENT IN C LANGUAGE
Control statements in java
Pointers in c - Mohammad Salman
VIT351 Software Development VI Unit3
Data types
Function overloading(c++)
Super Keyword in Java.pptx
C++ concept of Polymorphism
Presentation on C Switch Case Statements
Ad

Similar to INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C (20)

PPT
PPTX
Control Structures in C
PPT
Lec 10
PPTX
computer programming Control Statements.pptx
PPTX
Control structure of c
PPT
12 lec 12 loop
DOC
3. control statement
PPTX
C programming Control Structure.pptx
PPTX
Condition Stmt n Looping stmt.pptx
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
COM1407: Program Control Structures – Repetition and Loops
PPTX
C Programming Unit-2
PPTX
C Programming: Control Structure
PPTX
CONTROL FLOW in C.pptx
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
DOC
Slide07 repetitions
PPTX
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PDF
Programming fundamental 02
Control Structures in C
Lec 10
computer programming Control Statements.pptx
Control structure of c
12 lec 12 loop
3. control statement
C programming Control Structure.pptx
Condition Stmt n Looping stmt.pptx
Unit ii chapter 2 Decision making and Branching in C
COM1407: Program Control Structures – Repetition and Loops
C Programming Unit-2
C Programming: Control Structure
CONTROL FLOW in C.pptx
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
Slide07 repetitions
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
Programming fundamental 02
Ad

More from Dr. Chandrakant Divate (20)

PDF
Yoga and Mediation Lab manual Final for MSBTE Diploma Student
PPTX
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
PPTX
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
PPTX
Experiment -7 Performing Asanas In Standing Position
PPTX
Experiment -6 Performing Asanas In Sitting Position
PPTX
Performing Supine Position Asanas- Sleeping on your back.
PPTX
Performing Prone Position Asanas- Sleeping on your back.
PPTX
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
PPTX
Perform warming up exercises to prepare the body from head to toe for Yoga.
PPTX
An introduction to yoga - Brief History and Publicity of Yoga in Universe
PDF
Web Technology LAB MANUAL for Undergraduate Programs
PPTX
UNIVERSAL HUMAN VALUES- Harmony in the Nature
PPTX
Study of Computer Hardware System using Block Diagram
PPTX
Computer System Output Devices Peripherals
PPTX
Computer system Input Devices Peripherals
PPTX
Computer system Input and Output Devices
PPTX
Introduction to COMPUTER’S MEMORY RAM and ROM
PPTX
Introduction to Computer Hardware Systems
PPTX
Fundamentals of Internet of Things (IoT) Part-2
PPTX
Fundamentals of Internet of Things (IoT)
Yoga and Mediation Lab manual Final for MSBTE Diploma Student
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
Experiment -7 Performing Asanas In Standing Position
Experiment -6 Performing Asanas In Sitting Position
Performing Supine Position Asanas- Sleeping on your back.
Performing Prone Position Asanas- Sleeping on your back.
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
Perform warming up exercises to prepare the body from head to toe for Yoga.
An introduction to yoga - Brief History and Publicity of Yoga in Universe
Web Technology LAB MANUAL for Undergraduate Programs
UNIVERSAL HUMAN VALUES- Harmony in the Nature
Study of Computer Hardware System using Block Diagram
Computer System Output Devices Peripherals
Computer system Input Devices Peripherals
Computer system Input and Output Devices
Introduction to COMPUTER’S MEMORY RAM and ROM
Introduction to Computer Hardware Systems
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT)

Recently uploaded (20)

PPT
Project quality management in manufacturing
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
web development for engineering and engineering
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
DOCX
573137875-Attendance-Management-System-original
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Welding lecture in detail for understanding
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PPT on Performance Review to get promotions
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Project quality management in manufacturing
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Embodied AI: Ushering in the Next Era of Intelligent Systems
web development for engineering and engineering
UNIT 4 Total Quality Management .pptx
Sustainable Sites - Green Building Construction
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Strings in CPP - Strings in C++ are sequences of characters used to store and...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Foundation to blockchain - A guide to Blockchain Tech
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Model Code of Practice - Construction Work - 21102022 .pdf
573137875-Attendance-Management-System-original
Operating System & Kernel Study Guide-1 - converted.pdf
Welding lecture in detail for understanding
Internet of Things (IOT) - A guide to understanding
PPT on Performance Review to get promotions
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...

INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C

  • 1. INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C C. P. Divate
  • 2. 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 1) printf() 2) scanf().
  • 3. 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.
  • 4. printf() Function • To print the value of some variable on the screen Integer Variable : int a=10; printf(“%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.
  • 5. 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
  • 6. scanf() Function • scanf() function is use to read data from keyboard and to store that data in the variables. • The general syntax for scanf() function is as follows. scanf(“Format String”,&variable); Here format string is used to define which type of data it is taking as input. this format string can be %c for character, %d for integer variable and %f for float variable.
  • 7. 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.
  • 8. 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.
  • 9. scanf() Function • For Float Variable : float per; printf(“Enter Percentage=”); scanf(“%f”,&per); • For Character Variable : char ans; printf(“Enter answer=”); scanf(“%c”,&ans);
  • 10. 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=getchar();
  • 11. 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’; putchar(c);
  • 12. Control Flow In C • Objectives of the module is 1)How to direct the sequence of execution using Decision control Structure 2)Have an understanding of the iterative process using Loop Control Structure
  • 13. 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, in its simplest general form, the statement can be written. if(expression) { statement; }
  • 14. • 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
  • 17. /* 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”); }
  • 18. Else if ladder if( expression1 ) statement1; else if( expression2 ) statement2; else statement3; For Example if( age < 18 ) printf("Minor"); else if( age < 65 ) printf("Adult"); else printf( "Senior Citizen"); •
  • 19. Decision Control Structure • The switch statement: causes a particular group of statements 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.
  • 20. The general form of switch-case switch(expression) { case expression1: statements; break; case expression2: statements; break; case expression3: statements; break; }
  • 21. • 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’ : printf(“RED”); break; case ‘b’ : printf(“BLUE”); break; default : printf(“ERROR”); break; }
  • 22. 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 (1) While loop (2) Do..while (3) For loop
  • 23. While Loop • The while statement is used to carry out looping operations. • The general form of the statements initialization; while(exp) { statement 1; statement 2; increment/ decrement; }
  • 24. While loop example #include<stdio.h> void main () { int digit = 0; while(digit<=9) { printf(“%d n”,digit); ++digit ; } }
  • 25. 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 { statement1; statement2; increment/decrement operator; } while(expression);
  • 26. Do-While Loop Example #include <stdio.h> void main() { int digit = 0; do { printf(“%d”, digit++); }while(digit<=9); }
  • 27. 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; }
  • 28. For loop example #include<stdio.h> void main() { for(x=0; x<9; x++) { printf(“%d”, x); printf(“n”); } }
  • 29. 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”); }
  • 30. 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; printf(“nI=%d”,i); }
  • 31. CONTINUE STATEMENT • The continue statement is used to skip or to bypass some step or iteration of looping structure. for(i=1; i<=10; i++) { if(i<5) continue; printf(“ni=%d”,i); } The output of the above program will be 6,7,8,9,10.
  • 32. 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.
  • 33. 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”); }