Easy Tutorial
❮ C Examples Output Array C Standard Library Ctype H ❯

C Variable

A variable is essentially just a name for a storage area that a program can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.

The name of a variable can consist of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Uppercase and lowercase letters are distinct because C is case-sensitive. Based on the basic types explained in the previous chapter, the following are the fundamental variable types:

Type Description
char Typically a single byte (eight bits), this is an integer type.
int Integer type, 4 bytes, range from -2147483648 to 2147483647.
float Single-precision floating-point value. Format includes 1 bit for the sign, 8 bits for the exponent, and 23 bits for the fraction.
double Double-precision floating-point value. Format includes 1 bit for the sign, 11 bits for the exponent, and 52 bits for the fraction.
void Represents the absence of type.

C also allows for the definition of various other types of variables, such as enumerations, pointers, arrays, structures, unions, etc., which will be explained in subsequent chapters. This chapter focuses on the basic variable types.

Variable Definition in C

A variable definition tells the compiler where to create the storage for the variable and how to create that storage. A variable definition specifies a data type and includes a list of one or more variables of that type, as shown below:

type variable_list;

Here, type must be a valid C data type, which can be char, w_char, int, float, double, or any user-defined object. **variable_list** can consist of one or more identifier names separated by commas. Here are some valid declarations:

int    i, j, k;
char   c, ch;
float  f, salary;
double d;

The line int i, j, k; declares and defines the variables i, j, and k, indicating to the compiler to create variables named i, j, and k of type int.

Variables can be initialized (assigned an initial value) at the time of declaration. An initializer consists of an equal sign followed by a constant expression, as shown below:

type variable_name = value;

Here are some examples:

extern int d = 3, f = 5;    // declaration and initialization of d and f
int d = 3, f = 5;           // definition and initialization of d and f
byte z = 22;                // definition and initialization of z
char x = 'x';               // variable x has the value 'x'

Uninitialized definitions: Variables with static storage duration are implicitly initialized to NULL (all bytes have the value 0), while the initial values of all other variables are undefined.

Variable Declaration in C

A variable declaration ensures that the variable exists with the specified type and name, allowing the compiler to continue with further compilation without needing the complete details of the variable. A variable declaration is meaningful only at compile time and requires the actual variable declaration during program linking.

There are two cases for variable declarations:

Unless specified with the extern keyword, all declarations are definitions.

extern int i; // declaration, not definition
int i; // declaration and definition

Example

In the following example, variables are declared at the top but defined and initialized within the main function:

#include <stdio.h>

// Variable definitions outside the function
int x;
int y;
int addtwonum()
{
    // Declaration of variables x and y as external variables
    extern int x;
    extern int y;
    // Assign values to external variables (global variables) x and y
    x = 1;
    y = 2;
    return x + y;
}

int main()
{
    int result;
    // Call the function addtwonum
    result = addtwonum();

    printf("result is: %d", result);
    return 0;
}

When the above code is compiled and executed, it produces the following result:

result is: 3

To reference a variable defined in another source file, simply add the extern keyword to the variable declaration in the referencing file.

addtwonum.c file code:

#include <stdio.h>
/* External variable declarations */
extern int x;
extern int y;
int addtwonum()
{
    return x + y;
}

test.c file code:

#include <stdio.h>

/* Define two global variables */
int x = 1;
int y = 2;
int addtwonum();
int main(void)
{
    int result;
    result = addtwonum();
    printf("result is: %d\n", result);
    return 0;
}

When the above code is compiled and executed, it produces the following result:

$ gcc addtwonum.c test.c -o main
$ ./main
result is: 3

Lvalues and Rvalues in C

There are two types of expressions in C:

Variables are lvalues and can appear on the left side of an assignment. Numeric literals are rvalues and cannot be assigned to, thus they cannot appear on the left side of an assignment. The following is a valid statement:

int g = 20;

However, the following is not valid and will result in a compile-time error:

10 = 20;
❮ C Examples Output Array C Standard Library Ctype H ❯