Unit 2 - I Input and Output v1.5
Unit 2 - I Input and Output v1.5
Table of Contents
Input/Output(I/O) Functions 3
Unformatted I/O 3
1. Reading a character 3
2. Writing a character 4
Formatted I/O 5
3. Formatted Input 5
3.1 Inputting Integers 5
3.2 Inputting real number: float and double types 6
3.3 Formatted Input: Strings 6
3.4 Reading Mixed Data Types 6
4. Formatted Output 7
4.1 Outputting Integers: 8
4.2 Output of Real Numbers 8
4.3 Output of single character 9
4.4 Output of strings 9
Input/Output(I/O) Functions
I/O functions in C are categorized as:
1. Unformatted I/O functions
E.g. getchar(), putchar()
2. Formatted I/O Functions
E.g. scanf(), printf()
Unformatted I/O
1. Reading a character
This means taking character input from the user.
❑ General Syntax:
variable_name = getchar();
❑ E.g.
char c;
c = getchar();
#include<stdio.h>
#include<ctype.h>
int main()
{
char c;
printf("Enter a character:");
c = getchar();
if(isalpha(c)!=0)
printf("Entered character is a letter.\n");
else if(isdigit(c)!=0)
printf("Entered character is a digit.\n");
else
printf("Entered character is not alpha-numeric\n");
return 0;
}
Output:
Enter a character:A
Entered character is a letter.
Enter a character:9
Entered character is a digit.
Enter a character:#
Entered character is not alpha-numeric
Enter a character:a
Output: A
Formatted I/O
3. Formatted Input
● Formatted Input refers to input data expected/inputted in predefined format in the program.
E.g.: Entered data: 15.75 123 John
● This data is read to corresponding type variables.
● Function used for formatted Input: scanf
● 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);
Output:
Enter 6 digit number:678123
The number is: 67812
❑ Formatted Input
Skipping a number from reading
❑ E.g. scanf("%d %*d %d", &a, &b) ;
❑ If user entered: 10 50 100
❑ Data read as: a <==10, b<==100
❑ 50 entered by the user is skipped.
❑ Formatted Input
char str[10];
scanf("%s",str); // Read till first space or \n found
scanf("%5s",str); // Read first 5 characters
scanf("%[^\n]",str); // Read until \n is found. May
//not work well with all
//compilers.
printf("Inputs = %d\n",d);
if(d!=3)
printf("Input Error");
else
printf("No Input Error");
return 0;
}
Output:
56 trtrtt
Inputs = 2
Input Error
Examples:
printf("hello world"); ==> hello world
printf(“ "); ==>prints a blank space
printf("\n"); ==>prints a new line
printf("hello\nworld"); ==>hello
world
1. printf("%d",9876);
2. printf("%6d",9876);
3. printf("%2d",9876);
4. printf("%-6d",9876);
5. printf("%06d",9876);
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");
Sample Questions:
1. Differentiate between formatted input and output in C.
2. What are the formatted input/output functions in C. Explain with examples.
3. What are the unformatted input/output functions in C. Explain with examples.
4. Predict the output of following statements:
char str = "India is my country";
printf("%s",str);
printf("%13s",str);
printf("%14.5s",str);
printf("%.6s",str);
printf("%-15.5s",str)