printf() and scanf() function in C
printf() function in C
printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default.These functions are declared and related macros are defined in “stdio.h” which is a header file in C language.
We have to include “stdio.h” file as shown in below to make use of these printf() and scanf() library functions in C program.
#include <stdio.h>
In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
Here’s a quick summary of the available printf format specifiers:
%c character
%d decimal (integer) number (base 10)
%ld Long integer
%e exponential floating-point number
%f floating-point number
%lf double number
%i integer (base 10)
%o octal number (base 8)
%s a string of characters
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%% print a percent sign
\% print a percent sign
Controlling integer width with printf
%d decimal (integer) number (base 10)
%ld Long integer
%e exponential floating-point number
%f floating-point number
%lf double number
%i integer (base 10)
%o octal number (base 8)
%s a string of characters
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%% print a percent sign
\% print a percent sign
The %3d specifier is used with integers, and means a minimum width of three spaces, which, by default, will be right-justified:
Left-justifying printf integer output To left-justify integer output with printf, just add a minus sign (-) after the % symbol,
printf integer zero fill option To zero-fill your printf integer output, just add a zero (0) after the % symbol,
printf integer formatting
As a summary of printf integer formatting, here’s a little collection of integer formatting examples. Several different options are shown, including a minimum width specification, left-justified, zero-filled, and also a plus sign for positive numbers.
Formatting floating point numbers with printf
Here are several examples showing how to format floating-point numbers with printf:
Here are several examples showing how to format floating-point numbers with printf:
printf string formatting
Here are several examples that show how to format string output with 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
|
As you can see from that last example, because the backslash character itself is treated specially, you have to print two backslash characters in a row to get one backslash character to appear in your output.
Here are a few examples of how to use these special characters:
scanf() function in C language:
In C programming language, scanf() function is used to read character, string, numeric data from keyboard.
The examples show various syntax for reading different data types
int x; scanf(“%i”,&x); or
scanf(“%d”,&x);
float x;scanf(“%f”,&x);
char x; scanf(“%c”,&c);
long int x; scanf(“%ld”,&x);
char str[100];scanf(“%s”,str);
Sometimes, you may need to exclude some character or number from user input. Suppose, the input is 30/01/2018 and you want to get day, month, year separately as integer. You can achieve this, using “ * ” in string format.int day,month,year;
scanf("%d%*c%d%*c%d", &day, &month, &year);
Here, %c is character type specifier and %*c means one character would be read from the console but it wouldn’t be assigned to any variable. Here we excluded two “/” from the input.
%*c would exclude one character. Remember, ‘/n’ and ‘/t’ are single characters.
%*d would exclude one integer.
%*f would exclude one float.
%*s would exclude one word.
Number in scanf:
Sometimes, we need to limit the number of digit in integer or float, number of character in string. We can achieve this by adding an integer(>0) in the string format.
int a;scanf("%5d", &a);
[set] in scanf:
The code above, would take just first 5 digit as input. Any digits after 5th one would be ignored. If the integer has less that 5 digits, then it would be taken as it is.
%5c would take exact 5 characters as input. Remember, if you take 5 character as input and want to store in an array, then the array size must not be less than 6. This means, the extra position is reserved for a null character `\0`. Otherwise, you may face some unexpected difficulties.
%5d would take an integer of at most 5 digits.
%5f would take a float type number of at most 5 digits or 4 digits and one “ . ” as input.
%5s would take a string of at most 5 non white-space characters as input.
[set] in scanf:
You can achieve regular expression like flavor in scanf using [set] in string format. This method does not work with integer or float type. Using [set], you can only take character type input.
char str[100];scanf("%[^\n]", str);
%[^\n] would take all characters in a single line as input. You may know that, whenever we press Enter in keyboard, then “\n” character is written in the console. Here, “^” means up-to and “\n” means new line and finally, [^\n] means take input up-to new line.
Similarly, %[^5] would take all characters as input up-to 5. If you write “abcde fghk5k zaq” in the console, then just “abcde fghk” would be taken as input.
%[0–9] would take just number characters as input. If you write “1234abc567” in the console, then just first numbers “1234” would be taken as input.
Similarly, %[a–z] would take just small letter alphabets as input. If you write “abc12ijkl” in the console, then just first small letters “abc” would be taken as input.
%[123] would just take any combination of 1, 2, 3 as input. Anything other than 1,2,3 would be ignored. If you write “1123390123” in the console then just first “11233” would be taken as input.
for more details of various input output functions visit https://en.cppreference.com/w/c/io/fscanf
Comments
Post a Comment