scanf() and printf() are the in-built input and output functions in C. They are found in header file <stdio.h>. The f in printf() and scanf() specifies formatted input and output in which we can specify the format - width justification and precision as to where and how the output should be printed on the console.
Format Specifiers
Integer
short signed %d or %l
short unsigned %u
int %d
long signed %ld
long unsigned %lu
unsigned hexadecimal %x
unsigned octal %o
Real
float %f
double %lf
long double %Lf
Character
signed, unsigned char %c
String
%s
Width & Justification
If given number itself require more columns than specified than width value is ignored. If specified value of width is greater, blanks are padded in left side of the value to be printed.
%d - print an integer, 1 column wide.
%2d - print an integer, 2 columns wide
%nd - print an integer, n columns wide
%-nd - print an integer, n columns wide padding cells in right side of the value to be printed.
Precision
Precision value is used when we want a result to having values specifying the result having floating point upto certain number of digits.
a float (%f) originally shows 6 digits of precision
Suppose we want our output to show less digits.
%6.3f - specifies 6 columns wide real value with 3 digits after decimal point. Rest of digits after decimal point will be truncated.
Excape Sequence
Certain defined characters following backslash '\' are excape sequences that convey a special meaning to compiler.
\n - newline
\b - backspace
\' - single quote
\\ - backslash
\t - tab
\r - carriage return
\a - alert
\" - double quotes
\', \" is used because compiler understand anything enclosed in ' ' as a character and anything enclosed in " " as string. They when used without \ convey special meaning. When we have to use them as it is in output, we use backslash with them.
For example
printf("It said , \"I am from Mars.\"");
Output : It said, "I am from Mars."
If we try to print this statement without using \" compiler will give error because it can go crazy with such usage of " ".
Syntax of printf()
printf("Format String including format specifier, justification, precision", list of variable);
For example
int a=200; float b=20.6673;
printf("\nThe Value of Sum =%6d and rate=%4.2f", a,b);
Output
The Value of Sum= 200 and rate=20.66
Syntax of scanf()
scanf("format_specifier",&variable_name);
(&) -the address of operator
&variable_name : return address location ( hexadecimal value) of the variable it precedes
It tells the scanf to store the value input from the keyboard at memory location which variable a corresponds to.
For example
scanf("%d",&a);
stores and integer value in variable a.
Format Specifiers
Integer
short signed %d or %l
short unsigned %u
int %d
long signed %ld
long unsigned %lu
unsigned hexadecimal %x
unsigned octal %o
Real
float %f
double %lf
long double %Lf
Character
signed, unsigned char %c
String
%s
Width & Justification
If given number itself require more columns than specified than width value is ignored. If specified value of width is greater, blanks are padded in left side of the value to be printed.
%d - print an integer, 1 column wide.
%2d - print an integer, 2 columns wide
%nd - print an integer, n columns wide
%-nd - print an integer, n columns wide padding cells in right side of the value to be printed.
Precision
Precision value is used when we want a result to having values specifying the result having floating point upto certain number of digits.
a float (%f) originally shows 6 digits of precision
Suppose we want our output to show less digits.
%6.3f - specifies 6 columns wide real value with 3 digits after decimal point. Rest of digits after decimal point will be truncated.
Excape Sequence
Certain defined characters following backslash '\' are excape sequences that convey a special meaning to compiler.
\n - newline
\b - backspace
\' - single quote
\\ - backslash
\t - tab
\r - carriage return
\a - alert
\" - double quotes
\', \" is used because compiler understand anything enclosed in ' ' as a character and anything enclosed in " " as string. They when used without \ convey special meaning. When we have to use them as it is in output, we use backslash with them.
For example
printf("It said , \"I am from Mars.\"");
Output : It said, "I am from Mars."
If we try to print this statement without using \" compiler will give error because it can go crazy with such usage of " ".
Syntax of printf()
printf("Format String including format specifier, justification, precision", list of variable);
For example
int a=200; float b=20.6673;
printf("\nThe Value of Sum =%6d and rate=%4.2f", a,b);
Output
The Value of Sum= 200 and rate=20.66
Syntax of scanf()
scanf("format_specifier",&variable_name);
(&) -the address of operator
&variable_name : return address location ( hexadecimal value) of the variable it precedes
It tells the scanf to store the value input from the keyboard at memory location which variable a corresponds to.
For example
scanf("%d",&a);
stores and integer value in variable a.
No comments:
Post a Comment