Variables in C | Set 1

By | February 13, 2023

Variables in C

In C, a variable is a storage location that has a name and holds a value. A variable must be declared before it is used in a program. The syntax for declaring a variable in C is:

data_type variable_name;

where data_type is the type of the data that the variable will store, and variable_name is the name of the variable. For example:

int age;
float height;
char grade;

Once a variable is declared, you can assign a value to it. The assignment operator in C is =. For example:

age = 25;
height = 1.75;
grade = 'A';

You can also declare and assign values to a variable in the same statement:

int age = 25;
float height = 1.75;
char grade = 'A';

It is important to choose meaningful names for your variables so that your code is easy to understand. Variable names can consist of letters, digits, and underscores, but they cannot start with a digit. Additionally, there are some reserved words in C that cannot be used as variable names, such as int, float, while, etc.

Declare and assign values

Here are some examples of declaring and using variables in C:

#include <stdio.h>

int main() {
  int age;
  float height;
  char grade;

  age = 25;
  height = 1.75;
  grade = 'A';

  printf("Age: %d\n", age);
  printf("Height: %f\n", height);
  printf("Grade: %c\n", grade);

  return 0;
}

This program declares three variables, age, height, and grade, of type int, float, and char respectively. Then, it assigns values to these variables, and finally, it prints the values of these variables using the printf function.

You can also declare and assign values to variables in the same statement:

#include <stdio.h>

int main() {
  int age = 25;
  float height = 1.75;
  char grade = 'A';

  printf("Age: %d\n", age);
  printf("Height: %f\n", height);
  printf("Grade: %c\n", grade);

  return 0;
}

This program does the same thing as the previous example, but it declares and assigns values to variables in the same statement.

Naming rules of variables in C :

Here are the rules for naming variables in C:

  1. Variable names can only contain letters (both uppercase and lowercase), digits, and underscores:

    int student_age;
    float height_in_meters;
    char first_initial;
    
    
  2. Variable names must begin with a letter or an underscore:
    int age;
    float _height;
    char firstInitial;
    
    
    
  3. Variable names cannot contain spaces or special characters, such as @, #, $, etc.
    // This is not allowed in C
    int student age;
    
    // This is allowed in C
    int student_age;
    
    
  4. Variable names cannot be a reserved word in C, such as int, float, while, etc.
    // This is not allowed in C
    int int;
    
    // This is allowed in C
    int student_age;
    
    
  5. Variable names should be descriptive and meaningful:
    int student_age;
    float height_in_meters;
    char first_initial;
    
    
  6. It is a good practice to use camelCase for variable names:
    int studentAge;
    float heightInMeters;
    char firstInitial;
    
    
  7. Some compilers are case-sensitive, so age and Age are considered to be two different variables.
    int age;
    int Age;
    
    

    These are two different variables in C.

  8. Variable names should not be too long. A good rule of thumb is to keep them under 15 characters.
    // This is allowed in C
    int student_age;
    
    // This is not recommended
    int student_age_when_he_was_born;
    
    

By following these rules, you can ensure that your variable names are meaningful, easy to understand, and follow best practices.

Read next related article – Variables in C | Set 2

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.