Unit 2 - I Input and Output
Unit 2 - I Input and Output
C PROGRAMMING
UNIT 2
2
I/O in C
1. Unformatted I/O (getchar(),putchar()..)
2. Formatted I/O (printf(), scanf() )
Reading a character : getchar() 3
❑ General Syntax
variable_name = getchar();
❑ E.g.
char c;
c = getchar();
Reading a character(prg301) 4
Program: prg301
#include<stdio.h>
int main()
{
char c;
printf("Enter a character:");
c = getchar();
printf("Character %c is entered",c);
return 0;
}
Functions related to character data 5
❑ Function: putchar(variable_name);
#include<stdio.h>
void main()
{
char c;
printf("Enter a character:");
c = getchar();
printf("Character entered is:");
putchar(c);
}
Changing character case (prg304) 8
❑ Changing case of character entered.(Upper & lower case)
Program: prg304
void main()
{
char c;
printf("Enter a character:");
c = getchar();
if(islower(c))
putchar(toupper(c));
else
putchar(tolower(c));
}
Formatted Input 9
❑ General format:
scanf("control string",arg1,arg2,…,argn);
❑ Control string(format string): specifies order in
which data to be entered and the type of the data
expected.
❑ arg1,arg2,…,argn : variables where data is stored.
❑ E.g: scanf("%d %c %f", &num,&ch,&fno);
Formatted Input: Integers 11
Inputting Integers
❑ General format of control string: %wd
❑ w ➔ Specifies width of the data to read
❑ d ➔ Indicated we want to read an integer.
❑ E.g. scanf("%5d", &num1)
❑ Reads five digits of user entered number
void main()
{
int a;
printf("Enter 6 digit number:");
scanf("%5d", &a);
printf("The number is: %d",a);
}
Output:
Enter 6 digit number:678123
The number is: 67812
Formatted Input : Integers 13
Common usage:
float a,b,c;
scanf("%f %f %f",&a,&b,&c);
❑ For double data type, control string is "%lf"
Formatted Input: Real numbers 15
Inputting float
❑ General format of control string: %wf for float, %wlf for
double
❑ w ➔ Specifies width of the data to read incl. decimal point.
❑ f ➔ Indicated we want to read a float.
❑ E.g. scanf("%5f", &num1)
❑ Reads first five characters of user entered number incl. decimal point
int a;
char b;
float c;
scanf("%d %c %f",&a, &b,&c);
Formatted Input(prg306) 18
Examples:
printf("hello world"); 🡺 hello world
printf(" "); 🡺 //prints a blank space
printf("\n"); 🡺 //prints a new line
printf("hello\nworld"); 🡺 hello
world
printf("a=%d \t b=%d",10,5); 🡺 a=10 b=5
(tab inserted)
Formatted Output 21
Outputting Integers:
❑ General format specification: %wd
❑ w ⇒ Specify minimum field width for the output.
❑ That much(w) space is reserved on screen for the data
displayed.
Output of Integer Numbers 22
❑ Examples:
output
1. printf("%d",9876); 9 8 7 6
2. printf("%6d",9876); 9 8 7 6
3.printf("%2d",9876); 9 8 7 6
4.printf("%-6d",9876); 9 8 7 6
5. printf("%06d",9876); 0 0 9 8 7 6
2.printf("%7.2f",y); 9 8 . 7 7
3.printf("%-7.2f",y); 9 8 . 7 7
4.printf("%.4f",y); 9 8 . 7 6 5 4
5.printf("%10.2e",y); 9 . 8 8 e + 0 0 1
6.printf("%11.4e",-y); - 9 . 8 7 6 5 e + 0 1
Output of single character 25
❑ Examples:
Output
char ch = ‘H’; H
printf("%c", ch ); H
printf("%6c",ch); H
printf("%-4c",ch);
Output of strings 27
printf("%15.5s",str); H E L L O
printf("%.5s",str); H E L L O
printf("%-15.5s",str); H E L L O
Mixed Data Output 28
printf("\nOUTPUT RESULTS\n");
printf("==============\n");
printf("NAME \t AGE \t ID\n");
printf("Ram \t 35 \t 10434\n");
printf("Mohan \t 36 \t 104\n");
printf("Hari \t 25 \t 12\n");