C Variables

Variables are memory locations(storage area) in the C programming language.

The primary purpose of variables is to store data in memory for later use. Unlike constants which do not change during the program execution, variables value may change during execution. If you declare a variable in C, that means you are asking to the operating system for reserve a piece of memory with that variable name.

Variable Definition in C

Syntax:
type variable_name;

or

type variable_name, variable_name, variable_name;

Variable Definition and Initialization

Example:
int    width, height=5;
char   letter='A';
float  age, area;
double d;

/* actual initialization */width = 10;
age = 26.5;

Variable Assignment

Variable assignment is a process of assigning a value to a variable.

Example:
int width = 60;
int age = 31;

There are some rules on choosing variable names

  • A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character.
  • The first character must be a letter or underscore.
  • Blank spaces cannot be used in variable names.
  • Special characters like #, $ are not allowed.
  • C keywords cannot be used as variable names.
  • Variable names are case sensitive.
  • Values of the variables can be numeric or alphabetic.
  • Variable type can be char, int, float, double or void.

C Program to Print Value of a Variable

Example:
#include<stdio.h>

void main()
{
    /* c program to print value of a variable */    int age = 33;
    printf("I am %d years old.\n", age);
}
Program Output:
I am 33 years old.

Here are few other related articles for you to read:

Scroll Back to Top