Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

printf() and scanf() functions in C

printf() function

Its printf() function as an built-in library function within C language. It’s utilized for output, i.e, to print the specified statement to the console. Printf() function can be defined within stdio.h (header file) within the C library.

It will return the amount of characters printed. If there’s an issue, then it returns an error number.

Printerf() function can be used to print “character”, string, integer, octal, float and hexadecimal numbers on the screen output.

Syntax:

printf(“format string”,argument_list);

Format string: It’s an essential parameter, which can take value in %d (integer) %c (character) %s (string)and %f (float) etc.

Argument List: It’s an optional parameter that contains all variables needed to print.

Printf() function using the format specifier %d to show values of integer variables.

To create a newline, we make use of “\n” in the C printf() statement.

scanf() function

This scanf() function is a library function built in within C language. It is utilized for input, i.e, to read input data from the console. Its scanf() function is described in stdio.h (header file) in C library.

The Scanf() function can be used to read character or string data, as well as numeric information from keyboards

Syntax:

scanf(“format string”,&argument_list);

Format string: It’s mandatory parameter that accepts value in %d (integer)(integer) %c (character)or %s (string)and %f (float) etc.

& (Ampersand): It is added before the variable’s name, which signifies that data needs to be fed.

Argument List: It’s an optional parameter that contains all variables into which the input data has to feed. Then user inputs an number of st

Example to understand printf() and scanf() functions:

#include

inside main(){int main()

int x, y, z;

printf(“The result of the scanf() function is “%d”,

scanf(“%d%d%d”, &x, &y, &z));

printf(“\nx = %d”, x);

printf(“\ny = %d”, y);

printf(“\nz = %d”, z);

Return 0.

}

Output

This output program of above program is as follows: The output of above program is as follows:

7 5 4

The return value of this scanf() function is: 3.

7/7 = x

y = 5

z = 2

Now let’s look at the above-mentioned program

There are three int variables i.e. namely x, y and Z. The values are entered by the user through scanning() function, and result of scanf() is printed. The code snippet which illustrate process is as follows:

int x, y, z;

printf(“The result of the scanf() function is “%d”,

scanf(“%d%d%d”, &x, &y, &z));

Then the numbers of the variables x,y, and z which were calculated by user are then printed. The code snippet which illustrates the procedure is as follows:

printf(“\nx = %d”, x);

printf(“\ny = %d”, y);

printf(“\nz = %d”, z);

string value is assigned to the variable “str” and then displayed.

Leave a comment

Your email address will not be published.