Input and Output Operations in C Language
In the C programming language, you can perform input and output operations using the printf
and scanf
functions.
Printf (Output Operation)
The printf
function is used to display output on the console or terminal. It has the following syntax:
printf("format string", argument1, argument2, ...);
Here, the format string
specifies the format of the output, and the optional arguments
are the values to be displayed. The printf
function replaces the placeholders in the format string
with the corresponding values of the arguments. The placeholders start with %
and are followed by a letter or a combination of letters that specify the data type of the argument.
For example, the following printf
statement displays the string “Hello, World!” on the console:
printf("Hello, World!");
And the following printf
statement displays the value of a variable named x
:
int x = 10;
printf("The value of x is %d\n", x);
The %d
placeholder is used to display integer values.
Scanf (Input Operation)
The scanf
function is used to read input from the console or terminal. It has the following syntax:
scanf("format string", &variable1, &variable2, ...);
Here, the format string
specifies the format of the input, and the &
operator is used to pass the address of the variables to be read. The scanf
function waits for the user to input values according to the specified format, and then stores the values in the variables.
For example, the following scanf
statement reads an integer value from the console and stores it in a variable named x
:
int x;
scanf("%d", &x);
The %d
placeholder is used to read integer values.
Note that it’s important to use the correct data type placeholders in the printf
and scanf
statements, and to pass the address of the variables to be read by scanf
. This helps to prevent errors and ensure that the input and output operations work correctly.