C printf
and scanf
Syntax
//printf
printf("format-string", argumentList);
//scanf
scanf("format-specifiers", &arguments);
Example
#include <stdio.h>
int main() {
int num1,num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("Sum of %d and %d is %d", num1, num2, num1 + num2);
return 0;
}
Output
The output of the above example will be:
Enter two integers: 10 20
Sum of 10 and 20 is 30
Explanation
C printf
and scanf
are two commonly used functions input/output operations in C programming.
printf
is a function used for outputting data to the console. It takes in a string format and a list of arguments as input and outputs the data in a formatted way to the console.scanf
is a function used for reading data from the keyboard, file, or any other standard input device. It scans the data entered by the user and stores it in the memory location provided.
Use
The printf
function is used to output data to the console in a formatted way. It can be used to output strings, integers, floating-point numbers, and other types of data.
scanf
function is to read data from standard input in a specific format. It can data as integers, floating-point numbers, characters, and strings.
Important Points
- The
printf
function outputs data in a formatted way using a string format and a list of arguments. - The
scanf
function reads data from standard input and stores them in a memory location provided to it. - The format specifiers used in
printf
andscanf
functions should match the type of the argument. For example,%d
is used for integers,%f
is used for floating-point numbers, and%c
is used for characters. - The
scanf
function requires the memory addresses of the variables to read data into. This is done by the use of the&
operator.
Summary
C language printf
and scanf
functions are essential for input/output operations in C programming. The printf
function outputs data to the console in a formatted way, while the scanf
function is used to read data from standard input. Both printf
and scanf
functions use different format specifier characters to match the data type of the argument. Understanding the proper use and syntax of these functions is essential when it comes to writing successful C code.