SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
MANAGING INPUT AND OUTPUT OPERATIONS
REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY
MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
The operations which took place in order to take data and make
the display of the processed information are known as input &
output operations.
In C language, in general, user use scanf and printf functions for
input and output purpose respectively. The proper uses of these
functions are termed management of input & output operation.
There are also many functions which are used in C programming.
There exists several functions in ‘C’ language that can carry out
input output operations. These functions are collectively known
as standard Input/Output Library. Each program that uses
standard input / out put function must contain the statement.
Reading a character:
The basic operation done in input output is to read a characters from the
standard input device such as the keyboard and to output or writing it to
the output unit usually the screen.
The getchar function can be used to read a character from the standard
input device. This can also be done with the help of the scanf function.
The getchar has the following form.
variable name = getchar( );
variable name is a valid ‘C’ variable, that has been declared already and
that possess the type char.
# include < stdio.h >
void main ( )
{
char ch;
printf (“Type one character:”) ;
ch = getchar() ;
printf (” The character you typed is = %c”, ch) ;
}
Writing a character:-
The putchar function which in analogus to getchar function can be
used for writing characters one at a time to the output terminal.
The general form is
putchar (variable name);
Where variable is a valid C type variable that has already been declared
Ex:- putchar(ch);
Displays the value stored in variable C to the standard screen.
#include < stdio.h >
void main ( )
{
char in;
printf (” please enter one character”);
in = getchar ( ) ;
putchar (in);
}
Character test functions
C supports some character test functions that are contained in the file ctype.h and therefore the
statement. #include<ctype.h> must be included in the program.
Function Test
isalnum(c) is c an alphanumeric character
isalpha(c) is c an alphabetic character
isdigit(c) is c a digit
islower(c) is c a lowercase letter
isprint(c) is c a printable character
isupper(c) Is c an uppercase letter
ispunct(c) Is c a punctuation mark
isspace(c) Is c a white space character
Formatted Input:
The formatted input refers to input data that has been arranged in a
particular format. Input values are generally taken by using the scanf
function.
The scanf function has the general form:-
scanf (“control string”, arg1, arg2, arg3 



.argn);
The format field is specified by the control string and the arguments arg1,
arg2, 
...,argn specifies the address of location where address is to be
stored.
The control string specifies the field format which includes format
specifications and optional number specifying field width and the
conversion character % and also blanks, tabs and newlines.
The Blanks tabs and newlines are ignored by compiler. The
conversion character % is followed by the type of data that is to be assigned
to variable of the assignment. The field width specifier is optional.
Inputting Integer Numbers:-
The general format for reading a integer number is
% wd
Here percent sign (%) denotes that a specifier for conversion follows
and
w is an integer number which specifies the width of the field of the number
that is being read. The data type character d indicates that the number should
be read in integer mode.
Example : scanf (“%3d %4d”, &sum1, &sum2);
If the values input are 175 and 1342 here value 175 is assigned to sum1
and 1342 to sum 2.
Suppose the input data was follows 1342 and 175.
The number 134 will be assigned to sum1 and sum2 has the value 2
because of %3d the number 1342 will be cut to 134 and the remaining part is
assigned to second variable sum2.
If floating point numbers are assigned then the decimal or fractional part is
skipped by the computer.
To read the long integer data type we can use conversion specifier % ld & % hd
for short integer.
Inputting real numbers:
Unlike integer numbers, field specifications are not to be used
while representing a real number therefore real numbers are specified
in a straight forward manner using % f specifier.
The general format of specifying a real number input is
scanf (“% f “, &variable);
Example: scanf (“%f %f % f”, &a, &b, &c);
With the input data 321.76, 4.321, 678. The values 321.76 is assigned
to a , 4.321 to b & 678 to C.
If the number input is a double data type then the format specifier
should be % lf instead of %f.
Inputting character strings:
Single character or strings can be input by using the character
specifiers.
The general format is % wc or %ws
Where c and s represents character and string respectively and w
represents the field width.
The address operator need not be specified while we input
strings.
The specification %s terminates reading at the encounter of a
blank space.
Example : scanf (“%c %15s”, &ch, name):
Some versions of scanf support the following conversion specifications
for strings:-
%[characters]
%[^characters]
The specification %[characters] means that only the characters
specified within the brackets are permissible in the output string. If
the input string contains any other character, the string will be
terminated at the first encounter of such a character.
The specification %[^characters] does not permit the characters
specified after the circumflex(^) in the input string.
Reading mixed Data types:-
scanf statement can also be used to input a data line
containing mixed mode data.
For example:
scanf(“%d %c %f %s”, &count, &code, &ratio, name};
Formatted Output:-
Printf function is used to produce the outputs in such a way that they are
understandable and are in an easy to use form.
The general form of printf statement is
printf(“control string”, &arg1,&arg2,


.argn);
Control string consists of 3 types of items:
‱Characters that will be printed on the screen as they appear.
‱Format specifications that define the output format for display of each item.
‱Escape sequence characters such as n, t and b.
The arguments arg1, arg2,

