Format Specifiers in C and Console IO Tutorial
Table of Contents
Learn Format Specifiers in C and Console IO MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
C Programming language provides Format Specifiers and Console IO to help programmers Read and Write data.
The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system
Format Specifiers in C and Console IO Functions
There are two types of Input Output Functions.
- Console IO
- File IO
1. Console IO functions help to read data from Keyboard and Print / Display data to the Monitor.
2. File IO functions help to read data from Files on Disk and Write data to the Disk.
Format Specifiers in C are used in IO functions to display data in a specified format so that it can be read easily and printed for reports.
Escape Sequence Characters in C
Escape sequence characters in C language are not processed like normal characters. An Escape sequence starts with a BACK SLASH ( \ ) character. An immediate character next to Back Slash is also treated part of the Escape Sequence. Examples are Escape sequences are New Line Character, Line Feed, Form Feed etc.
SNO | Escape Sequence | Meaning |
---|---|---|
1 | \n | New Line – Cursor goes to next line beginning |
2 | \r | Carriage Return – Cursor goes to same line beginning |
3 | \b | Back Space – One character is deleted backwards |
4 | \t | Horizontal Tab – Cursor moves forward to 4 or 8 spaces |
5 | \f | Form Feed – Makes the printer to go to the beginning of next page |
6 | \a | Audible Alert – Plays a sound |
7 | \’ | Single Quote – Prints a single quote |
8 | \” | Double Quote – Prints a double quote |
9 | \\ | Back Slash – Prints a back slash |
Format Specifiers in C
Example 1:
int main() { printf("TOM\nCAT"); } //OUTPUT // \n produces new line //TOM //CAT
Format Specifiers in C
A Format Specifier specifies type of data to be Read from Keyboard or Printed on Screen. Types of data available are char, short, int, long, float, double and long double.
List of format specifiers is given below.
SNO | Format Specifiers in C | Data Type |
---|---|---|
1 | %d or %l | short signed |
2 | %u | short unsigned |
3 | %ld | long signed |
4 | %lu | long unsigned |
5 | %x | unsigned hexadecimal |
6 | %o | unsigned octal |
7 | %f | float |
8 | %lf | double |
9 | %Lf | long double |
10 | %c | signed or unsigned char |
11 | %s | string |
You can also specify Field Width and Precision as part of a format specifier. Filed Width (W) represents number of COLUMNS to be reserved. Precision (P) represents Number of Digits after Decimal Point. It is used in the below format. Symbols ‘+’ or ‘-‘ or used to specify Right Alignment and Left Alignment of text respectively. Plus sign is applied by default. So there is no need to type or specify + symbol.
Precision is used along with float and double data types
Note: If less columns are occupied with data, blanks will be shown in place holders. Blanks will appear on right if the text is Left Justified. Blanks will appear on Left if the text is Right Justified.
%(+ or -)(W.P)(Type Specifier) eg. %10d Right Justification %-10d Left Justification %12.2f, %-12.2f %16s, %-16s
Note: You can truncate right side of a String or Char Array using Field Width Specifiers as part of Format Specifiers. You can printf only first N character by using %(W.N)s notation with strings.
Example 1
int main() { printf("123456 123456\n"); printf("%6d %6d\n", 123, 45678); printf("%-6d %-6d", 4891, 78); return 0; } //OUTPUT 123456 123456 123 45678 4891 78
Format Specifiers
Example 2: Truncating part of String with Field Width Specifiers
Strings or char arrays can be truncated using %(W.P)s format specifier. W is the number of columns to be reserved and P is number of characters to be shown counting from Left.
int main() { printf("123456 123456\n"); printf("%6s %6s\n", "abc", "defgi"); printf("%-6s %-6s\n", "abc", "dghi"); printf("\n123456\n"); //prints only first 2 of 6 characters printf("%6.2s\n", "abcdef"); printf("%-6.3s\n", "abcdef"); return 0; } //OUTPUT 123456 123456 abc defgi abc dghi 123456 ab abc
Format Specifiers
Example 3: formatting string with float and double
Float and Double numbers can be formatted specifying Precision or Number of Digits after decimal point to be shown. Total number of digits are represented by W and Precision digits are part of W number. So here in this example, %6.2f specifies a total of 6 digits out of which 2 digits will be used to show precision.
int main() { printf("NAME=%s\n, Score=%6.2f", "TOMMY", 97.578963); return 9; } // // 97.57 //double value is type casted to float
//97.5 is double. //double value is type casted to float
Console IO Functions in C Language
Console is a combination of Keyboard and Monitor. and Console IO functions are classified in to two types.
- Formatted Input Output Functions
- Unformatted Input Output Functions
1. Formatted Input Output Functions
Formatted IO is done in C using only two functions namely PRINTF and SCANF.and Formatted IO functions work with all types of data namely integers, real number and character data.
int main() { char name[10]; int age; printf("Enter a name: "); scanf("%s, name); printf("Enter age: "); scanf("%d", &age); printf("Name=%s, age=%d", name, age); return 9; }
There are functions like SPRINTF and SSCANF aimed at storing the formatted data to a CHAR Array or String. Functionality of a SPRINTF is similar to PRINTF. Functionality of SSCANF is similar to SCANF. In the below example, keyboard data is read into a temporary String str.
int main() { char str[20]; char name[10]; int age; sprintf(str, "Name=%s, age=%d", "JIMMY", 25); printf("%s", str); sscanf(str, "%s%d",name, &age); printf("\n%s", str); }
2. Unformatted Input Output Functions
Unformatted IO is done in C using only Four functions namely GETCHAR, PUTCHAR, GETS and PUTS. and Unformatted Input Output functions are meant only to deal with Character type data and String type data. You can not process numbers using these functions.
Note 1: GETCHAR and PUTCHAR functions work with only SINGLE Character.GETS and PUTS functions work with character array or string data
Note 2: GETS function can read a string of characters with SPACES and TABS. Scanf can not read a line of text with spaces and tabs.
Format Specifiers in C examples
Example 1: Using GETCHAR and PUTCHAR functions:
int main() { char ch; printf("Enter a character: "); //Press enter after typing a character ch = getchar(); //Now print using PUTCHAR printf("\nEntered="); putchar(ch); return 9; }
Example 2: Using GETS and PUTS functions:
int main() { char ch; printf("Enter a character: "); //Press enter after typing a character ch = getchar(); //Now print using PUTCHAR printf("\nEntered="); putchar(ch); return 9; }
[WpProQuiz 41]
Format Specifiers in C MCQ
1) What is an Escape Sequence in C language.?
A) An escape sequence is a combination of two characters starting with Back Slash always.
B) An escape sequence is usually part of a string to tell compiler to produce New Lines, New tabs, Single or Double quotes etc
C) An escape sequence is used to format the output to look in desired way.
D) All the above
Answer [=] D
Explanation:
eg. \n outputs new line. \t outputs tab i.e 4 or 8 white spaces.
2) Choose a correct statement about C Escape Sequences.
A) \n produces new line.
B) \t produces one tab space (white spaces)
C) \b produces one Backspace
D) All the above
Answer [=] D
3) Choose a correct statement about C Escape Sequences.
A) \r produces one Carriage Return. \r does not take you to the next line. It takes cursor to only the beginning of same line.
B) \f produces form feed
C) \a produces Alert Sound or Beep Sound from PC motherboard speaker
D) All the above
Answer [=] D
4) Choose a correct statement about C Escape Sequences.
A) \’ outputs one Single Quote. Right Single Quote.
B) \” outputs one Double Quote.
C) \\ produces one Visible Back Slash \.
D) All the above
Answer [=] D
5) What is a format specifier in C language.?
A) A format Specifier tells compiler to treat a variable value is predefined way.
B) Different format specifiers are used to print different type of data.
C) Format specifiers are used to write data to files in a formatted way.
D) All the above
Answer [=] D
6) Choose a valid C format specifier.?
A) %d prints integer constants
B) %u prints unsigned integer constants
C) %ld prints signed long and %lu prints unsigned long constants
D) All the above
Answer [=] D
7) Choose a correct statement about C format Specifiers.
A) %c prints unsigned or signed character constants.
B) %s prints string constants
C) %l or %L prints long constants.
D) All the above
Answer [=] D
8) Choose a correct statement about format specifiers.
A) %f prints float constants with 6 digits of precision
B) %lf prints double constants
C) %Lf prints long double constants
D) All the above
Answer [=] D
9) What does C format specifier %W.D represent.?
A) W represents total number of columns including precision digits.
B) D represents number of precision digits out of W columns.
C) Plus(+) before W.D is for Right Alignment. Minus(-) before W.D is for Left Alignment
D) All the above
Answer [=] D
10) Choose a C Formatted Input Output function below.
A) printf(), scanf()
B) sprintf(), sscanf()
C) fprintf(), fscanf()
D) All the above
Answer [=] D
11) Choose a C unformatted input output function below.
A) gets(), puts()
B) getchar(), putchar()
C) A & B
D) None of the above
Answer [=] C
12) What is the output of C program.?
int main() { int a=123; printf("*%06d*",a); return 0; }
A) *123*
B) *6123*
C) *000123*
D) *006123*
Answer [=] C
Explanation:
%6d reservers 6 columns. %Zero6d causes Remaining columns will be filled with Zero.
13) What is the output of C program.?
int main() { int a=123456; printf("*%03d*",a); return 0; }
A) *123*
B) *6123*
C) *123456*
D) *012345*
Answer [=] C
Explanation:
%3d reserves three columns. But 123456 is more than 3 columns. So width 3 will be ignored.
14) What is the output of C program.?
int main() { int a=6543; printf("*%5d,*%-5d*",a,a); return 0; }
A) *56543,*56543*
B) * 6543,*6543 *
C) *6543 ,* 6543*
D) *6543,*6543*
Answer [=] B
Explanation:
%5d reserves 5 columns. 6543 is aligned right. So One Space is filled in the first column. %-5d aligns 6543 left. So right side one Space is filled as 6543 is only a 4 digit number.
15) What is the output of C program.?
int main() { float a=654.123456f; printf("%3.3f,%3.2f",a,a); return 0; }
A) 654,654
B) 654.123456,654.123456
C) 654.123,654.12
D) 654.000,654.00
Answer [=] C
Explanation:
%3.3 reserves 3 columns. But the 654.123456 is a 9 column number. So only .3 will be treated as important to print 3 digit precision number. .2 prints number with 2 digit precision.
16) What is the output of C program.?
int main() { char str[]="123"; printf("*%4s*%-4s*",str,str); return 0; }
A) *4123*4123*
B) * 123*123 *
C) *123 * 123*
D) *123*123*
Answer [=] B
Explanation:
%4s reserves 4 columns. If the string length is less you can see spaces on the left side. %-4s also reserves 4 columns but aligns content on the right side.
17) Choose a correct statement about sprintf and sscanf functions.?
char str[20];
A) sprintf(str,”formatstring”,variables) prints output to console. sscanf(str,”formatstring”,&variables) scans keyboard input and copies to str.
B) sprintf(str,”formatstring”,variables) prints output to string str. sscanf(str,”formatstring”,&variables) scans keyboard input and prints to console
C) sprintf(str,”formatstring”,variables) prints output to string str. sscanf(str,”formatstring”,&variables) scans input from str itself and assigns data to &variables.
D) None of the above
Answer [=] C
Explanation:
Both functions SSCANF or SPRINTF avoid getting input from keyboard or displaying on Console / Screen.
18) What is the output of C program.?
int main() { char ch='A'; ch=getchar(); putchar(ch); return 0; }//input= S
A) A
B) B
C) S
D) Compiler error
Answer [=] C
Explanation:
getchar() reads one character from keyboard. putchar(ch) prints one character.
19) Choose a correct C statement.?
A) SCANF can accept all types of data from keyboard including numbers and strings
B) GETS can accept only one String of any length and any number of words. GETCHAR can accept only one character constant from keyboard.
C) SCANF can accept only single word Strings
D) All the above
Answer [=] D
20) What is the output of c program with %* operator.?
int main() { char kh; scanf("%*c %c",&kh); putchar(kh); return 0; }//input=G H
A) G
B) H
C) A
D) Compiler error
Answer [=] B
Explanation:
%* ignores the entered argument. So here G is ignored. 2nd argument, H is accepted by variable kh. Remember, You have to enter all arguments corresponding to each % symbol.