C Variables

In this lesson, you will learn about Variables in C Programming Language, with examples to better understand the topic.


What is a Variable?

A variable is a container that stores the memory location, and a variable holds a unique name to identify that memory location. In C programming, the variable stores the data in the memory location that can be changed or reused.


How to create a variable?

To learn how to create the variable, let’s look at the example below:

Example

#include <stdio.h>
int main()
{
  int mathScore = 100;
  printf("%d", mathScore );
  return 0;
}

Output

100

In this example, we declare a variable name mathScore as int(we will go through Data Type in the next lesson), then assign a value to it. This is how variables are created in C.


C Rules for Naming Convention

Naming convention means following the convention for naming variables.

Reason for following the Naming Convention.

Following the naming convention for variables in programming is essential as it makes it easy for code reviewers to read and understand source code. Variable names should be good identifiers. It reduces the effort to understand what is going on in your program and work out with source code rather than struggling over confusing variable names.
C has a few rules for naming convention of variables as given below.

  • Variable names in C cannot begin with a number. It begins with a letter or the underscore
    character.
  • Variable names in C can only contain alpha-numeric characters and underscores
    nevertheless, you, characters such as +, %,-, &, etc. are not allowed.
  • Variable names in C are case-sensitive. For instance, books and BOOK are not the same
    variables.
  • Variable names in C have no size limit.
Note
Note: The reason variables can not begin with a numeric is a compiler assumption. The compiler considers a token as a numeric value rather than a variable name if a variable name begins with a number. In contrast, token as a non-numeric variable name assists in incorrectly determining the numeric type.

For instance:

Valid variable names

int marks;
int _marks;
int marks100Perc;

Invalid variable names

int 100;			// start with numeric or only numeric not allowed 
int marks perc;			// space not allowed 
int integer;			//reserved keyword 
int +total;			//a mathematical expression not allowed

Variable Scope

The scope is the range of accessibility of a variable to the source code where it is declared. The term scope means the lifetime of the variable in the program. The scope tells where the variable is declared, utilized, and modified in the code. C variables scope types are given below:

  • Local Variables
  • Global Variables
  • Static Variables

Local Variables

A local variable in C references the function or program in which it is declared. If you declare variables in a program, you can only use them within the same program. The local variable is created at the beginning of the function and initialized before use.

Example

// Program runs Local Variable Scope Example
#include <stdio.h>
 
int main() {
int math_Marks = 10; //local variable 
  printf("%d\n", math_Marks); //variable value outside the localVar() function body
  return 0;
}

Output

10
0

See the above example, math_Marks variable is declared within the localVar() function; thus, math_Marks has a LOCAL SCOPE and can only be accessed within localVar(). However, math_Marks outside localVar() shows different value(0).


Global Variables

Global variables in C are declared, accessed, and changed anywhere in any program function.

Example

// Program runs Global Variable Scope Example
#include <stdio.h>

int math_Marks = 100; //Global  variable
int main() {
  printf("%d\n", math_Marks);
  return 0;
}

Output

100
0

See in the above example, math_Marks variable is declared within localVar() function is declared thus math_Marks has a LOCAL SCOPE and can only be accessed within localVar(). However, math_Marks outside localVar() shows a different value(0) and is global to the program.


Static Variables

A variable simply with the static keyword is called a static variable. Static variables preserve their value between function calls because they cannot be modified outside the function. The STATIC keyword is used with a variable name to make it static. The reason for making variable static is that sometimes you want a value to do some other job in the program, so you share it among all instances of the class without deleting it.

A static variable is linked to a class, not by the object. When we use a static keyword with a variable, it attains value once. And can be used with the same value during the program’s lifetime.

Example

// Static variable Example
#include <stdio.h>

void staticFunc() {
  int math_Marks = 99; //local variable
  static int eng_Marks = 90; //static variable
  math_Marks = math_Marks + 1;
  eng_Marks = eng_Marks + 1;
  printf("%d,%d\n", math_Marks, eng_Marks);
}
int main() {
  staticFunc();
  staticFunc();
  staticFunc();
  return 0;
}

Output

100,91

100,92

100,93

Each time staticFunc() is called, eng_Marks variable preserves the information it held the last time the staticFunc() was executed, whereas math_Marks variable is local to staticFunc(). The local variable will print the same value for each function call, e.g., 100,100,100, and so on. However, the static variable holds the last time information; it will print the incremented value in each function call, e.g., 91, 92, 93.

In the next lesson, you will learn about Constants in the C programming language.