Variables in C

Last Updated Nov 1, 2015, 07:00:14 PM





Variables in C program refers to some location in the memory. The location is used to hold a value or data of an object of any type

Imagine if there 10 teddy bears we need ten different boxes to keep them. Just like that a variable needs to be stored in some memory location.

Variables are Temporary

Variables exist just as long as they needed and then disposed of. They memory location is not permanent the locations will be interchanged on demand.

The variable name can be mixed with letters, digits, and underscore character.

Different types of variables require different amounts of memory.

  • Variables in C must begin with a letter of an underscore
  • Uppercase and lowecase letters are not same both have a different meaning
  • Variable names in C language are case-sensitive
Variable Declaration
type variable_name;

There are five different types of variables in C programming.

  • char
  • int
  • float
  • double
  • void

How do define variables in C:

Variables must be declared before using them in your program. You can do this by writing the list of variables in the beginning of the program. When we do this, the compiler knows what variables being called and what type of variables they are. This entire process is called variable definition

When you write complex programs you might want to declare multiple variables in the same line. You can also declare multiple variables in the same line in C language. See the below example

During multiple variable declarations, you must separate each variable with a comma(,) as a separator. Also, all variables must be of the same type. For example, if you declare a multiple variable as int i, j, k; please note that, i , j and k must be of integer type.

You can also initialize variables in C. Initializing means, at the time of declaration only you can assign a value to the variable you declared.

Example

The above example tells the compiler, there are two different variables. i.e 1. integer and 2. char variable that holds the string type of data up to ten characters.

All variables in c must end with a semicolon(;)

Example of C Variables

Declaring Single Variable in C Programming

Try It Now

Multiple variable declaration in C Programming

Try It Now

As we already know, variables can be declared and initialized in the same line, let us see an example program.

Variable Initialization in C Programming

Variable initialization is C means, we need to assign the value once the variable is declared in the same line. In the below example the int i, j, k has the values assigned to it. In programming terms, it is called as variable initialization

Try It Now

Other Variable Topics you might want to learn

Global Variables in C

Static Variabls in C

Difference between Static and Global Variables- StackOverFlow