You are on page 1of 14

Input and Output Statements

I/O function categorization


• Console
• Disk
• Port
Formatted I/O Functions
• Printf() function
• Scanf() function
Printf()
• To display data on the VDU
• Header file – stdio.h
• Syntax
printf(format-string[,arguments]);
• Format string – literal text, escape sequence and conversion specifier
• Example:
printf(“a=%d\nb=%d\n”,a,b);
Escape sequence
• Sequence of characters that represent something else escaping from their original representation
• ‘\’ followed by single or more characters
• During compilation, sequence of characters is converted into a single escaped character in the
object code

• \n- newline
• \t – horizontal tab
• \\ - backslash
• \0 – null character
• \? – question mark
• \’ – single quotation mark
• \” – double quotation mark
• \b – backspace
Conversion specifier
• To display the content of variables in a specified data type format
• Consists of % symbol followed by one or more single character

• %c – character
• %d – integer
• %e – float in exponential form
• %f – float value
• %u- unsigned integer
Scanf()
• Take input from the keyboard
• Header file – stdio.h
• Syntax
scanf(format-string[,arguments,..]);
• Example:
int x;
float y
scanf(“%d%f,&x, &y);
Unformatted I/O functions
• Types:
• Character I/O functions
• String I/O functions
• Character I/O functions:
• getch()
• getche()
• getchar()
• putch()
• putchar()
getch()
• Conio.h
• Reads single character from keyboard and stores in the respective
variable
• Need not press enter key
• Character typed is not displayed in the VDU
• Example:
ch= getch();
getche()
• Conio.h
• Single character read directly from keyboard
• Need not press enter key
• It displays the typed character on the VDU

• ch= getche();
getchar()
• Stdio.h
• Buffered function
• Input from keyboard sent to stdin(input buffer) – temporary
• Once enter key is pressed value is stored into the variable

• ch=getchar();
putch()
• Conio.h
• Displays single character directly on the VDU

putch(ch);
putchar()
• Stdio.h
• Writes the character to the output buffer and displays on the screen

putchar(ch);
Structure of a program
Documentation Block

Pre-processor Directive Block

Global Declaration Block

Main() function Block


{
Local declaration block
Executable code block
}

Sub- function block

You might also like