SlideShare a Scribd company logo
1 of 15
Structured Programming Language
Data Output (printf)
Mohammad Imam Hossain,
Lecturer, CSE, UIU
printf Function
int printf (control string, arg1, arg2, …, argn)
• Writes the control string to the standard
output.
• If control string includes format specifier
(subsequences beginning with %), the
additional arguments following control
string are formatted and inserted in the
resulting string replacing their respective
specifiers.
Code Sample
Format Specifier of printf
%[flags][width][.precision][length]specifier
Specifier Output Example
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 07
x or X Unsigned hexadecimal integer 7fa or 7FA
f Floating point 392.65
e Scientific notation 3.9265e+2
g Use the shortest representation: %e or %f 392.65
c character A
s String of characters sample
% % followed by percent write % to the output %
Code Sample
Format Specifier of printf
%[flags][width][.precision][length]specifier
Specifiers
length d , i u , o , x, X f , e ,g c s
none int unsigned int float char char *
hh signed char unsigned char
h short int unsigned short int
l long int unsigned long int double(lf)
ll long long int unsigned long long int
L 392.65 long double
Code Sample
Format Specifier of printf
%[flags][width][.precision][length]specifier
width description
number Minimum number of characters to be printed. If the value to be printed
is shorter than this number, the result is padded with blank spaces. The
value is not truncated even if the result is larger.
* The width is not specified in the format string, but as an additional
integer value argument preceding the argument that has to be
formatted.
Code Sample
Format Specifier of printf
%[flags][width][.precision][length]specifier
.precision description
.number For e, f , g specifiers: this is the no of digits to be printed after the
decimal point (by default this is 6)
For s : this is the maximum number of characters to be printed. By
default all the characters are printed until the ending null character is
encountered.
.* The precision is not specified in the format string, but as an additional
integer value argument preceding the argument that has to be
formatted.
Code Sample
Format Specifier of printf
%[flags][width][.precision][length]specifier
flags Description
- Left-justify within the given field width; right justification is the default
+ Forces to precede the result with a plus or a minus sign.
# For o, x, X : the value is preceded with 0, 0x, 0X respectively for values
different than zero.
For e, f, g : it forces the written output to contain a decimal point even
if no digits follow.
0 Left-pads the number with zeros instead of spaces when padding is
specified
Code Sample
Practice problems
1. A C program contains the following variable declarations
float a=2.5, b=0.0005, c=3000.;
Show the output resulting from each of the following printf
statements.
a) printf(“%f %f %f”,a,b,c);
b) printf(“%3f %3f %3f”,a,b,c);
c) printf(“%8f %8f %8f”,a,b,c);
d) printf(“%8.4f %8.4f %8.4f”,a,b,c);
e) printf(“%8.3f %8.3f %8.3f”,a,b,c);
f) printf(“%-8f %-8f %-8f”,a,b,c);
g) printf(“%+8f %+8f %+8f”,a,b,c);
h) printf(“%08f %08f %08f”,a,b,c);
i) printf(“%#8f %#8f %#8f”,a,b,c);
Practice problems
2. Assume that i ,j , k are integer variables and i represents an
octal quantity, j represents a decimal quantity and k
represents a hexadecimal quantity. Write an appropriate
printf function for each of the following statements:
a) Display the values for i ,j , k with minimum field width of
eight characters for each value
b) Repeat part (a) with each output data item left justified
within its respective field.
c) Repeat part (a) with each output data item preceded by
zeros(0x, in the case of the hexadecimal quantity)

More Related Content

What's hot

What's hot (19)

Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
L03vars
L03varsL03vars
L03vars
 
Assignment8
Assignment8Assignment8
Assignment8
 
C programming part4
C programming part4C programming part4
C programming part4
 
keyword
keywordkeyword
keyword
 
Ch8 Arrays
Ch8 ArraysCh8 Arrays
Ch8 Arrays
 
Variables
VariablesVariables
Variables
 
C intro
C introC intro
C intro
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Huffman codes
Huffman codesHuffman codes
Huffman codes
 
Huffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh AgarwalHuffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh Agarwal
 
Huffman Code Decoding
Huffman Code DecodingHuffman Code Decoding
Huffman Code Decoding
 
Std 10 computer chapter 10 introduction to c language (part2)
Std 10 computer chapter 10 introduction to c language (part2)Std 10 computer chapter 10 introduction to c language (part2)
Std 10 computer chapter 10 introduction to c language (part2)
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
Pointers
PointersPointers
Pointers
 
