Input and Output Functions:-
1.Formatted Function:
- printf()
- scanf()
2.Unformatted Function:
- getch()
- putch()
- getche()
- putchar()
- getchar()
- puts()
- gets()
Formatted Functions:
• It read and write all types of data values.
• Require format string to produce formatted result.
• Returns value after execution.
Unformatted Functions:
• Works only with character data type.
• Do not require format conversion for formatting data type.
• C has three types of I/O functions:
- Character I/O
- String I/O
- File I/O
printf() function:
- This function displays output with specified format.
- It requires format conversion symbol or format string and variables names to the print the data.
- The list of variables are specified in the printf() statement.
- The values of the variables are printed as the sequence mentioned in printf().
- The format string symbol and variable name should be the same in number and type.
eg:-
1.
void main()
{
int NumInt = 2;
printf(“%d ”, NumInt; )
}
Output : 2
2.
void main()
{
float NumFloat=2.2;
printf(“%f ”, Numfloat; )
}
Output : 2.2000
3.
void main()
{
char LetterCh = ‘C’
printf(“%c ”, Letterch; )
}
Output : C
4.
void main()
{
int NumInt = 65;
clrscr();
printf(“%c %d”, NumInt, NumInt);
}
Output : A 65
5.
void main()
{
int NumInt = 7;
clrscr();
printf(“%f”, NumInt);
}
Output : Error Message : “Floating points formats not linked”
scanf() function:
- scanf() function reads all the types of data values.
- It is used for runtime assignment of variables.
- The scanf() statement also requires conversion symbol to identify the data to be read during the execution of the program.
- The scanf() stops functioning when some input entered does not match format string.
syntax:
- scanf(“%d %f %c”, &a, &b, &c);
- Scanf() statement requires ‘&’ operator called address operator.
- The address operator prints the memory location of the variable.
- scanf() statement the role of ‘&’ operator is to indicate the memory location of the variable, so that the value read would be placed at that location.
- The scanf() function statement also return values. The return value is exactly equal to the number of values correctly read.
- If the read value is convertible to the given format, conversion is made.
eg:- 1.
void main()
{
int a;
clrscr();
printf(“Enter value of ‘A’ : “);
scanf(“%c”, &a);
printf(“A : %c”,a);
}
OUTPUT Enter value of ‘A’ : 8 A : 8
2.
void main()
{
char a;
clrscr();
printf(“Enter value of ‘A’ : “);
scanf(“%d”, &a);
printf(“A : %d”,a);
}
OUTPUT Enter value of ‘A’ : 255 A : 255 Enter value of ‘A’ : 256 A : 256
DATA TYPES FORMAT STRINGS
Intiger:
- Short Integer %d or %i
- Short unsigned %u
- Long signed %ld
- Long unsigned %lu
- Unsigned hexadecima %u
- Unsigned octal %o
Real:
- Floating %f or %g
- Double Floating %lf
Character:
- Signed character %c
- Unsigned character %c
- String %s
- Octal number %o
- Displays Hexa decimal number
- in lowercase %hx
- Displays Hexa decimal number
- in uppercase %p
- Aborts program with error %n
Escape Sequence:
- printf() and scanf() statement follows the combination ofcharacters called escape sequence.
- Escape sequence are special characters starting with ‘\’.
Escape sequence use
- \n New Line
- \b Backspace
- \f Form feed
- \' Single quote
- \\ Backslash
- \o Null
- \t Horizontal tab
- \r Carriage return
- \a Alert
- \" Double quote
- \v Variable tab
- \? Question mark
void main()
{
int a = 1, b = a + 1, c = b + 1, d = c + 1;
clrscr();
printf(“\t A = %d\nB = %d \’C = %d\’”,a,b,c);
printf(“\n\b***\D = %d**”,d);
printf(“\n*************”);
printf(“\rA = %d B = %d”, a, b);
}
OUTPUT A = 1
B = 2
‘C = 3’ ***
D=4** ****************
A = 1
B = 2******
getchar():
- This function reads a character type data from standard input. It reads one character at a time till the user presses the enter key.
- Syntax VariableName = getchar();
- Example char c; c = getchar();
putchar:
- This function prints one character on the screen at a time, read by the standard input.
- Syntax – putchar(variableName)
- Example char c = ‘C’; putchar(c);
getch() and getche():
- These functions read any alphanumeric character from the standard input device.
- The character entered is not displayed by the getch() function.
- The character entered is displayed by the getche() function.
- Exampe:- ch = getch();
- ch = getche();
gets():
- This function is used for accepting any string through stdin keyword until enter key is pressed.
- The header file stdio.h is needed for implementing the above function.
- Syntax
- char str[length of string in number];
- gets(str);
void main()
{
char ch[30];
clrscr();
printf(“Enter the string : “);
gets(ch);
printf(“\n Entered string : %s”, ch);
}
puts():
- This function prints the string or character array.
- It is opposite to gets()
- char str[length of string in number];
- gets(str);
- puts(str);
0 Comments