Variables in C Programming with Advanced Example

Variable in C is nothing more than a label for a storage location that our applications can alter.

Each C variable has a type that governs the amount and layout of its memory, the range of values that can be stored in that memory, and the set of operations that can be applied to the variable.

A variable’s name can contain letters, digits, and an underscore character. It must commence with a letter or underscore.

What is a Variable?

  • A variable in C is a memory location’s name.

  • It is used for data storage.

  • Its value is modifiable and it can be reused multiple times.

  • It is a method for representing memory location through a symbol in order to make it easily identifiable.

The Rules for Defining the Variables

  • A variable may contain letters, numbers, and underscores.

  • Only the alphabet and the underscore are permitted as variable name initials. It cannot be the first digit.

  • The variable name cannot contain any spaces.

  • Any reserved words or keywords, such as int, float, etc., cannot be used in a variable name.

Basic variable types

Type Description
CharFrequently a single octet (one byte). It is a type of integers.
IntThe most natural size for a machine’s integers.
FloatA value with single-precision floating point precision.
DoubleA value with double precision floating point.
VoidRepresents the lack of a given type.

In addition to Enumeration, Pointer, Array, Structure, Union, etc., the C programming language permits the definition of a variety of other variable types, which we shall discuss in later chapters.

Let us learn basic variable types in this chapter.

Different Types of Variables in C

There are numerous varieties of variables in c:

  • Local variable

  • Global variable

  • Static variable

  • Automatic variable

  • External variable

Local Variable in C

A local variable that is declared inside the function or block.

Local variables are variables that are declared inside a function or block. They can only be utilized by statements within the function or code block.

Local variables are not accessible to other functions. The following example demonstrates the use of local variables.

It must be declared at the start of the block.

void main_function(){  
int a=20;   //local variable  
}  

You must initialize the local variable prior to using it.

Global Variable in C

A global variable is referred to as a variable declared outside of a function or block. Any function may alter the global variable’s value. It is accessible by all functions.

These variables are not restricted to a single function, hence any function may be used to access and modify global variables.

It must be declared at the block’s beginning.

int value=100;  //global variable  
void main_function1(){  
int a=20;  //local variable  
} 

What is a static variable in C?

Static Variable in C refers to a variable declared with the static keyword.

The one allocated statically, whose lifetime spans the duration of the program’s execution.

It is declared with the keyword ‘static’, and its value survives between function calls.

void function1(){  
int a=20;//local variable  
static int b=20;//static variable  
a=a+1;  
b=b+1;  
printf("%d,%d",a,b);  
}  

If this function is called multiple times, the local variable will output the same value for each function call, e.g., 21, 21, 21 etc. However, the static variable will output the incremented value for each function call, such as 21, 22, 23, etc.

Automatic Variable

Automatic variables is specified within a block in C are, by default. Using the auto keyword, we can explicitly specify an automatic variable.

An automatic variable in computer programming is a local variable that is automatically allocated and deallocated as the program flow enters and exits the variable’s scope.

The scope is the lexical context in which a variable is defined, namely the function or block.

void main(){  
int a=20;//local variable (also automatic)  
auto int b=20;//automatic variable  
}  

External Variable

An external variable in the C programming language is a variable defined outside of any function block.

A local (automatic) variable, on the other hand, is a variable defined within a function block.

Variable Definition in C

Variable Definition in C is the location and amount of storage for a variable specified by its definition to the compiler.

A data type is specified in a variable definition, which also includes a list of one or more variables of that type.

type variablefunction_list;

In this case, the type must be one of the recognized C data types, such as char, w char, int, float, double, bool, or any user-defined object.

Variable lists may have one or more identifier names that are separated by commas. Here are a few legitimate declarations.

int a, b, c;
char c, ch;
float f, price;
double d;

It tells the compiler to create int variables with the names a, b, and c.

The line int a, b, and c; declares and defines the variables a, b, and c.

In their declaration, variables can be initialized (given a starting value).

The equal sign is followed by a constant expression in the initializer, as shown below.

type variablefunctions_name = value;

Examples include:

extern int d = 20, f = 25; // declaration of d and f.
int d = 20, f = 25; // definition and initializing d and f.
byte q= 45; // definition and initializes q.
char y= 'y'; // the variable y has the value 'y'.

Variable Declaration in C

A Variable Declaration in C assures the compiler that a variable with the specified type and name exists, allowing the compiler to proceed with compilation without requesting detailed information about the variable.

A variable’s meaning is only determined at the time of compilation; the compiler requires the real variable definition at the time of linking.

A variable declaration is beneficial when many files are used and the variable is defined in one of the files that will be available at the time of program linking.

Use the term extern to declare a variable in any location.

A variable can be declared numerous times in a C program, but it can only be defined once per file, function, or block of code.

Example Variable Declaration in C

Consider the following example, in which variables are declared at the top but defined and initialized within the main function.

#include <stdio.h>
// Variable declaration:
extern int w, x;
extern int y;
extern float z;
int main () {
   /* variable definition: */
   int w, x;
   int y;
   float z;
 
   /* actual initialization */
   w = 20;
   x = 40;
  
   y = w + x;
   printf("value of y : %d \n", y);
   z = 70.0/3.0;
   printf("value of z : %f \n", z);
 
   return 0;
}

When the above example code is compiled and executed, the following result is produced:

value of y : 60
value of z : 23.333334

A similar idea applies to function declarations, where you offer a function name at the time of declaration and its actual definition can be provided elsewhere.

As an example,

You can test the above example here! ➡ C Online Compiler

// function declaration
int functions();
int main() {
   // function call
   int x = functions();
}
// function definition
int functions() {
   return 0;
}

Conclusion

This tutorial taught us how to create variables in C, how to give them values, and what happens when a variable is created.

While establishing a new variable, we also gained insight into the significance of datatype.

In the following post, I’ll create a Constant in C and try to explain its many components in full detail.

I hope you enjoy this post on Variables in C, in which I attempt to illustrate the language’s core grammar.


Leave a Comment