totn C Language

C Language: Variables

This C tutorial explains how to declare variables in the C language.

Description

In the C Programming Language, variables are used to store various types of data. Variables must be defined before they can be used within a program. Variable declarations include the type of the variable and, optionally, its initial value.

Syntax

The syntax for declaring a variable in the C language is:

c_type variable_name1 [= value1];

Additional variables of the same C type can be declared by comma separating the variables as follows:

c_type variable_name1 [= value1] [, variable_name2 [= value2], ... variable_name_n [= value_n]];

Parameters or Arguments

c_type
The type of the variable being declared. Refer to the Types section below for a list of the common C types.
variable_name1
The name of the first variable to declare.
value1
Optional. If provided, it is the value to assign to variable_name1.
variable_name2, ... variable_name_n
Optional. These are additional variables that will be declared with the same C type.
value2, ... value_n
Optional. These are the values that you wish to assign to variable_name2 through variable_name_n.

Note

  • Declaration statements must end with a semicolon.

Constant Variable

To define a constant variable, you can place the keyword const in front of the declaration as follows:

const c_type variable_name1 [= value1];

Variables defined as constants can not be changed after their initial value is set.

Types

The following is a table of the most common variable types used in C. The min and max values are based on a 32-bit system:

C TypeMin ValueMax Value
signed char-128127
unsigned char0255
short int-32,76832,767
unsigned short int065,535
int-2,147,483,6482,147,483,647
unsigned int04,294,967,295
long int-2,147,483,6482,147,483,647
unsigned long int04,294,967,295
float1.17 x 10-383.4 x 1038
double2.2 x 10-3081.79 x 10308
TIP: Variable min/max values may vary from system to system. To find the appropriate values for your system, locate and refer to the limits.h C header file on your system.

Let's take a closer look at how to define and use each of these C variable types.