Chtp6 09
Chtp6 09Chtp6 09
Chtp6 09
 
Formatted I/O statement in C
Formatted I/O statement in CFormatted I/O statement in C
Formatted I/O statement in C
 

Similar to SPL 4 | printf in C

Similar to SPL 4 | printf in C (20)

Input And Output
 Input And Output Input And Output
Input And Output
 
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
 
T03 a basicioprintf
T03 a basicioprintfT03 a basicioprintf
T03 a basicioprintf
 
SPL 5 | scanf in C
SPL 5 | scanf in CSPL 5 | scanf in C
SPL 5 | scanf in C
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in c
 
Unit 2- Module 2.pptx
Unit 2- Module 2.pptxUnit 2- Module 2.pptx
Unit 2- Module 2.pptx
 
C format string vulnerability
C format string vulnerabilityC format string vulnerability
C format string vulnerability
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
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
 
scanf function in c, variations in conversion specifier
scanf function in c, variations in conversion specifierscanf function in c, variations in conversion specifier
scanf function in c, variations in conversion specifier
 
7512635.ppt
7512635.ppt7512635.ppt
7512635.ppt
 
Ch02
Ch02Ch02
Ch02
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 
C programming language for beginners
C programming language for beginners C programming language for beginners
C programming language for beginners
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
 
Format String Attack
Format String AttackFormat String Attack
Format String Attack
 

More from Mohammad Imam Hossain

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

SPL 4 | printf in C

  • 1. Structured Programming Language Data Output (printf) Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2. printf Function int printf (control string, arg1, arg2, …, argn) • Writes the control string to the standard output. • If control string includes format specifier (subsequences beginning with %), the additional arguments following control string are formatted and inserted in the resulting string replacing their respective specifiers.
  • 4. Format Specifier of printf %[flags][width][.precision][length]specifier Specifier Output Example d or i Signed decimal integer 392 u Unsigned decimal integer 7235 o Unsigned octal 07 x or X Unsigned hexadecimal integer 7fa or 7FA f Floating point 392.65 e Scientific notation 3.9265e+2 g Use the shortest representation: %e or %f 392.65 c character A s String of characters sample % % followed by percent write % to the output %
  • 6. Format Specifier of printf %[flags][width][.precision][length]specifier Specifiers length d , i u , o , x, X f , e ,g c s none int unsigned int float char char * hh signed char unsigned char h short int unsigned short int l long int unsigned long int double(lf) ll long long int unsigned long long int L 392.65 long double
  • 8. Format Specifier of printf %[flags][width][.precision][length]specifier width description number Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger. * The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
  • 10. Format Specifier of printf %[flags][width][.precision][length]specifier .precision description .number For e, f , g specifiers: this is the no of digits to be printed after the decimal point (by default this is 6) For s : this is the maximum number of characters to be printed. By default all the characters are printed until the ending null character is encountered. .* The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
  • 12. Format Specifier of printf %[flags][width][.precision][length]specifier flags Description - Left-justify within the given field width; right justification is the default + Forces to precede the result with a plus or a minus sign. # For o, x, X : the value is preceded with 0, 0x, 0X respectively for values different than zero. For e, f, g : it forces the written output to contain a decimal point even if no digits follow. 0 Left-pads the number with zeros instead of spaces when padding is specified
  • 14. Practice problems 1. A C program contains the following variable declarations float a=2.5, b=0.0005, c=3000.; Show the output resulting from each of the following printf statements. a) printf(“%f %f %f”,a,b,c); b) printf(“%3f %3f %3f”,a,b,c); c) printf(“%8f %8f %8f”,a,b,c); d) printf(“%8.4f %8.4f %8.4f”,a,b,c); e) printf(“%8.3f %8.3f %8.3f”,a,b,c); f) printf(“%-8f %-8f %-8f”,a,b,c); g) printf(“%+8f %+8f %+8f”,a,b,c); h) printf(“%08f %08f %08f”,a,b,c); i) printf(“%#8f %#8f %#8f”,a,b,c);
  • 15. Practice problems 2. Assume that i ,j , k are integer variables and i represents an octal quantity, j represents a decimal quantity and k represents a hexadecimal quantity. Write an appropriate printf function for each of the following statements: a) Display the values for i ,j , k with minimum field width of eight characters for each value b) Repeat part (a) with each output data item left justified within its respective field. c) Repeat part (a) with each output data item preceded by zeros(0x, in the case of the hexadecimal quantity)