Printf and scanf in C Language
Printf and scanf in C Language
·
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 C program to make use of these
printf() and scanf() library functions in C language.
1.
PRINTF() FUNCTION IN C LANGUAGE:
·
In C programming language, printf() function is
used to print the (“character, string, float, integer, octal and hexadecimal
values”) onto the output screen.
·
We use printf() function with %d format specifier to display the value of
an integer variable.
·
Similarly %c is used to
display character, %f for float
variable, %s for string
variable, %lf for double
and %x for
hexadecimal variable.
·
To generate a newline,we use “\n” in C printf()
statement.
Note:
·
C
language is case sensitive. For example, printf() and scanf() are
different from Printf() and Scanf(). All characters in printf() and scanf()
functions must be in lower case.
·
%d
got replaced by value of an integer variable (no),
·
%c got
replaced by value of a character variable (ch),
·
%f got
replaced by value of a float variable (flt),
·
%lf got
replaced by value of a double variable (dbl),
·
%s got
replaced by value of a string variable (str),
·
%o got
replaced by a octal value corresponding to integer variable (no),
·
%x got
replaced by a hexadecimal value corresponding to integer variable
·
\n got
replaced by a newline.
2.
SCANF FUNCTION IN C LANGUAGE:
·
In
C programming language, scanf() function is used to read character, string,
numeric data from keyboard
·
Consider
below example program where user enters a character. This value is assigned to
the variable “ch” and then displayed.
·
Then,
user enters a string and this value is assigned to the variable “str” and
then displayed.
#include <stdio.h>
int main()
{
char ch;
char str[100];
printf("Enter
any character \n");
scanf("%c",
&ch);
printf("Entered
character is %c \n", ch);
printf("Enter
any string ( upto 100 character ) \n");
scanf("%s",
&str);
printf("Entered
string is %s \n", str);
}
Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
hai
Entered string is hai
Comments
Post a Comment