SlideShare a Scribd company logo
1 of 40
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
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
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);
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)

C functions
C functionsC functions
C functions
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inline function
Inline functionInline function
Inline function
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Loops c++
Loops c++Loops c++
Loops c++
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
user defined function
user defined functionuser defined function
user defined function
 

Similar to Input and Output In C Language

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 Functionimtiazalijoono
 
Basic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.comBasic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.comAkanchha Agrawal
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptxRohitRaj744272
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingMd. Ashikur Rahman
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and typesimtiazalijoono
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of Ceducationfront
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5REHAN IJAZ
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 
Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg PatelTechNGyan
 

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
 
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
 
First c program
First c programFirst c program
First c program
 
Basic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.comBasic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.com
 
C programming
C programmingC programming
C programming
 
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. AnsariBasic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programming
 
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
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of C
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg Patel
 

Recently uploaded

How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

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
  • 7.
  • 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
  • 19.
  • 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);
  • 26.
  • 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’);