argn are the variables whose values are
formatted and printed according to the specifications of the control string.
A simple format specification has the following form:
% w.p type-specifier
Where w is an integer number that specifies the total
number of columns for the output value and
p is another integer number that specifies the number of digits
to the right of the decimal point or the number of characters to
be printed from a string.
A newline can be introduced by the help of a newline character.
For example:- printf (“Enter 2 numbers”);
For example
#include < stdio.h >
main ( )
{
printf (“Hello!”);
printf (“Welcome to the world of Engineering!”);
}
Output:
Hello! Welcome to the world of Engineering.
For Example :
printf (“Hello!n”);
OR
printf (“n Welcome to the world of Engineering”);
Output of integer numbers:-
The format specification for printing an integer number is
%wd
Where
w specifies the minimum field width for the output.
d specifies that the value to be printed is an integer.
For example:
printf(“%d”,9876);
printf(“%6d”, 9876)
printf(“%2d”, 9876);
printf(“%-6d”, 9876);
printf(“%06d”, 9876);
It is possible to force the printing to be left-justified by placing a
minus sign directly after the % character.
It is also possible to place zeroes before the field width as
shown in the last example.
Output of Real Numbers:-
The output of a real number may be displayed in decimal notation using
the following format specification:
% w.pf
The integer w indicates the minimum number of positions that are to
be used for the display of the value and the integer p indicates the number
of digits to be displayed after the decimal point.
The value, when displayed, is rounded to p decimal places and printed right-
justified in the field of w columns. Leading blanks and trailing zeros will
appear as necessary. The default precision is 6 decimal places. The negative
numbers will be printed with the minus sign.
y=98.7654
printf(“%7.2f”, y);
printf(“%-7.2f”, y);
printf(“%f”, y);
printf(“%10.2e”,y);
printf(“%11.4e”,-y);
printf(“%-10.2e”,y);
printf(“%e”,y);
printf(“%7.4f”, y)
Printing a single character:-
A single character can be displayed in a desired position using
the format:
%wc
The character will be displayed right-justified in the field of w
columns. We can make the display left-justified by placing a minus
sign before the integer w.
The default value for w is 1.
Printing of Strings:-
The format specification for outputting strings is similar to
that of real numbers that is
%w.ps
Where w specifies the field width for display and p instructs that
only the first p characters of the string are to be displayed. The
display is right-justified.
For example: Printing the string “NEW DELHI 110001”.
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
6 7 8 9 0
%s
%20s
%20.10s
%.5s
%-20.10s
%5s
Mixed Data Output:-
It is permitted to mix data types in one printf statement.
For example:-
printf(%d %f %s %c”, a, b, c, d);
printf uses its control string to decide how many variables
to be printed and what their types are. Therefore, the
format specifications should match the variables in
number, order, and type.
Specifier Meaning
%c – Print a character
%d – Print a Integer
%i – Print a Integer
%e – Print float value in exponential form.
%f – Print float value
%g – Print using %e or %f whichever is smaller
%o – Print actual value
%s – Print a string
%x – Print a hexadecimal integer (Unsigned) using lower case a – F
%X – Print a hexadecimal integer (Unsigned) using upper case A – F
%a – Print a unsigned integer.
%p – Print a pointer value
%hx – hex short
%lo – octal long
%ld – long

