SlideShare a Scribd company logo
By: Prof. Adnan Faisal Khan
By: Prof. Adnan Faisal Khan
CHAPTER 10
Standard Input
• Standard input refers to the input using keyboard
• A program may need certain input from the user
• C language provides many functions to get intput from the user:
• scanf()
• gets()
• getch()
• getche()
• Include the header file <stdio.h> in program to call scanf() and gets()
and include the header file <conio.h> in program to call getch() and
getche() function.
Standard Output
• The term standard output refers to the output displayed on the monitor
• The result of a program is called the out of the program
• C language provides many functions to display program output to the
user
• Printf()
• Puts()
• Include the header file <stdio.h> in program to call printf() and puts()
printf ( ) function
• The printf function is used to display output on the monitor
• It can display text, constants, values of variables or expressions on
monitor in specified format:
Syntax: printf (Format String, argument_list);
Format String Argument list
• Also called control string
• Give in double quotes
• May consist of the following:
• Text
• Format specifiers
• Escape sequence
• Consists of constants, variable
or expression are to be printed
on the screen
• Separated by comma
• Use of argument list is
optional
• Using Format String without Argument List
printf(“Hello World”);
Control String
• Using Format String with Single Argument
int m =80;
printf(“ Your marks are %d”, m);
Control String
• Using Format String with Multiple Arguments
int m=80;
char c=‘A’;
• Printf(“your marks are %d and grade %c”, m, c);
• One printf can print several lines by using newline escape sequence – ‘n’
printf( “ Welcome n to n C “);
Format strings can have
multiple format specifier, if
you are printing multiple
values
Input and Output In C Language
Program 10.1
Write a program that displays a message and values of integer and character
variables.
#include<stdio.h>
#include<conio.h>
void main()
{
int n=10;
char ch='*';
clrscr();
printf("Testing Output...");
printf("%d",n);
printf("%c",ch);
getch();
}
Program 10.2
Write a program that adds two floating point numbers and shows the
sum on the screen
#include<stdio.h>
#include<conio.h>
void main()
{
float a=2.5,b=3.0,sum;
sum = a+ b;
clrscr();
printf(“%f + %f = %f ”,a,b,sum);
getch();
}
Program 10.3
Write a program that calculate and print area of square with given height and width
#include<stdio.h>
#include<conio.h>
void main()
{
int area,h,w;
h=2;
w=4;
area= h *w;
printf(“Area of square is %d”,area);
// printf(“ Area of square is %d when height is %d and width is %d”,area,h,w);
getch();
}
Format Specifier
• Format specifier is used to specify the format according to which values
will be read and display
• It determines the following things:
• Data type of Variable
• Field width
• Format of the value
• Format specifier is started with symbol %
• Different format specifiers are used with different types of variable
• Integer format specifier
• Character format specifier
• Floating Point format specifier
3 4 5 1
3 4 5 1
Format Specifier
• Integer Format Specifier
Format Specifier Type
%d Used for signed decimal integer value
%i Used for signed integer value
%o Used for unsigned octal value
%u Used for unsigned integer value
%x Used for unsigned hexadecimal value with lower case like a,b,c
etc.
%X Used for unsigned hexadecimal value with Upper case like A,B,C
etc.
Format Specifier (Cont..)
• Floating-Point Format Specifier
• Character Format Specifier
Format Specifier Type
%f Used for signed floating point value
%e Used for exponent value
%g Used for short notation value
Format Specifier Type
%c Used for character value
%s Used for strings
How integer values are printed in C language?
• The format specifier %d is used to display integer values in printf
functions.
• If an integer variable m contains 100 value then it can print by
following statement:
printf(“Your marks are %d”, m);
output:
Your marks are 100
How floating point values are printed in C
language?
• The format specifier %f is used to display floating point value in printf
function.
• If an value of float variable a is 90.55 . The following statement:
printf(“Average marks are %f”, a);
output:
Average marks are 90.550000
Field width Specifier
Field Width specifier for integer
• %d format specifier is used to display integer value in printf function
• A value can be written between % and d in “%d” format specified
Example
int area =25;
printf(“Area = %4d”,area);
• The value will appear right-justified and two spaces will appear before the actual
value
Specifying width and precision for floating point
value
Input and Output In C Language
Programs From
10.4 to 10.8 from book
Escape Sequence
• Special characters used in format string to modify the format
of output
• Character are not displayed in the output
• Always begin with backslash “”.
• Backslash is called an escape character
Escape sequence Character represented
b Backspace
f Form Feed
n New Line
r Carriage return
t Tab
’ Single quote
” Double quote
xdd
ASCII code in hexadecimal notation. Each d represent
digit
ddd ASCII code in octal notation. Each d represents a digit
• b use to insert backspace in the output
• f (Form Feed page break)
• n (New line) – We use it to shift the cursor control to the new line
printf(
• r (Carriage Return) – We use it to position the cursor to the
beginning of the current line.
• t (Horizontal tab) – We use it to shift the cursor to a couple of spaces
to the right in the same line.
• ’ (Apostrophe or single quotation mark) – We use it to display the
single-quotation mark.
• ” (Double quotation mark) – We use it to display the double-
quotation mark.
•  (Backslash) – We use it to display the backslash character.
• a (Audible bell) – A beep is generated indicating the execution of the
program to alert the user.
Programs From
10.9 to 10.11 from book
Scanf Function
• Read data from the standard input device ( Usually Keyboard) and
store it in a variable
• Requires stdio.h header file used to read input from keyboard
• Syntax
• scanf(“format string”, &variable);
Input and Output In C Language
Programs From 10.12 to
10.22 from book
Character Input
• Scanf function can be used for character input
• Scanf function is not suitable for all situations, specially when enter
key is not require on any input ( e.g. arrow keys in games)
• Specialized function for character input are available
• getch()
• getche()
• These functions are part of conio.h library file
getch()
• The getch function is used to input single character from the user
• Requires conio.h header file to use this function
• When this function is executed, it waits for any key to be pressed
• Character entered by the user is not displayed on the screen
• The function is frequently used to pause program execution
Syntax: [var =] getch();
Variable It indicates the variable in which the character is stored. The
use of variable is optional
Program 10.23
#include<stdio.h>
#include<conio.h>
Void main()
{
char c;
clrscr();
printf(“ Enter Character ”);
c= getch();
printf(“n You Entered %c”,c);
getch();
}
Enter Character
You Entered s
Output
getche()
• The getche() function is used to input single character from the user
• Requires conio.h header file to use this function
• When this function is executed, it waits for any key to be pressed
• Character entered by the user displayed on the screen
Syntax: [var =] getche();
Variable It indicates the variable in which the character is stored. The
use of variable is optional
Program 10.24
#include<stdio.h>
#include<conio.h>
Void main()
{
char c;
clrscr();
printf(“ Enter Character ”);
c= getche();
printf(“n You Entered %c”,c);
getch();
}
Enter Character s
You Entered s
Output
gets() Function
• Used to input string value from the user.
• User press Enter key and string is stored variable
• The null character is automatically entered at the end of string
• Requires stdio.h header file to use this function
Syntax: gets( String_Variable );
Example printf(“Enter a string”);
gets(str);
Suppose if user input “Pakistan” then the string will stores in str as follows:
P a k i s t a n 0
puts() Function
• Used to display string on the screen.
• It can display a string constant or string variable
• Requires stdio.h header file to use this function
Syntax: puts( parameter );
Parameter It indicates the string variable in which the string is stored.
In case of string constant, it is written in double quotes
Example puts(str);
• Program 10.25
Write a program that inputs a string and displays it on a screen
#include<stdio.h>
#include<conio.h>
void main()
{
char book[ 50];
clrscr();
printf(“Enter name of your favorit book”);
gets(book);
printf(“Your favorite book is”);
puts(book);
getch();
}
Enter name of your favourite book : Holy Quran
Your favourite book is : Holy Quran
clrscr() Function
• Used to clear screen
• after clear the screen cursor blinks on the top-left corner
• Requires conio.h header file to use this function
Syntax: clrscr();
sizeof Operator
• Used to find the size of any data value
• Its give the number of bytes occupied by that value
Syntax: sizeof(operand);
Examples : sizeof(10);
sizeof(4.5);
sizeof(“Pakistan”);
sizeof(‘A’);
Input and Output In C Language
Input and Output In C Language
Input and Output In C Language

More Related Content

What's hot (20)

Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
Ashim Lamichhane
 
File handling in c
File handling in cFile handling in c
File handling in c
aakanksha s
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
File in c
File in cFile in c
File in c
Prabhu Govind
 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
baabtra.com - No. 1 supplier of quality freshers
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
File handling-c
File handling-cFile handling-c
File handling-c
CGC Technical campus,Mohali
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
Aditya Vaishampayan
 
Function
FunctionFunction
Function
jayesh30sikchi
 
Operators php
Operators phpOperators php
Operators php
Chandni Pm
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
Samiksha Pun
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
baabtra.com - No. 1 supplier of quality freshers
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
Emertxe Information Technologies Pvt Ltd
 
C formatted and unformatted input and output constructs
C  formatted and unformatted input and output constructsC  formatted and unformatted input and output constructs
C formatted and unformatted input and output constructs
GopikaS12
 
String in c programming
String in c programmingString in c programming
String in c programming
Devan Thakur
 
BREAK AND CONTINUE.pptx
BREAK AND CONTINUE.pptxBREAK AND CONTINUE.pptx
BREAK AND CONTINUE.pptx
visaagan2022
 

Similar to Input and Output In C Language (20)

UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
JavvajiVenkat
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Input Output function in c programing language.pptx
Input  Output function in c programing language.pptxInput  Output function in c programing language.pptx
Input Output function in c programing language.pptx
amit0815q
 
20220823094225_PPT02-Formatted Input and Output.pptx
20220823094225_PPT02-Formatted Input and Output.pptx20220823094225_PPT02-Formatted Input and Output.pptx
20220823094225_PPT02-Formatted Input and Output.pptx
putrielisabeth3
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
yazad dumasia
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
mohd_mizan
 
Chap 2 input output dti2143
Chap 2  input output dti2143Chap 2  input output dti2143
Chap 2 input output dti2143
alish sha
 
Functions of stdio conio
Functions of stdio   conio Functions of stdio   conio
Functions of stdio conio
Bhavik Vashi
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
Thesis Scientist Private Limited
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
Dr. SURBHI SAROHA
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
RohitRaj744272
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
C language
C languageC language
C language
TaranjeetKaur72
 
Lecture#5 c lang new
Lecture#5 c lang newLecture#5 c lang new
Lecture#5 c lang new
Zeeshan Ahmad
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
Hemantha Kulathilake
 
C basics
C   basicsC   basics
C basics
thirumalaikumar3
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdfModule 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
 
Unit No 2.pptx Basic s of C Programming
Unit No 2.pptx   Basic s of C ProgrammingUnit No 2.pptx   Basic s of C Programming
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Input Output function in c programing language.pptx
Input  Output function in c programing language.pptxInput  Output function in c programing language.pptx
Input Output function in c programing language.pptx
amit0815q
 
20220823094225_PPT02-Formatted Input and Output.pptx
20220823094225_PPT02-Formatted Input and Output.pptx20220823094225_PPT02-Formatted Input and Output.pptx
20220823094225_PPT02-Formatted Input and Output.pptx
putrielisabeth3
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
yazad dumasia
 
Chap 2 input output dti2143
Chap 2  input output dti2143Chap 2  input output dti2143
Chap 2 input output dti2143
alish sha
 
Functions of stdio conio
Functions of stdio   conio Functions of stdio   conio
Functions of stdio conio
Bhavik Vashi
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
RohitRaj744272
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Lecture#5 c lang new
Lecture#5 c lang newLecture#5 c lang new
Lecture#5 c lang new
Zeeshan Ahmad
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdfModule 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
 
Unit No 2.pptx Basic s of C Programming
Unit No 2.pptx   Basic s of C ProgrammingUnit No 2.pptx   Basic s of C Programming
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
Ad

Recently uploaded (20)

Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
muneebrana3215
 
A Brief Introduction About Jack Lutkus
A Brief Introduction About  Jack  LutkusA Brief Introduction About  Jack  Lutkus
A Brief Introduction About Jack Lutkus
Jack Lutkus
 
Writing Research Papers: Guidance for Research Community
Writing Research Papers: Guidance for Research CommunityWriting Research Papers: Guidance for Research Community
Writing Research Papers: Guidance for Research Community
Rishi Bankim Chandra Evening College, Naihati, North 24 Parganas, West Bengal, India
 
How to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo SlidesHow to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo Slides
Celine George
 
How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
 
Odoo 18 Point of Sale PWA - Odoo Slides
Odoo 18 Point of Sale PWA  - Odoo  SlidesOdoo 18 Point of Sale PWA  - Odoo  Slides
Odoo 18 Point of Sale PWA - Odoo Slides
Celine George
 
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
wygalkelceqg
 
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General QuizPragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya - UEM Kolkata Quiz Club
 
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANASTUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
Kweku Zurek
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
 
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGYHUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
DHARMENDRA SAHU
 
Order: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptxOrder: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptx
Arshad Shaikh
 
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptxQUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
Sourav Kr Podder
 
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT PatnaSwachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Quiz Club, Indian Institute of Technology, Patna
 
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
 
How to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 WebsiteHow to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 Website
Celine George
 
Introduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdfIntroduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdf
CME4Life
 
Multicultural approach in education - B.Ed
Multicultural approach in education - B.EdMulticultural approach in education - B.Ed
Multicultural approach in education - B.Ed
prathimagowda443
 
Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
muneebrana3215
 
A Brief Introduction About Jack Lutkus
A Brief Introduction About  Jack  LutkusA Brief Introduction About  Jack  Lutkus
A Brief Introduction About Jack Lutkus
Jack Lutkus
 
How to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo SlidesHow to Use Owl Slots in Odoo 17 - Odoo Slides
How to Use Owl Slots in Odoo 17 - Odoo Slides
Celine George
 
How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
 
Odoo 18 Point of Sale PWA - Odoo Slides
Odoo 18 Point of Sale PWA  - Odoo  SlidesOdoo 18 Point of Sale PWA  - Odoo  Slides
Odoo 18 Point of Sale PWA - Odoo Slides
Celine George
 
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
wygalkelceqg
 
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANASTUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
Kweku Zurek
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
 
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGYHUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
DHARMENDRA SAHU
 
Order: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptxOrder: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptx
Arshad Shaikh
 
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptxQUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
Sourav Kr Podder
 
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
 
How to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 WebsiteHow to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 Website
Celine George
 
Introduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdfIntroduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdf
CME4Life
 
Multicultural approach in education - B.Ed
Multicultural approach in education - B.EdMulticultural approach in education - B.Ed
Multicultural approach in education - B.Ed
prathimagowda443
 
Ad

Input and Output In C Language

  • 1. By: Prof. Adnan Faisal Khan
  • 2. By: Prof. Adnan Faisal Khan CHAPTER 10
  • 3. Standard Input • Standard input refers to the input using keyboard • A program may need certain input from the user • C language provides many functions to get intput from the user: • scanf() • gets() • getch() • getche() • Include the header file <stdio.h> in program to call scanf() and gets() and include the header file <conio.h> in program to call getch() and getche() function.
  • 4. Standard Output • The term standard output refers to the output displayed on the monitor • The result of a program is called the out of the program • C language provides many functions to display program output to the user • Printf() • Puts() • Include the header file <stdio.h> in program to call printf() and puts()
  • 5. printf ( ) function • The printf function is used to display output on the monitor • It can display text, constants, values of variables or expressions on monitor in specified format: Syntax: printf (Format String, argument_list); Format String Argument list • Also called control string • Give in double quotes • May consist of the following: • Text • Format specifiers • Escape sequence • Consists of constants, variable or expression are to be printed on the screen • Separated by comma • Use of argument list is optional
  • 6. • Using Format String without Argument List printf(“Hello World”); Control String • Using Format String with Single Argument int m =80; printf(“ Your marks are %d”, m); Control String • Using Format String with Multiple Arguments int m=80; char c=‘A’; • Printf(“your marks are %d and grade %c”, m, c); • One printf can print several lines by using newline escape sequence – ‘n’ printf( “ Welcome n to n C “); Format strings can have multiple format specifier, if you are printing multiple values
  • 8. Program 10.1 Write a program that displays a message and values of integer and character variables. #include<stdio.h> #include<conio.h> void main() { int n=10; char ch='*'; clrscr(); printf("Testing Output..."); printf("%d",n); printf("%c",ch); getch(); }
  • 9. Program 10.2 Write a program that adds two floating point numbers and shows the sum on the screen #include<stdio.h> #include<conio.h> void main() { float a=2.5,b=3.0,sum; sum = a+ b; clrscr(); printf(“%f + %f = %f ”,a,b,sum); getch(); }
  • 10. Program 10.3 Write a program that calculate and print area of square with given height and width #include<stdio.h> #include<conio.h> void main() { int area,h,w; h=2; w=4; area= h *w; printf(“Area of square is %d”,area); // printf(“ Area of square is %d when height is %d and width is %d”,area,h,w); getch(); }
  • 11. Format Specifier • Format specifier is used to specify the format according to which values will be read and display • It determines the following things: • Data type of Variable • Field width • Format of the value • Format specifier is started with symbol % • Different format specifiers are used with different types of variable • Integer format specifier • Character format specifier • Floating Point format specifier 3 4 5 1 3 4 5 1
  • 12. Format Specifier • Integer Format Specifier Format Specifier Type %d Used for signed decimal integer value %i Used for signed integer value %o Used for unsigned octal value %u Used for unsigned integer value %x Used for unsigned hexadecimal value with lower case like a,b,c etc. %X Used for unsigned hexadecimal value with Upper case like A,B,C etc.
  • 13. Format Specifier (Cont..) • Floating-Point Format Specifier • Character Format Specifier Format Specifier Type %f Used for signed floating point value %e Used for exponent value %g Used for short notation value Format Specifier Type %c Used for character value %s Used for strings
  • 14. How integer values are printed in C language? • The format specifier %d is used to display integer values in printf functions. • If an integer variable m contains 100 value then it can print by following statement: printf(“Your marks are %d”, m); output: Your marks are 100
  • 15. How floating point values are printed in C language? • The format specifier %f is used to display floating point value in printf function. • If an value of float variable a is 90.55 . The following statement: printf(“Average marks are %f”, a); output: Average marks are 90.550000
  • 17. Field Width specifier for integer • %d format specifier is used to display integer value in printf function • A value can be written between % and d in “%d” format specified Example int area =25; printf(“Area = %4d”,area); • The value will appear right-justified and two spaces will appear before the actual value
  • 18. Specifying width and precision for floating point value
  • 20. Programs From 10.4 to 10.8 from book
  • 21. Escape Sequence • Special characters used in format string to modify the format of output • Character are not displayed in the output • Always begin with backslash “”. • Backslash is called an escape character
  • 22. Escape sequence Character represented b Backspace f Form Feed n New Line r Carriage return t Tab ’ Single quote ” Double quote xdd ASCII code in hexadecimal notation. Each d represent digit ddd ASCII code in octal notation. Each d represents a digit
  • 23. • b use to insert backspace in the output • f (Form Feed page break) • n (New line) – We use it to shift the cursor control to the new line printf( • r (Carriage Return) – We use it to position the cursor to the beginning of the current line. • t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line. • ’ (Apostrophe or single quotation mark) – We use it to display the single-quotation mark. • ” (Double quotation mark) – We use it to display the double- quotation mark. • (Backslash) – We use it to display the backslash character. • a (Audible bell) – A beep is generated indicating the execution of the program to alert the user.
  • 24. Programs From 10.9 to 10.11 from book
  • 25. Scanf Function • Read data from the standard input device ( Usually Keyboard) and store it in a variable • Requires stdio.h header file used to read input from keyboard • Syntax • scanf(“format string”, &variable);
  • 27. Programs From 10.12 to 10.22 from book
  • 28. Character Input • Scanf function can be used for character input • Scanf function is not suitable for all situations, specially when enter key is not require on any input ( e.g. arrow keys in games) • Specialized function for character input are available • getch() • getche() • These functions are part of conio.h library file
  • 29. getch() • The getch function is used to input single character from the user • Requires conio.h header file to use this function • When this function is executed, it waits for any key to be pressed • Character entered by the user is not displayed on the screen • The function is frequently used to pause program execution Syntax: [var =] getch(); Variable It indicates the variable in which the character is stored. The use of variable is optional
  • 30. Program 10.23 #include<stdio.h> #include<conio.h> Void main() { char c; clrscr(); printf(“ Enter Character ”); c= getch(); printf(“n You Entered %c”,c); getch(); } Enter Character You Entered s Output
  • 31. getche() • The getche() function is used to input single character from the user • Requires conio.h header file to use this function • When this function is executed, it waits for any key to be pressed • Character entered by the user displayed on the screen Syntax: [var =] getche(); Variable It indicates the variable in which the character is stored. The use of variable is optional
  • 32. Program 10.24 #include<stdio.h> #include<conio.h> Void main() { char c; clrscr(); printf(“ Enter Character ”); c= getche(); printf(“n You Entered %c”,c); getch(); } Enter Character s You Entered s Output
  • 33. gets() Function • Used to input string value from the user. • User press Enter key and string is stored variable • The null character is automatically entered at the end of string • Requires stdio.h header file to use this function Syntax: gets( String_Variable ); Example printf(“Enter a string”); gets(str); Suppose if user input “Pakistan” then the string will stores in str as follows: P a k i s t a n 0
  • 34. puts() Function • Used to display string on the screen. • It can display a string constant or string variable • Requires stdio.h header file to use this function Syntax: puts( parameter ); Parameter It indicates the string variable in which the string is stored. In case of string constant, it is written in double quotes Example puts(str);
  • 35. • Program 10.25 Write a program that inputs a string and displays it on a screen #include<stdio.h> #include<conio.h> void main() { char book[ 50]; clrscr(); printf(“Enter name of your favorit book”); gets(book); printf(“Your favorite book is”); puts(book); getch(); } Enter name of your favourite book : Holy Quran Your favourite book is : Holy Quran
  • 36. clrscr() Function • Used to clear screen • after clear the screen cursor blinks on the top-left corner • Requires conio.h header file to use this function Syntax: clrscr();
  • 37. sizeof Operator • Used to find the size of any data value • Its give the number of bytes occupied by that value Syntax: sizeof(operand); Examples : sizeof(10); sizeof(4.5); sizeof(“Pakistan”); sizeof(‘A’);