JavaClassCode.com

facebook twitter googleplus

C Tutorial

C Introduction C Installation C Keywords and Identifier C Variables C Constants C Data Types C Input/Output C Operators C Introduction Examples

C Control Flow

C if...else C for loop C while loop C break and continue C switch case C goto Control Flow Examples

C Functions

C Function C User-define Function C Function Types C Recursion C Storage Class C Function Examples

C Pointers

C Pointers C Pointers & Arrays C Pointers and Functions C Memory Allocation Array and Pointer Examples

C Strings

C Strings C String Functions C String Examples

Structure and Union

C Structure C Struct and Pointers C Struct and Function C Unions C Struct Examples

C Programming Files

C Files Input/Output C Files Examples

Additional Topics

C Enumeration C Preprocessors C Standard Library C Programming Examples

C Variables


Variables

In programming language, a variable is a storage area or bucket that holds the data. The variable should be provided as a unique name. A variable name is symbolic representation of a memory location. For example:

Here, amount is a variable of data type int and the variable amount is assigning an integer value 500. The value of variable can be changed in the program. It can hold different value at different time. For example:

It is good practice to provide the meaningful variable names. For example, if we want to calculate the simple interest then it is advice to give a meaningful name of variables such as prin, roi, noy to represent principle, rate of interest and number of years rather than providing variables name as x, y, z.

Naming convention of variable

  • Variable is case-sensitive. For example: int a and int A are different variables.
  • Variable starts with any alphabet (a-z, A-Z) or underscore (_).
  • Variables can be named alphanumeric. For example: a1 = 5, var1, var2
  • Variable does not allow space.
  • Variable name cannot be allows as any C keywords.

You should always try to provide a meaningful variable name. For example: firstName is better than fn as a variable.

C programming is strongly typed programming language. That means, once the variable type is declared it cannot be changed in the program later. For example:

Here the data type of number variable is int, you cannot assign a float (decimal) value in it. You also cannot redefine number variable to double.

C Variable Declaration

When a variable declared, then this variable allocates the memory according to the data type.

After a variable declaration, it takes the “Garbage value” inside itself. Garbage value is provided by compiler to the variable.

Syntax for Single Variable Declaration

For example

Source code

Output

Value of x is : 14

Here the value of x 14 is a garbage value provided by compiler to x.

Syntax for Multiple Variable Declaration

For example

Source code

Output

Value of x is : 14
Value of y is : 23
Value of z is : 64

Here the value of x 14, 23, and 64 is a garbage value provided by compiler to x, y, and z respectively.

C Variable Initialization

When a variable is initialized, the variable allocates memory according to its data type. For example: int for 2bytes (16-bit) | 4bytes (32-bit) | 8bytes (64-bit).

In variable initialization, a variable takes only one value at a time. For example: int a = 10; int a = 10, 20;

Syntax for Single Variable Initialization

For example

Source code

Output

Value of x is : 10

Syntax for Multiple Variable Initialization

For example

Source code

Output

Value of x is : 10
Value of y is : 20