Printf function

printf() is used to display output.
Syntax :
printf ("format string", argument list);

format string : collection of escape sequence or/and conversion specification.
Argument list : contains a list of variables.

Example of scanf and printf :

#include<stdio.h>
int main()
{
    char str[10];
    char ch;
    int i;
    float f;

    printf("Enter Character : ");
    scanf("%c",&ch);  // Accept Character
    printf("\nCharacter is : %c ",ch);

    printf("\nEnter String : ");
    scanf("%s",str);  // Accept String
    printf("\nString is : %s ",str);

    printf("\nEnter Integer : ");
    scanf("%d",&i);  // Accept Integer
    printf("\nInteger is : %d ",i);

    printf("\nEnter Float : ");
    scanf("%f",&f);  // Accept Float
    printf("\nFloat is : %f ",f);

    return 0;
}



Explanation :
scanf will take the input as character (%c), string (%s), integer (%d), float (%f) and displays the output through printf statement by using conversion specification (%c, %s, %d, %f).
NOTE :
  • 1 .#include<stdio.h> is mandatory for printf function.
  • 2. format specification of scanf and printf function must be enclosed in double quotes.
  • 3. & ampersand is required in argument list of scanf function to be used with the list of variables.
  • 4. The format specification and argument list must be separated by comma.

Post a Comment

0 Comments