Write a program to declare and initialize variables and display them.

initialize variables
#include<stdio.h>
#include<conio.h>
int main()
{
	int age=22;
	float height=5.4;
	char sex='M';
	printf("Age: %d \nHeight: %f \nSex: %c",age,height,sex);
	getch();
}


/*In the above program ,int,height and char variables are declared and valu are assigned.Using printf()
statement values are displayed.
Maximum length of a variable up to 31 characters
The variable should not be a c keyword
The variable name should not start with a digit*/

The above code is a C program that declares and assigns values to three variables: an integer variable age, a floating-point variable height, and a character variable sex.

  1. The program starts by including the standard input/output header file stdio.h and the non-standard conio.h header file which provides the getch() function.
  2. Inside the main() function, it declares three variables age, height, sex with the int, float, char data types respectively and assigns values to them.
  3. The program uses the printf() function to print the values of the variables age, height and sex using format specifiers %d,%f and %c respectively.
  4. The getch() function is used to wait for the user to press a key before terminating the program.

The program also includes comments explaining the code and some rules for naming a variable in C language.

It’s important to note that the getch() function is not a part of the standard C library, it is provided by the non-standard conio.h header file and it’s use is not recommended in most cases.

Thanks

Write a program to declare and initialize variables and display them.

Leave a Reply

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

Scroll to top