
Education Journalist | Study Abroad Lead
Variables in C are essential elements in programming, serving as storage units for data manipulation. In the context of the C language for GATE exam preparation, a thorough understanding of variables is crucial for developing effective and functional code.
- This comprehensive guide delves into the realm of variables in C, covering various aspects such as data types, naming conventions, variable declaration, and definition.
- It explores the storage and memory allocation implications of different data types, including integers, floating-point numbers, and more.
- The guide also clarifies the distinction between local and global variables, as well as automatic and static variables
- Additionally, it emphasises the importance of adhering to proper naming conventions to ensure code readability and maintainability.
| Table of Content |
Key Terms: Variables in C, Data, Uppercase, C Language, Rvalue, Lvalue, Primitive Data, Digit
Use Of The Variables In C
[Click Here for Sample Questions]
Variables are storage areas in a code that allow programs to store and manipulate data. In the C language, each variable has a specific type that determines various characteristics, including the layout and size of the memory allocated for the variable, the range of values it can hold, and the set of operations that can be performed on it.
The naming rules for variables in C are as follows:
- The name of a variable can be a combination of digits, letters (both lowercase and uppercase), and underscore characters.
- The name must begin with either an underscore or a letter.
- C is case-sensitive, meaning that lowercase and uppercase letters are distinct, so variables named with different cases are considered separate variables.
For example, the following variable names are valid in C:
- count
- _value
- totalAmount
- MAX_SIZE
- temp_123
However, the following variable names would be invalid:
- 123_var (does not start with a letter or underscore)
- my-variable (contains a hyphen, which is not allowed in variable names)
- SumTotal and sumtotal (C is case-sensitive, so these are considered different variables)
It's essential to follow these naming rules to write clean and readable code in C and avoid any compilation errors or confusion.
Read More: Tree Topology
Rules For Naming A Variable In C
[Click Here for Sample Questions]
It is crucial to abide by certain guidelines when defining variables in C in order to give them names that are valid and meaningful:
- Not a digit, but a letter or an underscore, must begin the variable name.
- Underscores, numbers, and letters—both lowercase and uppercase—can all be used in the variable name.
- The variable's name cannot contain any reserved keywords for the C language, such as float, int, etc.
- There should be no blanks or spaces in the variable name.
- Because C is case-sensitive, variable names are typically written in lowercase to make them easier to read.

Rules For Naming A Variable In C
Data Type Of The Variable
[Click Here for Sample Questions]
In the C language, it is necessary to assign a data type to all variables. Data types define the type of data that can be stored in a variable. If a variable is not provided with a data type, the C compiler will generate a syntax error or a compile-time error.
The C language supports various data types, including:
- float: Used for single-precision floating-point numbers.
- int: Used for storing whole numbers (integers).
- double: Used for double-precision floating-point numbers.
- char: Used for storing individual characters.
- long int: Used for storing larger integer values.
- short int: Used for storing smaller integer values.
Types of Primary/ Primitive Data Types in C Language
[Click Here for Sample Questions]
The types of Primary Data Types in C Language are mentioned below:
| Type of Variable | Name | Description | Uses |
|---|---|---|---|
| char | Character | It is a type of integer. | Used for single alphabets and ASCII character sets. |
| int | Integer | It is the most natural size of an integer. | Used for storing whole numbers. |
| float | Floating-Point | It is a single precision floating-point value. | Used for real number values or decimal points. |
| double | Double | It is a double precision floating-point value. | Used for large-sized numeric values. |
| void | Void | Represents an absence of type. | Used to define functions with no return value. |

In the above code, we have declared and initialise variables of different data types: marks (int), status (char), number (double), and percentage (float). The output of the program will be:

Regarding the example where an incorrect value is assigned to an integer variable:

The output generated will be:
![]()
The C compiler will automatically convert the incorrect value (20.397) to the nearest whole number (20) when assigning it to the integer variable a. This behavior is because integers can only store whole numbers. The compiler performs this conversion silently without issuing any error or warning.
The Data Type Cannot be Changed
That's correct. Once a variable is defined with a particular data type in C, one cannot change its data type later in the same scope. Here's the example provided and the error it would generate:

