Write a program to demonstrate the use of scanf() with diffrent format.

#include<stdio.h>
#include<conio.h>
int main()
{
	int a,b;
	float x;
	char name[20];
	printf("Enter two integers:-\n");
	scanf("%4d %4d",&a,&b);
	printf("\n Entered integers are");
	printf("\n %4d %4d",a,b);
	printf("\n");   //Only new line//
	printf("\n Enter a real number:-\n");
	scanf("%f",&x);
	printf("\n Enter float number is:-");
	printf("\n %f",x);
	printf("\n");  //Only new line//
	printf("\n Enter a string :- \n");
	scanf("%7s",name);
	printf("\n Entered string");
	printf("\n %7s",name);
	getch();
	
}

This program demonstrates the use of scanf() with different format specifiers to read input from the user.

  • scanf("%4d %4d", &a, &b) reads two integers from the user and stores them in the variables a and b. The %4d specifier limits the maximum number of digits that can be entered for each integer to 4.
  • scanf("%f", &x) reads a real number from the user and stores it in the variable x.
  • scanf("%7s", name) reads a string from the user and stores it in the character array name. The %7s specifier limits the maximum length of the string to 7 characters.

The program then displays the entered values using printf() with different format specifiers.

Thanks

Write a program to demonstrate the use of scanf() with diffrent format.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top