Basics of Variables in C for programmers | 2 Types of variables

In this article, we will discuss variables in C with examples. Furthermore, we will discuss types of variables and it’s various uses.

So do not skip any part otherwise you may not understand the concepts.

In my last article, we have already discussed what is variables in C, their syntax, lvalues and rvalues. You can read it from here.

Variables in C

  • A variable is a named location in memory that is used to hold a value that can be modified by the program
  • Remember, in C programming language the name of a variable has nothing to do with its data type.

Where Variables Are Declared

Variables can be declared in three places

  • Inside function ( local variables)
  • In the definition of function parameters (formal parameters)
  • Outside of all functions. (global variables)

Here we will discuss only local and global variables.

Local Variables

  • Variables declared inside a function (any function) is called local variables.
  • Local variables are only accessible to the statements inside the function block.
  • Function block begins with the open curly braces ‘{‘ and closed with a closing curly braces ‘}’.
void func1(void)
{
   int x;
   x = 9;
}
void func2(void)
{
  int x;
  x = -19;
}

Code Explanation

  • The integer variable x is declared twice, once in func1( ) and once in func2( ).
  • The x in func1( ) has no relationship to the x in func2( ) because each x is known only to the code within the block in which it is declared.

  • If a variable is declared within an inner block has the same name as a variable declared by an enclosing block, the variable in the inner block hides the outer block variable.
Consider the following code


#include <stdio.h>
int main(void)
{
  int x;
  x = 10;
  if(x == 10) 
  {
     int x; /* this x hides the outer x */
     x = 99;
     printf("Inner x: %d\n", x);
   }
   printf("Outer x: %d\n", x);
   return 0;
}

The output of the above code

Inner x: 99
Outer x: 10

Code explanation

In the above example, the x which is declared within the if block hides the outer x. Thus, the inner x and the outer x are two separate and distinct objects. Once that block ends, the outer x once again becomes visible.

Global Variables

  • These are just opposite of the local variables.

  • Global variables can accessed from any piece of code throughout the program.

  • Can hold their value throughout the program execution.

  • Global variables can be created by declaring the variables outside either the main function or any user defined function.

  • Global variables in C are used when many function in your program use the same data.

  • If a global variable and a local variable have the same name, all references to that variable name inside the code block in which the local variable is declared will refer to that local variable and have no effect on the global variable.

  • Let’s understand the concept by an example.
#include <stdio.h>
int count; /* count is global */
void func1(void);
void func2(void);
int main(void)
{
  count = 100;
  func1();
  return 0;
}
void func1(void)
{
  int temp;
  temp = count;
  func2();  /* function func2() calling */
  printf("count is % d", count); /* will print 100 */
}
void func2(void)
{
  int count;
  for(count=l; count<10; count++)
   putchar('.');
}

Code explanation

  • Neither main( ) nor func1( ) has declared the variable count, both may use it.
  • func2( ), however, has declared a local variable called count .
  • When func2( ) refers to count, it refers to only its local variable, not the global one.

Points to be Noted for Global Variables

  • Avoid using too many global variables.
  • Global variables holds up the memory the entire time your program is being executed.
  • Global variables in C can cause problems in large programs when accidentaly changes in value takes place and it was being used elsewhere in the program.

Static Variable

Source:- edureka
  • Static variables in C are permanent variables.
  • Keyword ‘static‘ is used to declare the static variables
  • Syntax
static return_type variable_name;
For Example, 
    static int sum;
  • Static variables in C retain the values during the function call.
  • This makes static variables useful when you write generalized functions and function libraries that other programmers may use.
  • The static modifier has different effects upon local variables and global variables.

static Local Variables

  • When a static variables in C is declared, a permanent storage is being created by the compiler as much as it creates for a global variables.
  • It’s advantage over the global variable is that, it is known to the local blocks only but its value retains during the function call.

Let’s understand this with an example

An example of a function that benefits from a static local variable is a number-series generator that produces a new value based on the previous one.

You could use a global variable to hold this value. However, each time the function is used in a program, you would have to declare that global variable and make sure it did not conflict with any other global variables already in place.

The better solution is to declare the variable that holds the generated number to be static, as shown here:

int series(void)
{
  static int series_num;
  series_num = series_num + 23;
  return series_num;
}

Code explanation

The variable series_num stays in existence between function calls, instead of coming and going the way a normal local variable would i.e., each call to series( ) can produce a new member of the series based on the preceding number without declaring that variable globally.

static Global Variables

  • static global variable in C instructs the compiler to create a global variable known only to the file in which it is declared.
  • A static global variable has an internal linkage. This means that even though the variable is global, routines in other files have no knowledge of it and cannot alter its contents directly, keeping it free from side effects.
  • The series generator example from the previous section is recoded so that a seed value initializes the series through a call to a second function called series_start( ). The entire file containing series( ), series_start( ), and series_num is shown here:

static int series_num;
void series_start(int seed);
int series(void);
int series(void)
{  
  series_num = series_num+23;
  return series_num;
}
/* initialize series_num */
void series_start(int seed)
{
  series_num = seed;
}

Code explanation

Calling series_start( ) with some known integer value initializes the series generator. After that, calls to series( ) generate the next element in the series.

There are many more concepts if we will go deeper into this.

So let’s conclude our discussion here and will meet again and start doing some real coding.

For any queries or doubts, please do not hesitate to ping me.

4 thoughts on “Basics of Variables in C for programmers | 2 Types of variables”

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Please Reload After Disabling Ad\'s