State the use of printf() and scanf() with suitable example
The printf() function is used to display output and the scanf() function is used to take input from users.
The printf() and scanf() functions are commonly used functions in C Language. These functions are inbuilt library functions in header files of C programming.
printf() Function:
In C Programming language, the printf() function is used for output.
printf() function can take any number of farguments. First argument must be enclosed within the double quotes "hello" and every other argument should be separated by comma (,) within the double quotes.
Important points about printf():
printf() function is defined in stdio.h header file. By using this function, we can print the data or user-defined message on monitor (also called the console).
- printf() can print a different kind of data format on the output string.
- To print on a new line on the screen, we use "\n" in printf() statement.
C language is case sensitive programming language. For example, printf() and scanf() in lowercase letters treated are different from Printf() and Scanf(). All characters in printf() and scanf() builtin functions must be in lower case.
Syntax :
printf("format specifier", argument_list);
The format string for output can be :
%d (integer), %c (character), %s (string), %f (float) %lf (double) and %x (hexadecimal) variable.
Simple Example of printf() Function
#include<stdio.h>
void main()
{
int num=450;// assign number
printf("Number is %d \n", num);//print number
getch();
}
scanf() function:
In C programming language, scanf is a function that stands for Scan Formatted String. It is used to read data from stdin (standard input stream i.e. usually keyboard) and then writes the result into the given arguments.
It accepts character, string, and numeric data from the user using standard input.
scanf also uses format specifiers like printf.
Simple Example of scanf() Function
// C program to implement scanf
#include <stdio.h>
void main()
{
int a, b;
printf("Enter first number: ");
scanf("%d", &a);//accept 1st no from user
printf("Enter second number: ");
scanf("%d", &b);//accept 2nd no from user
printf("A : %d \t B : %d" , a , b);//print both numbers
getch();
}
Comments
Post a Comment