Basics of C Programming for Beginners — Variables in C (Part-II)

Vijay Aneraye
6 min readDec 25, 2023

--

This tutorial is meant for beginners.In this tutorial , We attempt to understand the variables in the C programming language

Before moving on to this section, please read the first part of the preceding post if you haven’t already.

What Are Variables in C?

  • Variables are containers for storing data values, like numbers and characters.
  • In other words, variable is nothing but a name given to a storage area
  • Variables hold a value that can be modified and reused many times during the program execution.
  • In the C programming language, variables must be declared before they can be used. This tells the compiler how to work with the variable and tells the linker how much space needs to be allocated for it.

Variable Declarations/(Creating) Variables

  • To declare a variable, you need to provide its type and an identifier (name).
  • Syntax:
   type identifier1, identifier2, … identifiern;
  • Examples:
#include <stdio.h> 
int a ;
int b ;
int sum;

Variable Declarations with Definitions

  • There are situations when you’ll want to make sure a variable has a starting value before using it. This may conveniently be completed as part of the declaration of the variable. The basic format for defining and declaring a variable in one go is as follows:
  • Syntax
  type variableName = value;
  • Example
int a = 10, b=20,c=30;

What is the difference between initializing and declaring a variable?

  • Declaring a variable means we define a name for the variable and specify its type .
  • Example
int name, age;
  • When we do assign the variable a value later, there is no need to specify the data type again.
  • Example:
#include <stdio.h>
// declaring variables
int a ;
int b ;
int c;
int main(){
// assigning value here
a = 10;
b = 20;
c = 30;
printf("value of variable a: %d \n",a);
printf("value of variable b: %d \n",b);
printf("value of variable c: %d \n",c);
return 0;
}
  • Declare a variable and assign it a value in same line, this is called initialising the variable.
  • Examples:
int a = 10, b=20,c=30;
  • In summary:
int age; // declaration, create a variable called n capable of holding integer values
int age = 27; // initialisation, creating a variable called n and assigning a value, storing a number in that variable

Rules for naming variables in C

  • A variable name can contain letters (uppercase or lowercase), numbers, or an underscore.
  • Variable names must begin either with a letter or an underscore, for example age and _age are valid.
  • Reserved keywords cannot be used as a variable name in C programming.
  • Variable names are case sensitive, for example age is different from Age.
  • No white spaces are allowed within the variable name.
  • Variables should be declared with their data type. Variable names without data type will generate errors.
  • Example of valid variable names:
int a = 2; 
int sum;
float _ab;
int b20;
  • Example of in-valid variable names:
int 20a; //invalid name because variable name start with number
floar x y; // invalid name because there is space between x and y
int goto; // invalid name banecause goto is keyword in c
b = 2 //is an invalid variable.

Types of Variables in C :

  • Local variables
  • Global variables
  • Static variables
  • External variables
  • Automatic variables

Local Variables:

  • The variable that is declared inside function or inside block is called a local variable.
  • The scope of Local variables is within the defined function only.
  • You cannot use a local variable outside the function (in which it is declared).
  • Examples:
#include <stdio.h> 

void personInformation()
{
// Local Variables of the function
int age = 26;
float height = 5.8;

printf("age is %d \n", age);
printf("height is %f", height);
}

int main()
{
personInformation();
return 0;
}
  • Output
age is 26 
height is 5.800000%
  • Local Scope of Variables in C — Nested Blocks
#include <stdio.h>

int main()
{
int localVariableForMain = 7;
//inner block can access the local variable
{
localVariableForMain = localVariableForMain +10;
printf("localVariableForMain is %d",localVariableForMain);
}

return 0;
}

The inner block is able to access the value of localVariableForMain that’s declared in the outer block, and modify it by adding 10 to it.

  • Output
localVariableForMain is 17                                                                                                                                                                                                         
  • Local Scope of Variables in C — Nested Blocks
#include <stdio.h>

int main()
{
int my_num = 7;
{
int new_innter_block_num = 10;
}
printf("new_num is %d",new_innter_block_num); // compilation error, This is because the variable new_innter_block_num is declared in the inner block and its scope is limited to the inner block
return 0;
}
  • Output:
error: use of undeclared identifier 'new_innter_block_num'
printf("new_num is %d",new_innter_block_num); // compilation error, This is because the variable new_innter_block_num is declared in the inner block and its scope is limited to the inner block
^
1 error generated.
  • Local Scope of Variables in C — Different Blocks
#include <stdio.h>

int main()
{
int my_num = 7;
printf("%d",my_num);
my_func();
return 0;
}

void my_func()
{
printf("%d",my_num); // compilation error this is because my_num variable is declared in main() function and my_num is obnly accessible inside main() method
}
  • Output
error: use of undeclared identifier 'my_num'
printf("%d",my_num); // compilation error this is because my_num variable is declared in main() function and my_num is obnly accessible inside main() method

Global variable:

  • The scope of the global variable is the entire program.
  • They are not defined inside any function and can be used in any function.
  • Examples
#include <stdio.h> 

// Declaring global variable
int globaleNum = 23;

void function1()
{
// Function using global variable a
printf("The number is %d \n", globaleNum);
}

void function2()
{
// Function using global variable a
printf("The number is %d \n", globaleNum);
}

int main()
{
// Calling functions
function1();
function2();

return 0;
}
output:
The number is 23
The number is 23

Static Variable:

  • The static variable is defined using the static keyword.
  • The value of a static variable is initialized only once at the time of program loading and it retains its value until the program terminates.
  • Its scope depends on the area of its declaration. If a static variable is defined within a function, it is a local variable. If declared outside the function, its scope is global.
  • Syntax
static data_type variable_name;
  • Examples
static int count;
  • Local Scope Static Variable in C
#include <stdio.h>

void countCalls(void);

int main()
{
countCalls();
countCalls();
countCalls();

return 0;
}

void countCalls(void)
{
static int count = 0; // static variable

count++;
printf("Function has been called %d times\n", count);
}
Output:
Function has been called 1 times
Function has been called 2 times
Function has been called 3 times
  • Global Scope Static Variable in C
#include <stdio.h>

static int count = 0; // global static variable

void countCalls(void);

int main()
{
countCalls();
countCalls();
countCalls();

return 0;
}

void countCalls(void)
{
count++;
printf("Function has been called %d times\n", count);
}
Output:
Function has been called 1 times
Function has been called 2 times
Function has been called 3 times

External Variable

  • The external variables use the extern keyword before the variable definition.
  • This enhances the variable visibility, and the variable can be used in different C files. It is a global variable. It is not necessary to initialize the variable at the time of its declaration. Its lifetime is the entire program.
  • Syntax:
extern data_type variable_name;

Example:

extern int a; 

Constant Variable

  • The constant variables are special variables in C whose value does not change throughout the program after initialization.
  • It is a read-only variable in C.
  • Initializing a constant variable at the time of variable declaration is mandatory.
  • It uses the “const” keyword before its definition.
  • Syntax:
const data_type variable_name = initial_value;

Example:

const int a = 10;

Conclusion

This article explains all the types of variables used in C programming. We understand the range and lifespan of variables in C as well.

We’ll discuss about the different Data types available in the C programming language in the next section.

Next Part : Date Types in C Programming Language

Thanks for reading and happy coding!

--

--