Input Output - printf() and scanf() functions
printf() and scanf() Functions in C
printf() and scanf() are inbuilt library functions in the C programming language.
These functions are available in the C standard library by default.
They are declared in the header file:
To use these functions in a C program, include the header file:
printf() Function in C
In C programming, the printf() function is used to display output on the screen.
It can print:
-
Characters
-
Strings
-
Integers
-
Floating-point numbers
-
Octal values
-
Hexadecimal values
Common printf Format Specifiers
Controlling Integer Width with printf
Minimum Width
-
Specifies a minimum width of 3 spaces
-
Output is right-justified by default
Left-Justified Output
-
Add a minus sign
-after% -
Output becomes left-justified
Zero-Filled Output
-
Add
0after% -
Empty spaces are filled with zeros
printf Integer Formatting Summary
printf() supports several integer formatting options:
-
Minimum width specification
-
Left-justified output
-
Zero-filled output
-
Showing
+sign for positive numbers
These options help control how numbers appear in the output.
Formatting Floating-Point Numbers with printf
The printf() function in C allows you to control how floating-point numbers are displayed.
Below are examples showing different ways to format floating-point output using printf():
printf String Formatting
The printf() function in C can be used to format string output.
Below are several examples that demonstrate how to format strings using printf().
printf Special Characters
The following character sequences have a special meaning when used as printf() format specifiers.
\a | Audible alert |
\b | Backspace |
\f | Form feed |
\r | Carriage return |
\? | Question mark |
\n | New Line |
\t | Tab |
\v | Vertical tab |
\\ | Back slash |
\0 | ASCII null character |
\’ | Single quotes |
\”” | Double quotes |
\o | Octal Constant |
\x | Hexadecimal constant |
Using Backslash and Special Characters in printf()
As seen in the previous example, the backslash (\) character is treated as a special character in C.
To print one backslash in the output, you must type two backslashes (\\) inside the string.
Examples of Using Special Characters
Below are some examples demonstrating how to use these special characters in printf().
scanf() Function in C Language
In C programming, the scanf() function is used to read characters, strings, and numeric data from the keyboard.
Basic Syntax Examples
These examples show how to read different data types:
Using * in scanf() (Ignoring Input)
Sometimes you may need to exclude characters from user input.
Example
Input: 30/01/2018
Goal: Read day, month, year separately as integers.
-
%c→ reads a character -
%*c→ reads one character but does not store it
Here, the two / characters are ignored.
More Examples
-
%*c→ exclude one character -
%*d→ exclude one integer -
%*f→ exclude one float -
%*s→ exclude one word
Note: \n and \t are each single characters.
Limiting Input Size in scanf()
You can limit the number of digits or characters by placing a positive integer inside the format specifier.
Examples
-
Takes only the first 5 digits
-
Extra digits are ignored
-
If fewer than 5 digits are entered, input is taken normally
Other Limits
-
%5c→ takes exactly 5 characters-
Array size must be at least 6 (extra space for
\0)
-
-
%5d→ integer up to 5 digits -
%5f→ float with at most 5 characters total -
%5s→ string up to 5 non-whitespace characters
Using [set] in scanf() (Pattern-Based Input)
[set] provides regex-like behavior for character input.
It works only with characters or strings, not integers or floats.
Read an Entire Line
-
^→ means up to -
\n→ newline character -
%[^\n]→ read everything until Enter is pressed
More Pattern Examples
-
%[^5]→ read characters until5appearsInput:
abcde fghk5k zaq
Output stored:abcde fghk -
%[0-9]→ read only digitsInput:
1234abc567
Output stored:1234 -
%[a-z]→ read only lowercase lettersInput:
abc12ijkl
Output stored:abc -
%[123]→ read any combination of1,2, or3Input:
1123390123
Output stored:11233
More Information
For additional details on input/output functions, visit:




Comments
Post a Comment