C Variables

In simple words, variable is a name given to memory box with a name, where we can “store” some value. Its value can be changed depending upon conditions and it can be reused many times.

  • A variable can support different type of data such as integer, float, character etc
  • Value of variables can be changed according to the information passed to the program.
  • You can perform different set of operation to the variables.

Rules for naming C Variable

  • A Variable name must begin with letter or underscore.
  • Variables are case sensitive
  • You can use letters & digits in variable names.
  • No special symbols can be used other than underscore.
  • sum, city, person_2, _value are some examples for Variable name

Characters allowed in C variable name:

You can use Underscore(_), Capital Letters ( A – Z ), Small Letters ( a – z ) and Digits ( 0 – 9 ) while choosing a variable name. Blanks, Commas and other special symbols are not allowed in variable naming. Variable name Should not be Reserved Word.

Example of Valid or Invalid Variable Names

These are some of the examples of valid variable names for city

  • city
  • City
  • City1
  • _City
  • my_city
  • city_1
  • city_of_person
  • status_flag
  • _city1

Invalid Variable Names

  • 1city
  • 1_city
  • city 1

Declaring & initializing C Variable

  • First, you need to declare a variable in the C program before to use it.
  • Memory space is not created for a variable during a declaration. It happens only on the variable definitions.
  • Variable initialization assigs a value to the variable.

1.Variable declarationdata_type variable_name;int x, y, z; char ch;
2.Variable initializationdata_type variable_name = value;int x = 10, y = 20; ch=’l’;

Example of Variable in C Programming

Here is the simple example of C Variable:

int main()
	/* Variable declaration */
	int num;
	/* Variable initialization */
	num = 10;
	/* print value of a variable */ printf("Number is: %d \n", num); 
	return 0;
Tutorials Class - Output Window

Number is=10

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.