Weitere Àhnliche Inhalte

Was ist angesagt?

Inline function
Inline functionInline function
Inline function
Tech_MX
 
Type conversion
Type conversionType conversion
Type conversion
Frijo Francis
 
Typecasting in c
Typecasting in cTypecasting in c
Typecasting in c
Tushar Shende
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 

Was ist angesagt? (20)

Inline function
Inline functionInline function
Inline function
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Type conversion
Type conversionType conversion
Type conversion
 
Functions in c
Functions in cFunctions in c
Functions in c
 
File in C language
File in C languageFile in C language
File in C language
 
C tokens
C tokensC tokens
C tokens
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
C functions
C functionsC functions
C functions
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
C++ string
C++ stringC++ string
C++ string
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
Typecasting in c
Typecasting in cTypecasting in c
Typecasting in c
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
C program
C programC program
C program
 

Ähnlich wie MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf

Input And Output
 Input And Output Input And Output
Input And Output
Ghaffar Khan
 
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 basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
 
T03 b basicioscanf
T03 b basicioscanfT03 b basicioscanf
T03 b basicioscanf
teach4uin
 

Ähnlich wie MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf (20)

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
 
Input And Output
 Input And Output Input And Output
Input And Output
 
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
 
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
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
input
inputinput
input
 
string , pointer
string , pointerstring , pointer
string , pointer
 
Unit 2- Module 2.pptx
Unit 2- Module 2.pptxUnit 2- Module 2.pptx
Unit 2- Module 2.pptx
 
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’
 
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’
 
C tutoria input outputl
C tutoria input outputlC tutoria input outputl
C tutoria input outputl
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
Chapter3
Chapter3Chapter3
Chapter3
 
Functions of stdio conio
Functions of stdio   conio Functions of stdio   conio
Functions of stdio conio
 
Chap 2 input output dti2143
Chap 2  input output dti2143Chap 2  input output dti2143
Chap 2 input output dti2143
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
C programming language for beginners
C programming language for beginners C programming language for beginners
C programming language for beginners
 
T03 b basicioscanf
T03 b basicioscanfT03 b basicioscanf
T03 b basicioscanf
 

Mehr von SowmyaJyothi3 (6)

WEBMINING_SOWMYAJYOTHI.pdf
WEBMINING_SOWMYAJYOTHI.pdfWEBMINING_SOWMYAJYOTHI.pdf
WEBMINING_SOWMYAJYOTHI.pdf
 
NEURALNETWORKS_DM_SOWMYAJYOTHI.pdf
NEURALNETWORKS_DM_SOWMYAJYOTHI.pdfNEURALNETWORKS_DM_SOWMYAJYOTHI.pdf
NEURALNETWORKS_DM_SOWMYAJYOTHI.pdf
 
CLUSTERING IN DATA MINING.pdf
CLUSTERING IN DATA MINING.pdfCLUSTERING IN DATA MINING.pdf
CLUSTERING IN DATA MINING.pdf
 
Association Rule.ppt
Association Rule.pptAssociation Rule.ppt
Association Rule.ppt
 
Association Rule.ppt
Association Rule.pptAssociation Rule.ppt
Association Rule.ppt
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 

KĂŒrzlich hochgeladen

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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

KĂŒrzlich hochgeladen (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
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
 
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
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
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
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
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
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
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)
 

MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf

  • 1. MANAGING INPUT AND OUTPUT OPERATIONS REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
  • 2. The operations which took place in order to take data and make the display of the processed information are known as input & output operations. In C language, in general, user use scanf and printf functions for input and output purpose respectively. The proper uses of these functions are termed management of input & output operation. There are also many functions which are used in C programming. There exists several functions in ‘C’ language that can carry out input output operations. These functions are collectively known as standard Input/Output Library. Each program that uses standard input / out put function must contain the statement.
  • 3. Reading a character: The basic operation done in input output is to read a characters from the standard input device such as the keyboard and to output or writing it to the output unit usually the screen. The getchar function can be used to read a character from the standard input device. This can also be done with the help of the scanf function. The getchar has the following form. variable name = getchar( ); variable name is a valid ‘C’ variable, that has been declared already and that possess the type char.
  • 4. # include < stdio.h > void main ( ) { char ch; printf (“Type one character:”) ; ch = getchar() ; printf (” The character you typed is = %c”, ch) ; }
  • 5. Writing a character:- The putchar function which in analogus to getchar function can be used for writing characters one at a time to the output terminal. The general form is putchar (variable name); Where variable is a valid C type variable that has already been declared Ex:- putchar(ch); Displays the value stored in variable C to the standard screen.
  • 6. #include < stdio.h > void main ( ) { char in; printf (” please enter one character”); in = getchar ( ) ; putchar (in); }
  • 7. Character test functions C supports some character test functions that are contained in the file ctype.h and therefore the statement. #include<ctype.h> must be included in the program. Function Test isalnum(c) is c an alphanumeric character isalpha(c) is c an alphabetic character isdigit(c) is c a digit islower(c) is c a lowercase letter isprint(c) is c a printable character isupper(c) Is c an uppercase letter ispunct(c) Is c a punctuation mark isspace(c) Is c a white space character
  • 8. Formatted Input: The formatted input refers to input data that has been arranged in a particular format. Input values are generally taken by using the scanf function. The scanf function has the general form:- scanf (“control string”, arg1, arg2, arg3 



.argn); The format field is specified by the control string and the arguments arg1, arg2, 
...,argn specifies the address of location where address is to be stored. The control string specifies the field format which includes format specifications and optional number specifying field width and the conversion character % and also blanks, tabs and newlines. The Blanks tabs and newlines are ignored by compiler. The conversion character % is followed by the type of data that is to be assigned to variable of the assignment. The field width specifier is optional.
  • 9. Inputting Integer Numbers:- The general format for reading a integer number is % wd Here percent sign (%) denotes that a specifier for conversion follows and w is an integer number which specifies the width of the field of the number that is being read. The data type character d indicates that the number should be read in integer mode.
  • 10. Example : scanf (“%3d %4d”, &sum1, &sum2); If the values input are 175 and 1342 here value 175 is assigned to sum1 and 1342 to sum 2. Suppose the input data was follows 1342 and 175. The number 134 will be assigned to sum1 and sum2 has the value 2 because of %3d the number 1342 will be cut to 134 and the remaining part is assigned to second variable sum2. If floating point numbers are assigned then the decimal or fractional part is skipped by the computer. To read the long integer data type we can use conversion specifier % ld & % hd for short integer.
  • 11. Inputting real numbers: Unlike integer numbers, field specifications are not to be used while representing a real number therefore real numbers are specified in a straight forward manner using % f specifier. The general format of specifying a real number input is scanf (“% f “, &variable); Example: scanf (“%f %f % f”, &a, &b, &c); With the input data 321.76, 4.321, 678. The values 321.76 is assigned to a , 4.321 to b & 678 to C. If the number input is a double data type then the format specifier should be % lf instead of %f.
  • 12. Inputting character strings: Single character or strings can be input by using the character specifiers. The general format is % wc or %ws Where c and s represents character and string respectively and w represents the field width. The address operator need not be specified while we input strings. The specification %s terminates reading at the encounter of a blank space.
  • 13. Example : scanf (“%c %15s”, &ch, name): Some versions of scanf support the following conversion specifications for strings:- %[characters] %[^characters] The specification %[characters] means that only the characters specified within the brackets are permissible in the output string. If the input string contains any other character, the string will be terminated at the first encounter of such a character. The specification %[^characters] does not permit the characters specified after the circumflex(^) in the input string.
  • 14. Reading mixed Data types:- scanf statement can also be used to input a data line containing mixed mode data. For example: scanf(“%d %c %f %s”, &count, &code, &ratio, name};
  • 15. Formatted Output:- Printf function is used to produce the outputs in such a way that they are understandable and are in an easy to use form. The general form of printf statement is printf(“control string”, &arg1,&arg2,


.argn); Control string consists of 3 types of items: ‱Characters that will be printed on the screen as they appear. ‱Format specifications that define the output format for display of each item. ‱Escape sequence characters such as n, t and b. The arguments arg1, arg2,

