C Variables

Used to store and manipulate data.
Each variable can be decalred with a data type that decribes the values it can hold.
Syntax - Declaring a variable
data_type variable_name;

  1. data_type - Specifies the data types that a variable can hold a value i.e; int, float, char, etc...
  2. variable_name - Declares the name of the variale

Syntax - Initializing a variable
data_type variable_name = value;
Here we declare a value in view of the data type.

#include <stdio.h>

int main() {
    int age;  // Declaration
    age = 25; // Initialization

    float height = 1.75; // Declaration and initialization

    char grade = 'A';    // Declaration and initialization
    age = 19; // Modifiying a variable

    return 0;

We can modifiy the variables after we declared or intialize the value for the varibale.
While modifying the type of the value should be same.

Rules for Declaring Variable Names

To declare a variable name correctely here are some rules




Valid Characters

Letters (uppercase and lowercase), digits, underscore

myVar, count_1, Total


Any length, but only the first 31 characters matter

averageScore, temperature2

Reserved Keywords

Cannot be the same as C keywords

int, if, while (invalid)

Case Sensitivity

C is case-sensitive

myVar and myvar are different

Meaningful and Descriptive

Names should convey the purpose of the variable (Not important but recommended)

totalStudents, averageTemperature

No Spaces

Variable names cannot contain spaces

student_age, total Score (Invalid)

#include <stdio.h>

int main() {
    int age;  // Declaration
    // Valid variable names
    int age;
    float average_score;
    char _grade;
    double temperature2;
    int counter123;
    // Invalid variable names 
    int 2ndPlace;        // Starts with a digit 
    char student-age;    // Contains invalid character '-' 
    double average mark; // Contains space 
    float if;            // Reserved keyword

    return 0;

Using Operators

We can use operators while assigning a value for the variable.
We can use addition, subraction, multiplication and more.

#include <stdio.h>

int main() {
    double PI = 3.14159;
    int radius = 5;
    int result = PI*radius;
    printf("The value is: %f\n", result);

    return 0;

Using Constants

Declaring a variable const make the variable values cannot be modified.
const data_type CONSTANT_NAME = value;

#include <stdio.h>

int main() {
    const double PI = 3.14159;

    printf("The value of PI is: %f\n", PI);

    return 0;

Local and Global Variables

We can access varibales in two ways

  1. Local Variables - Variables accessed with in a function or block
  2. Global Variables - Varable accessed outside any function throughout the program
#include <stdio.h>

int globalVar = 10; // Global variable

int main() {
    int localVar = 5; // Local variable

    printf("Global variable: %d\n", globalVar);
    printf("Local variable: %d\n", localVar);

    return 0;

Quick Recap - Topics Covered

C Variabless

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.

Example 1
Example 1 Example 2 Example 3 Example 4 Example 5