Formatted Input/Output Statements
Formatted input output functions
printf( )
scanf( )
Escape sequences are also used to provide formatted output with printf()
Unformatted I/O Functions
Input
-To read a character
getch()
getche()
getchar()
-To read the string
gets()
Output
-To write a character
putch()
putchar()
-To write the string
puts()
getch and getche are from conio.h
getchar is from stdio.h
Formatted input output functions
printf( )
scanf( )
All input/output operations are carried out through function calls such as printf and scanf.
There are several functions that have input/output operation standards these are known as the standard input/output library.
Each program that uses a standard input/output function must contain the statement
#include <stdio.h>
Formatted Input
Scanf() is used to take formatted input in c programming. General form of scanf is
Scanf(“control string”,arg1,arg2,…argn);
The control string specifies the string format in which data is to be entered and the arguments arg1,arg2 … specifies the address of location.
Control stfing may contain field specification with % to ensure data type and and blank, tabs, newline
Blanks, tabs and newlines must separate the values supplied to scanf()
Example: scanf(“%d %d”,&num1,&num2);
Will read data 30 50 and assign it to num1=30 and num2-50
Here % sign indicates that a conversion specification follows and d known as datatype character indicates that number to be read is in integer mode. And & before the variable name is must as it is ‘address of’ operator. It gives the location number used by the variable in memory.
Commonly used scanf formats
Format specifiers
Format specifiers
Code
|
Meaning
|
%c
|
Read a single character
|
%d
|
Read a decimal integer
|
%e
|
Read a floating point value
|
%f
|
Read a floating point value
|
%g
|
Read a floating point value
|
%h
|
Read a short integer
|
%i
|
Read a decimal, hexadecimal or octal integer
|
%o
|
Read an octal integer
|
%s
|
Read a string (Cannot read blank space in string)
|
%u
|
Read an unsigned decimal integer
|
%x
|
Read a hexadecimal integer
|
%[..]
|
Read a string of word(s)
|
The input will be terminated when scanf encounters invalid mismatch of data. The data item must be separated by spaces and must match the variables receiving the input in same order.
Formatted Output
The printf() statement is used for printing results and captions. The general form of printf statement is
Printf(“control string”,arg1,arg2,…argn);
Where control string consist of three things:
1. Character
2. Format specification such as %d %c
3. Escape sequence such as \n \t
Examples: printf(“Programming in c”);
Printf(“sum=%d”,1234);
Commonly used printf formats
Code
|
Meaning
|
%c
|
print a single character
|
%d
|
Print a decimal integer
|
%e
|
Print a floating point value in exponent form
|
%f
|
Print a floating point value without exponent
|
%g
|
Print a floating point value eithrt e-type or f-type depending on value
|
%i
|
Print a single decimal integer
|
%o
|
Print a octal integer, without leading zero
|
%s
|
Print a string
|
%u
|
Print a unsigned decimal integer
|
%x
|
Print a hexadecimal integer, without leading 0x
|
Escape sequences are also used to provide formatted output with printf()
‘\a’ audible alert
‘\b’ back space
‘\f’ form feed
‘\n’ new line
‘\r’ carriage return
‘\t’ horizontal tab
‘\0’ null
‘\”’ double quote
‘\’’ single quote
Unformatted I/O Functions
Input
-To read a character
getch()
getche()
getchar()
-To read the string
gets()
Output
-To write a character
putch()
putchar()
-To write the string
puts()
getchar() reading a single character
For example
char name;
name=getchar();
will assign character to the variable name
getch() return the character that is typed
getche() return and display the character that is typed
getchar() also displays the charcter but it requires enter key
getch and getche are from conio.h
getchar is from stdio.h
putchar() or putch() is used for writing a character to the terminal.
char ans=’y’;
putchar(ans)
will display y to the screen.
gets() is used to insert string from the keyboard
char fname[50];
gets(fname);
gets() accept string from the keyboard and it stop when enter key is hit. gets() can accept space and tabs of the string while scanf() can not accept it.
gets can read only one string at a time while scanf() can read more then one.
puts() is opposite of string function. It display the string on the screen
puts(fname);
puts() can output only one string at a time while printf() can print multiple string.
0 comments:
Post a Comment