argn are the variables whose values are formatted and printed according to the specifications of the control string.
  • 16. A simple format specification has the following form: % w.p type-specifier Where w is an integer number that specifies the total number of columns for the output value and p is another integer number that specifies the number of digits to the right of the decimal point or the number of characters to be printed from a string. A newline can be introduced by the help of a newline character. For example:- printf (“Enter 2 numbers”);
  • 17. For example #include < stdio.h > main ( ) { printf (“Hello!”); printf (“Welcome to the world of Engineering!”); } Output: Hello! Welcome to the world of Engineering. For Example : printf (“Hello!n”); OR printf (“n Welcome to the world of Engineering”);
  • 18. Output of integer numbers:- The format specification for printing an integer number is %wd Where w specifies the minimum field width for the output. d specifies that the value to be printed is an integer.
  • 19. For example: printf(“%d”,9876); printf(“%6d”, 9876) printf(“%2d”, 9876); printf(“%-6d”, 9876); printf(“%06d”, 9876); It is possible to force the printing to be left-justified by placing a minus sign directly after the % character. It is also possible to place zeroes before the field width as shown in the last example.
  • 20. Output of Real Numbers:- The output of a real number may be displayed in decimal notation using the following format specification: % w.pf The integer w indicates the minimum number of positions that are to be used for the display of the value and the integer p indicates the number of digits to be displayed after the decimal point. The value, when displayed, is rounded to p decimal places and printed right- justified in the field of w columns. Leading blanks and trailing zeros will appear as necessary. The default precision is 6 decimal places. The negative numbers will be printed with the minus sign. y=98.7654
  • 21. printf(“%7.2f”, y); printf(“%-7.2f”, y); printf(“%f”, y); printf(“%10.2e”,y); printf(“%11.4e”,-y); printf(“%-10.2e”,y); printf(“%e”,y); printf(“%7.4f”, y)
  • 22. Printing a single character:- A single character can be displayed in a desired position using the format: %wc The character will be displayed right-justified in the field of w columns. We can make the display left-justified by placing a minus sign before the integer w. The default value for w is 1.
  • 23. Printing of Strings:- The format specification for outputting strings is similar to that of real numbers that is %w.ps Where w specifies the field width for display and p instructs that only the first p characters of the string are to be displayed. The display is right-justified. For example: Printing the string “NEW DELHI 110001”. 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  • 25. Mixed Data Output:- It is permitted to mix data types in one printf statement. For example:- printf(%d %f %s %c”, a, b, c, d); printf uses its control string to decide how many variables to be printed and what their types are. Therefore, the format specifications should match the variables in number, order, and type.
  • 26. Specifier Meaning %c – Print a character %d – Print a Integer %i – Print a Integer %e – Print float value in exponential form. %f – Print float value %g – Print using %e or %f whichever is smaller %o – Print actual value %s – Print a string %x – Print a hexadecimal integer (Unsigned) using lower case a – F %X – Print a hexadecimal integer (Unsigned) using upper case A – F %a – Print a unsigned integer. %p – Print a pointer value %hx – hex short %lo – octal long %ld – long