printf and scanf in c programming

 C INPUT AND OUTPUT FUNCTIONS

Today we will learn about input and output functions in c, we use scanf() for taking an input from the user and use printf() function to print the output on the screen.

The standard library stdio.h contains the input and output functions printf() and scanf();

input and output functions in c
printf and scanf in c programming


The syntax of scanf:-

scanf("format specifier", &argument_list);  

The scanf() function is written with the keyword scanf followed by parenthetis and the format specifier within the quotes and then comma and then the argument list with address of operator.

The syntax of printf:-

printf("format specifier", argument_list);  

The printf() function is written with the keyword printf followed by parenthetis and the format specifier within the quotes and then comma and then the argument list.

Format Specifier:-

The format specifier is used to tell the compiler which data type is used like (int, float ..) %d, %f ..etc.

Examples:-

1. This example prints the number 10 as we are initializing n with 10 so it prints 10.>

 
    #include<stdio.h>
    int main()
    {
      int n=10;
      printf("The number is :%d", n) ;
      return 0;
    }  

OUTPUT

	The number is: 10

2. This example prints the character z as we are initializing the variable ch with a last z so it prints the value z.


    #include<stdio.h>
    int main() 
    {
    char ch='z';
    printf("The character is : %c", ch) ;
    return 0;
    }

  OUTPUT 
	The character is z


3. In this example first we are taking the input from the user by printing Enter a number: when the user enter a number we store the value that user entered in the variable n and then we prints the value to the screen.


   #include<stdio.h>
    int main() 
    {
    int n;
    printf("Enter a number :") ;
    scanf("%d", &n);
    printf("The number entered is : %d",n); 
    return 0;
    }
	
OUTPUT 

	Enter a number: 5

	The number entered is : 5

Post a Comment

0 Comments