In the above code, first, define the variable marks as an int and initialise it to the value 20.
- Then, try to redefine marks as a float, which causes a compilation error.
- The error message will be something like "redeclaration of 'marks' with a different type 'float'".
Variable Definition in C
[Click Here for Sample Questions]
In the C language, variable definitions play a crucial role in informing the compiler about the storage size and location required for the given variable. A variable definition also specifies the data type of the variable. The syntax for a variable definition is as follows:
![]()
Here, data_type must be a valid C data type, such as wchar_t, char, float, int, bool, double, or any user-defined object. The variable_list consists of one or more variable names separated by commas.
Valid declarations and definitions of variables include:

For example, the line int p, q, r; both declares and defines the variables p, q, and r, instructing the compiler to create three variables of type int.
Variables can also be initialised during declaration using an initializer, denoted by an equal sign followed by a constant expression:
![]()
Examples of variable declarations and initializations are:

When variables are defined without an initializer, variables with static duration are implicitly initialised with NULL (all bytes set to 0), while the initial values of other variables are not defined.
Also Read:
Declaration of Variable in C
[Click Here for Sample Questions]
Variable informs the compiler that a variable with a given name exists, without providing the complete details about that variable.
- This enables the compiler to proceed with compilation, assuming that the variable will be defined elsewhere during program linking.
- Variable definitions, on the other hand, provide the complete details about the variables during compilation.
- The actual memory allocation and initialization of variables occur during the time of program linking.
- Using variable declarations is particularly useful when working with multiple files.
- By declaring variables with the extern keyword in one file, we indicate that these variables are defined in another file.

Declaration of Variable in C
In this example, the variables p, q, c, and f are declared using extern, indicating that they are defined in another file. Inside the main function, local variables are defined with the same names p and q, which are separate from the external variables.
The output of the code will be:

This is because the local variables p and q inside the main function are used for computation, and their values are distinct from the external variables with the same names. The external variables are defined elsewhere in the program.
Classification of Variables in C
[Click Here for Sample Questions]
In C, variables can be categorised into the following basic types based on their scope and behaviour:
- Global Variable: A variable declared outside any block or function is known as a global variable.
- It is accessible to all functions in the program.
- Global variables must be declared at the beginning of a file, and any function can change its value. Example:

Global Variable
- Local Variable: A local variable is declared inside a block or a function and is only accessible within that block or function.
- Local variables must be declared before they are used and can be initialised with a value. Example:

Local Variable
- Automatic Variable: Every variable declared inside a block in C is by default automatic in nature.
- They are automatically allocated and deallocated when the block is entered and exited, respectively.
- The auto keyword can also be used to explicitly declare automatic variables, although it is not necessary. Example:

Automatic Variable
- Static Variable: Static variables are declared using the static keyword. They retain their value between function calls and are initialised only once during program execution. Example:

Static Variable
- External Variable: An external variable allows sharing a variable across multiple source files in C.
- To declare an external variable, the extern keyword is used.
- External variables are usually declared in one source file and defined in another. Example:

External Variable
These different types of variables serve various purposes and have specific rules and scope throughout the C program.
R values and L values in C
[Click Here for Sample Questions]
An Rvalue refers to the data value that is stored at some memory address. It represents the value of an expression that can appear on the right-hand side (RHS) of an assignment but cannot appear on the left-hand side (LHS).
- Rvalues are essentially temporary values that do not have a memory address.
- Numeric literals are examples of rvalues. They cannot be assigned or modified.
An Lvalue refers to an expression that represents a memory location.
- It can appear on both the RHS and the LHS of an assignment.
- Variables are examples of Values as they have a memory address and can be assigned or modified.
Example:

R values and L values in C
In the first statement, 'g' is an lvalue, and we assign the value 10 to it, which is a valid operation. In the second statement, '30' is an rvalue, and trying to assign a value to it like this is not allowed in C, resulting in a compile-time error.
Read More: Introduction To Array
Things to Remember
- Variables in C are storage areas used to store and manipulate data in the program.
- Variable names in C must follow specific rules, starting with a letter or underscore, containing letters, numbers, and underscores only.
- C is case-sensitive, so variable names with different cases are considered distinct.
- Data types in C define the type of data a variable can hold, such as int, char, float, double, etc.
- Variable definitions in C include specifying the data type and allocating memory for the variable.
- Variable declarations inform the compiler about the existence of a variable without providing complete details.
- Definitions include the full information about the variable and its memory allocation.
Sample Questions
Ques. Which of the following data types in C is used to store single-precision floating-point numbers? (1 mark)
(A) int
(B) float
(C) char
(D) double
Ans: B. float
Ques. Which of the following rules must be followed while naming a variable in C? (1 mark)
(A) The name can start with a digit.
(B) Special characters like @, #, and $ are allowed in the name.
(C) The name should not contain spaces or blanks.
(D) The name can be a combination of digits and letters, but not underscores.
Ans: C. The name should not contain spaces or blanks.
Ques. What is the scope of a local variable in C? (1 mark)
(A) The local variable is accessible from any function within the program.
(B) The local variable is accessible only within the function where it is declared.
(C) The local variable is accessible throughout the entire file where it is defined.
(D) The local variable is accessible from any file within the program.
Ans: B. The local variable is accessible only within the function where it is declared.
Ques. Which of the following data types is used to represent a single character in C? (1 mark)
(A) float
(B) double
(C) char
(D) int
Ans: C. char
Ques. What is the initial value of an automatic variable in C if not explicitly initialized? (1 mark)
(A) 0
(B) 1
(C) The initial value is undefined.
(D) The initial value is the value of the last variable declared in the same scope.
Ans: C. The initial value is undefined.
Ques: Why do we declare a variable in a program? (3 marks)
Ans: Declaring a variable in a program informs the compiler about the existence of a variable with a specific name and data type. It serves as a forward declaration or promise that the variable will be defined somewhere else in the program during linking. Variable declarations are particularly useful when working with multiple files in a program, as they allow sharing variables across different files using the extern keyword.
Ques: Why do we need to define any variable in a program? (2 marks)
Ans: Variable definition in C is essential as it provides complete information to the compiler about the variable, including its data type and memory allocation. When a variable is defined, the compiler reserves memory space for it based on its data type. Additionally, variables can be initialised during definition to assign an initial value.
Ques: What happens if we try to assign a variable with an incorrect value of datatype? (3 marks)
Ans: If we try to assign a variable with an incorrect value of datatype, the compiler will generate a type mismatch error at compile-time. The error occurs when the assigned value does not match the data type of the variable. For example, if we try to assign a floating-point value to an integer variable, the compiler will raise a type error. In some cases, the compiler may automatically perform type conversion to fit the value into the variable's data type, but this may result in a loss of precision.
Ques: In C, what is a local variable's range? (2 marks)
Ans: The block or function where a local variable is declared in C determines the scope of the variable. The variable can only be accessed and utilised in that particular block or function, in other words. The local variable's memory is released when the block or function is terminated, making its value inaccessible.
Ques. What distinguishes a local variable from a global variable in C? (3 marks)
Ans: The scope and lifetime of a local variable and a global variable in C are their primary differences. A local variable is one that is declared and only usable within a certain block or function. As its memory is allocated when the block or function is entered and freed when it is exited, it has a finite lifespan. A global variable, on the other hand, is declared outside of any block or function and can be accessed from anywhere in the program. Due to the fact that its memory is both allocated at programme startup and released at programme termination, it has a longer lifespan. Global variables are more broadly available but may also result in more complex programme behaviour because they can be accessed and changed by any function in the programme.
For Latest Updates on Upcoming Board Exams, Click Here: https://t.me/class_10_12_board_updates
Check-Out:






Comments