How to Declare Variable in C

What is variable in C?

In C, a variable is a user defined name that is used as a container to store data values.

Variables are used to store and manipulate data in C program.

The data type of a variable in C needs to be specified explicitly (data type must be declared before variable name).

Declaring Variable in C

1. Declaring Variable without Initialization:

In C, variable can be declared without initialization (without assigning value) and value can be assigned to the variable later.

Variable Syntax Without Initialization

datatype : It specifies the data type of the variable to be declared. The 'datatype' will be replaced by the specified data type like int, float, char, etc.

variable_name : It refers to a suitable name of a variable. The 'variable_name' will be replaced by a suitable variable name.

Example of Variable Without Initialization
Initialization of Variable

In the example, the code int my_number; declares an integer variable named my_number without assigning any value. The variable will be initialized later.

The code my_number = 25; initializes the variable my_number by assigning an integer value 42.

Different components of the variable in the example:

  • int is the data type.
  • my_number is the variable name.
  • 25 is the value assigned to the variable.

2. Variable Syntax with Initialization:

In C, variables can be declared and initialized (assigning value) with value at the time of declaration.

Variable Syntax With Initialization

datatype : It specifies the data type of the variable to be declared. The 'datatype' will be replaced by the specified data type like int, float, char, etc.

variable_name : It refers to a suitable name of a variable. The 'variable_name' will be replaced by a suitable name that you prefers for your variable.

value : This refers to the data value to be assigned to the variable at the time of variable declaration. The 'value' will be replaced by your data value.

Example of Variable With Initialization

In the example, the code float my_float_num = 12.56; declares a float (floating-point) data type variable named my_float_num and initializes the variable by assigning a floating-point value 12.56.

Different components of the variable in the example:

  • float is the data type.
  • my_float_num is the variable name.
  • 12.56 is the value assigned to the variable.

3. Multiple Variables in Single Statement:

In C, multiple variables of the same type (data type) can be declared in one statement.

Example of Multiple Variables in Single Statement

In the example, the code int x, y, z; declares multiple variables in a single statement without initialization of the variables. These variables are initialized later. While the code int a = 5, b = 10, c = 15; declares multiple variables in a single statement with initialization of the variables.

More Examples of Declaring Variables in C

Output of the above example

Integer: 42
Float: 3.140000
Integer x: 10
Integer y: 12

%d and %f are formatters in C. Please read How to use formatters in C

Live Code Playground

In the following C code editor, you can practice by creating more variables. Click the 'Execute Code' button to execute the code; the executed output will be displayed in the following C Code Output frame. Practice until you become comfortable and proficient with your code.

C logo C Online Editor
C Code Output