C Programming

What are Variables in C Programming

Programming language enables users to communicate with computers in a way they can understand. However, to accomplish anything meaningful, users need to be able to store and manipulate data. That’s where variables come in, the Variables are an essential concept in programming that allows us to represent and manipulate data in our code. In C programming, variables play a crucial role in defining and storing data in memory, making them a fundamental building block for any program.

In this guide, we will explore the concept of variables in C programming, including their syntax, types, and usage.

What are Variables in C Programming

Variables are used to store data values that can be modified while the program is running. A variable has a data type, which defines the kind of data that can be stored in it, and a name, which is used to identify the variable.

The following table shows some of the common data types in C programming, along with their storage requirements and examples.

Data type Storage Example
char 1 byte Store characters in it like A, C, D
int 2 to 4 bytes Can hold an integer like 2, 450, 560
double 8 bytes Can hold double precision decimal values like 22.35
float 4 bytes Holds a single precision decimal point 2.35
void 0 byte Absence of any type

Note: It’s important to note that the size of an int can vary depending on the system, and may be either 2 or 4 bytes. Likewise, the size of the float may differ between various implementations.

Rules to Name a Variable in C Programming

The below-mentioned rules must be kept in mind while naming a variable in C programming:

  1. Variable names must not start with a digit.
  2. The variable name consists of digits, alphabets, and underscore characters. Blank and spaces are not allowed in a variable name.
  3. The reserved words or keywords like float, and int are not allowed in a variable name.
  4. C is case-sensitive language so upper and lower case are treated differently, try to name the variable in lowercase.

According to the above rules, some examples of valid variable names are:

  • int myNumber;
  • float average_value;
  • char _result;

The following variables are invalid and you cannot declare the variable like these in C programming:

  • int 123abc;
  • float my-value;
  • char first name;
  • double $total;

How to Declare, Define and Initialize the Variable in C Programming

The declaration of the variable must be done before it is used in the program. The declaration is informing the compiler about the variable that exists with the following datatype and is used in the program.

For example, you can declare an integer variable named “age” to store a person’s age:

int age;

You can then assign a value to the variable using the assignment operator:

age = 27;

You can also declare and initialize a variable in a single statement:

int age = 27;

You also define the multiple variables of the same data type in a single line:

int age, DOB;

You can also use variables in an expression where they can be combined with operators to perform calculations or comparisons.

For example:

int a = 5;

int b = 10;

int sum = a + b;

In the above example, the variables a and b are used in an expression whose result is stored in a variable “sum”.

Variables are typically declared and defined within the main function or within other functions defined in the program. However, with the extern keyword, you can declare a variable outside of any function using the following syntax:

extern int a;

Types of Variables in C Programming

Following are the different types of variables in C programming:

1: Local Variable

In C programming, a local variable is a variable that is declared inside a function or block. It can only be accessible within the function or block in which it was defined, and as a result, its scope is limited to that function.

For example:

#include <stdio.h>

int main() {

  int a = 15;
  float f = 5.99;
  char ch = 'z';

  // Print variables
  printf("%d\n", a);
  printf("%f\n", f);
  printf("%c\n", ch);


}

Output

2: Static Variable

In C programming, a static variable is a variable that retains its value between function calls and has a local scope. When a variable is declared as static within a function, its value is initialized only once and it retains its value between function calls.

For example:

#include<stdio.h>

int fun() {

  static int count = 1;
  count++;
  return count;
}
int main() {
  printf("%d ", fun());
  printf("%d ", fun());
  return 0;


}

Output

If the count variable was not static then the output would be “2 2”.

3: Global Variable

In C programming, a global variable is a variable that is declared outside of any function and is accessible to all functions in the program. The global variable’s value can be read and modified by any function.

Here’s an example program of C in which we have used a global variable:

#include <stdio.h>

int my_var = 42; // global variable declaration

int main() {

  printf("The value of the Global Variable is %d\n", my_var);
  return 0;


}

Output

4: Automatic Variable

In C programming, variables declared inside a function are usually treated as automatic variables. Because of the reason that they work as local variables for the function in which they are declared, automatic variables are also known as local variables.

The automatic variable is created and destroyed each time the function is called. This is an optional variable as there is not a big difference between auto and local variables in C programming.

Example:

#include <stdio.h>

int main() {

  int a = 10;
  auto int b = 5; // automatic variable using the 'auto' keyword
  printf("The value of a is %d\n", a);
  printf("The value of b is %d\n", b);
  return 0;


}

Output

5: External Variable

The external variable is another variable type that can be declared once in a program and you can use it in multiple source files. The external variables are also called global variables because you can use them anywhere in your code.

For example:

#include <stdio.h>

extern int a; // declare the variable 'a' as external

int main() {

   printf("The value of a is %d\n", a); // use the external variable 'a'
   return 0;


}

int a = 5; // define the external variable 'a'

Output

Bottom Line

The variable is used to name a memory location that stores data. In C, we can modify the value of the variable and can use it multiple times. Once you have defined a variable type you can change it. It contains the list of one or more than one variables. The variable has five different types including local, static, global, automatic, and external variable. We have discussed the detail of the C variables and provided you examples in the above section of the guide.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.