Reading:  

Getting started with C Language


Working with Variables and Command line in C

Scope and life time of a variable

Storage classes

There are four storage classes in C language. The variables behavior depends upon these storage classes. They are

  • Automatic
  • External
  • Static
  • Register

The scope of a variable determines what parts of the program the variable is actually available. The scope is broadly divided in to two. They are

  1. Global
  2. Local

 If the scope of the variable is the entire program then it is called a global variable and if the scope is only internal or to any block then it is called a local variable. We can access a global variable anywhere from the program. But if it is a local variable it is only accessible within that block or function only. 

Automatic variable

Automatic variables are declared inside a function, where it is utilized.  These variables are created when a function is called and destroyed automatically when the function is exited. This automatic creation and destroying gives the name automatic variables. Thus these variables are private to those functions.  These automatic variables are also called internal or local variables.  A variable declared inside a function is an automatic variable.

Example :

main ()
{
int a;
……..
……..
}

By default a is an automatic variable.

The keyword 'auto' is used to declare an automatic variable.

Example :

main ()
{
Auto int a;
……..
……..
}

 

 

External variables

The variables that are both alive and active throughout the entire program are called external variables. External variables are also called global variables. Thus it is alive we can access these variables from anywhere in the program using any function.  The declaration is outside the functions.

int a;
main ()
{
………
...…..
}
Function1 ()
{
……….
……….
}
Function1 ()
{
……….
……….
}

The keyword ‘extern’ is used along with declaration.

Syntax :

extern int a;
………….
………….
Example :
main ()
{
extern int a;
… ..
… ..
}

Static variables

As the name itself the value of static variable persists until the end of the program. The keyword 'static' is used with the variables to make them static variables.

Syntax :

static int a;

Static  variables are divided into two. They are

  • Internal static variables
  • External static variables

Internal static variables

Internal static variables are those which are declared inside a function. The scopes of internal static variables extend up to the end of the function in which they are defined. Thus the internal static variables are similar to that of automatic variables except the existence. Static variables can be used to retain values between function calls. if a static variable is declared once when the program is compiled  it is never initialized again.

External static variables

An external static variable are declared outside of all functions and is available to all the functions in that program. The difference between static external variables and ordinary global variables are that static external variables  are available only within the file where it is defined while the normal global variable  can be accessed by other files. 

Register variables   

We can tell the compiler that a variable should be kept in one of the machine’s registers , instead of keeping in the memory ( where normal variables stored ). Since a register access is much faster than a memory access. Keeping the frequently accessed variables in the register will lead to faster execution of  programs.

Syntax :

register int a;

only a few variables can be kept in register so keep the variables in register carefully.

Command line arguments

Using command line arguments the main() function of a C program accepts arguments from command line or from other shell scripts and operating systems like DOS or Linux.  To use command line arguments we must understand the declaration of the main function which accepts the arguments. The main function accepts the two arguments one argument is number of command line arguments, and the other argument is a full list of all of the command line arguments.

  • argc
  • argv[]

where,

argc      - Number of arguments in the command line including program name

argv[]   – This is carrying all the arguments

Syntax  :

int main ( int argc, char *argv[] )

The argv[] is an array of character pointers is the listing of all the arguments. argv[0] is the name of the program, if the name is not available it gives an empty string. After that, every element number less than argc is a command line argument. We can use each argv element just like a string, or an array.

 

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!