C Variables

A variable is a data name that may be used to store a value. Unlike constant variables are also remain unchanged during the execution of a program. A variable name should be chosen in a meaningful way so that it relects its function or nature in the program. There are some rules for choosing a variable name. They are following:

  • Must begin with a letter or underscore.
  • First 31 characters are significant.
  • Variables name are case sensitive.
  • consist of Capital letters(A-Z), lowercase letters(a-z), digits(0-9), and the underscore character.
  • Should not be a keyword.
  • White space is not allowed.
  • Special characters are not allowed.

There are the following variable types in C language:

Type Description
char Used to store characters and letters
int Used to store an integer.
float Used to store single-precision floating point value.
double Used to store double-precision floating point value.
void Comprises an empty set of values.

Example of a c program using variable is given below:

Example

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int first_number, second_number, sum;
  5. printf("Enter two integers: ");
  6. scanf("%d %d", &first_number, &second_number);
  7. sum = first_number + second_number;
  8. printf("%d + %d = %d", first_number, second_number, sum);
  9. return 0;
  10. }

Output:

Enter two integers: 7
            17
            7 + 17 = 24