Monday 28 September 2020

Give the syntax and examples of usage of scanf() and printf().

 Question: Give the syntax and examples of usage of scanf() and printf().

Answer

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).

printf() function

The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

printf("format string",argument_list); 

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

scanf() function

The scanf() function is used for input. It reads the input data from the console.

scanf("format string",argument_list);

Example of input and output in C language that prints addition of Two numbers.

#include<stdio.h>
#include<conio.h>  

void main()
{    
    int x, y, sum;  
  
    printf("enter first number:");  
    scanf("%d", &x);  
    printf("enter second number:");  
    scanf("%d", &y);  
  
    sum=x+y;  
    printf("Sum of two numbers: %d ", sum);  
    getch();
}

Output

enter first number:9
enter second number:4
Sum of two numbers: 13

No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint