CodesJava

Easy learning with example program codes

c printf and scanf


C printf() function:

The printf() function is an inbuilt library function in C language, which is used for output, i.e, to print a given statement on the console. The printf() function is defined in stdio.h (header file) in C library.

Syntax:

printf(“format string”,argument_list);

  • Format string: It is a mandatory parameter which takes value as %d (integer), %c (character), %s (string), %f (float) etc.
  • Argument List: It is also a mandatory parameter which includes all the variables that are required to print.

C scanf() function:

The scanf() function is an inbuilt library function in C language, which is used for input, i.e, to  read the input data from the console. The scanf() function is also defined in stdio.h (header file) in C library.

Syntax:

scanf(“format string”,&argument_list);

  • Format string: It is a mandatory parameter which takes value as %d (integer), %c (character), %s (string), %f (float) etc.
  • & (Ampersand): It is added just before the variable name which represents that the data need to be feeded.
  • Argument List: It is also a mandatory parameter which includes all the variables in which the input data need to be feed.

 

Example:

#include<stdio.h>
 
void main()
{
int a, b;
printf ("Enter two numbers: ");
scanf ("%d, %d",&a,&b);
printf("Sum of x+y = %i", a+b);
}

Output

Enter two numbers: 2,3             
Sum of x+y = 5
Posted in C   
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki