Table of contents
1.
Introduction
2.
What is Variable in C
3.
C Variable Syntax
3.1.
Example
4.
Basic Variable Types 
4.1.
1. char
4.2.
2. int
4.3.
3. float
4.4.
4. double
4.5.
5. void
5.
Variable Declaration in C
5.1.
Example
5.2.
C
6.
How to Use Variables in C?
6.1.
Steps to Use Variables
6.2.
Example
6.3.
C
7.
Rules for Naming Variables in C
8.
C Variable Types
9.
Lvalues and Rvalues in C
9.1.
Lvalue
9.2.
Rvalue
10.
Frequently Asked Questions
10.1.
How to represent variables in C?
10.2.
How to declare variables?
10.3.
How to access Variables in C?
11.
Conclusion
Last Updated: Jan 2, 2025
Easy

Variables in C

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A variable is a name given to a storage location that our programs can control. Each variable in C has a particular kind, which determines the size and layout of the variable's memory, the range of values saved inside that memory, and the set of operations applied to the variable.

Variables in C Language

What is Variable in C

A variable definition tells the compiler how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or extra variables of that type as follow: 

type variable_list;


Valid declarations are shown here −

int x, y, z;
char c;
float f;
double d;


The line int x, y, z; declares and defines the variables x, y, and z, which instructs the compiler to create variables named x, y, and z of type int.

Variables can initialize in their statement. using a constant expression as follows −

type variable_name = value;

C Variable Syntax

data_type variable_name;


Explanation

  • data_type: Specifies the type of data the variable will store (e.g., int, float, char).
     
  • variable_name: The name used to reference the variable in the program.


Example

int age;      // Declares an integer variable named 'age'
float salary; // Declares a floating-point variable named 'salary'
char grade;   // Declares a character variable named 'grade'

Basic Variable Types 

1. char

C uses char kind to store characters and letters. The char type is integer type because C stores integer numbers rather than characters. In C programming, char values are stored in 1 byte in memory, ranging from -128 to 127 or 0 to 255.

2. int

Int, short for "integer," is a basic variable type built into the compiler and used to define numeric variables retaining whole numbers. ... C, C++, C# and many different programming languages understand int as a data type.

3. float

Float is a shortened period for "floating point." using the definition. It is a fundamental data type built into the compiler. It is indeed used to define numeric values with floating decimal points. C, C++, C#, and many different programming languages understand float as a data type.

4. double

A double is a data type in c language that stores high-precision floating-point data or numbers in computer memory. It is called double data type because it can hold the double size of data compared to the float data kind. A double has 8 bytes, the same as 64 bits in length.

5. void

In computer programming, while void is used as a function return type, it indicates that the function does now not return a value. While void appears in a pointer statement, it specifies that the pointer is universal. when used in a function's parameter listing, void indicates that the function takes no parameters.
 

Also see, Short int in C Programming

Variable Declaration in C

A variable declaration presents a guarantee to the compiler that there exists a variable with the given type and name so the compiler can continue for a similar compilation without requiring the complete detail about the variable. A variable definition has its that means at the time of collection only. The compiler desires actual variable definition at the time of linking this system.

A variable declaration is helpful when you use multiple files, and also you define your variable in one of the files so that it will be available at the time of linking the program. You will be using the keyword extern to declare a variable at any place. Although you can declare a variable multiple times to your C program, it could be described only once in a particular file, a function, or a block of code.

Also read - Bit stuffing program in c

Example

  • C

C

#include <bits/stdc++.h>
using namespace std;
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main ()
{
  /* variable definition: */
  int a, b;
  int c;
  float f;
  /* actual initialization */
  a = 10;
  b = 20; 
  c = a + b;
  printf("value of c : %d \n", c);
  f = 70.0/3.0;
  printf("value of f : %f \n", f);
  return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

value of c: 30
value of f: 23.333334

How to Use Variables in C?

Variables in C are used to store data that can be manipulated or accessed throughout a program. To use a variable, you must declare it, initialize it, and optionally modify it.

Steps to Use Variables

  1. Declare: Define the variable with its type.
  2. Initialize: Assign a value to the variable.
  3. Use: Perform operations or display the variable's value.

Example

  • C

C

#include <stdio.h>
int main() {
int age = 25; // Declaration and initialization
printf("Age: %d", age); // Using the variable
return 0;
}
You can also try this code with Online C Compiler
Run Code

Rules for Naming Variables in C

When naming variables in C, follow these rules to ensure proper functionality and readability:

  • Variable names must start with a letter or an underscore (_) but cannot begin with a digit.
  • Variable names can contain letters, digits, and underscores but no special characters.
  • Reserved keywords of C (e.g., int, return) cannot be used as variable names.
  • Variable names are case-sensitive (age and Age are different).
  • Choose descriptive names for better readability, such as totalMarks instead of tm.

C Variable Types

C provides several types of variables to store different kinds of data. Here are the primary types:

  1. Integer (int): Stores whole numbers.
    Example: int age = 30;  
    printf("Age: %d", age);
  2. Floating-point (float): Stores decimal numbers.
    Example: float price = 99.99;  
    printf("Price: %.2f", price);
  3. Double (double): Stores large decimal numbers with double precision.
    Example: double distance = 12345.6789;  
    printf("Distance: %.4lf", distance);
  4. Character (char): Stores a single character.
    Example: char grade = 'A';  
    printf("Grade: %c", grade);
  5. Boolean (_Bool): Stores true (1) or false (0).
    Example: #include <stdbool.h>
    _Bool isPassed = 1;  
    printf("Passed: %d", isPassed);
  6. Void (void): Represents no value, often used for functions with no return type.
    Example: void displayMessage() {
       printf("Hello, World!");
    }

Lvalues and Rvalues in C

Two types of expressions in C −

Lvalue

"l-value" refers to a memory location that is identifies an object. l-value can also seem as both left-hand or right-hand sides of an assignment operator(=). l-value frequently represents an identifier—a facet of an assignment.

Rvalue

Rvalue" refers to the data value saved at a few addresses in memory. A rvalue is an expression that couldn't have a value assigned to it. This means that rvalue can seem on the right but not on the left-hand side of an assignment operator(=).
You can also read about the memory hierarchy, 

Frequently Asked Questions

How to represent variables in C?

  • Variable call ought to start with a letter or underscore.
  • Variables are case sensitive
  • They may be constructed with digits letters.
  • No special symbols are allowed other than underscore.
  • sum, height, _value are a few examples for variable name

How to declare variables?

  • Variables must be declared in the C program before applying.
  • Memory space isn't allocated for a variable at the same time as a declaration. It takes place only on the variable definition.
  • Variable initialization approach assigning a value to the variable.

How to access Variables in C?

Variables that might be declared inside a function or block are known as local variables. They can be used most effectively by means of statements that can be inside that function or block of code. Local variables aren't recognized to functions outside their personal.

Conclusion

Variables in C play a fundamental role in programming, serving as containers for data that can be manipulated and accessed throughout a program. By understanding how to declare, initialize, and use variables effectively, programmers can write efficient and organized code.

Live masterclass