Skip to content

Data Type Qualifiers and Storage Class Specifier

Rafiul Islam edited this page Nov 2, 2018 · 8 revisions

Topic

  • Variable Scope
    • Local
    • Global
  • Type Qualifiers
    • const
    • volatile
  • Storage Class
    • extern
    • auto
    • static
    • register

Variable Scope

Local variable

Variable that hasn't any reference outside of the block. Local variable can override the same variable name in locally. Local variables are initialize with garbage value.

Global variable

Variable those are available for any block of the code file. Generally global variable are defined at top most section of the code file. But it can be define any where else in code file. Global variables are initialize with 0(integer) or NULL(characters)

#include <stdio.h>

/*
    global declaration area
*/
char x[] = "global"; // define a global variable

void func(); // function prototypes

int main()
{
    puts(x); // access the global 'x'

    char x[] = "main function"; // override 'x' with new declaration
    puts(x); // override the 'x' as main function local

    func(); // function call

    return 0;
}

void func()
{
    puts(x); // access the global 'x'

    char x[] = "function"; // override 'x' with new declaration
    puts(x);

    label:{
        // new label block
        char x[] = "label"; // override 'x' again
        puts(x);
    }

    puts(x);
}
/*
    output:
    -------
    global
    main function
    global
    function
    label
    function
*/

Type Qualifier

const generally declare for global variable; this state that this variable can't be changed by program or program code.

const int month = 30;

volatile keyword assign for indicate that a variable can change by the system. A const variable also assign with volatile.

const volatile int port = 1202; // constant port address that can be changed by the system

Storage Class Specifier

  • A declaretion declare the name and the type of an object
  • A definition cause the storage to be allocated for the object object may have many declaration but there can be only one definition.

extern

The principal of extern is to specify that an object is declared with external linkage elsewhere in the program. Variable may be defined letter in the same code file (globally) or any other file of the program (Project files).

illigle or garbage:
#include <stdio.h>

int main()
{
    char s[];
    puts(s);
    return 0;
}
char s[] = "Hello";
correct:
#include <stdio.h>

int main()
{
    extern char s[];
    puts(s);
    return 0;
}
char s[] = "Hello";
extern checking sequence current file -> other files of ame project

static

static variables are initial with zero

Variables declared with static are the parmanent variable of their block or file. General local variables are predefined as auto which indicate that the variable will create and release in a block each time the block called and act like a fresh declared value.

When a local variable assigned with static keyword the variable parmently declared for this local block and each time it can remember its previous value.

#include <stdio.h>

void func()
{
    int x = 0; // initialize x as a local value with 0
    printf("Init: %d\n",x); // print the  initial value
    x = x + 10; // update value by adding 10
    printf("Update: %d\n",x); // print the updated value
    return;
}

int main()
{
    /// calling the function 3 times
    func();
    func();
    func();

    return 0;
}
/*
    outputs:
    ---------
    Init: 0
    Update: 10
    Init: 0
    Update: 10
    Init: 0
    Update: 10
*/

When x is defined as a local value then every time the program call the func it will create a new x with initial value 0 and updated 10. After a single func call executed x will release as garbage value.

using static
#include <stdio.h>

void func()
{
    static int x = 0; // initialize x as a locally global value with 0
    printf("Init: %d\n",x); // print the  initial value
    x = x + 10; // update value by adding 10 with previous updated 'x'
    printf("Update: %d\n",x); // print the updated value
    return;
}

int main()
{
    /// calling the function 3 times
    func();
    func();
    func();

    return 0;
}
/*
    outputs:
    ---------
    Init: 0
    Update: 10
    Init: 10
    Update: 20
    Init: 20
    Update: 30
*/

When a local variable defined with static it will act like locally global value, that means each time the func call for execution it will remember the previous updated value of the x and assign it to current call initial value and never release the x till the program alive.

When a global variable assigned with static that means this variable can't be accessed by other file as extern storage. Simply this is the private variable for data for its file (concept of OOP).

auto

auto storage type only for local variables, this can't be use with a global one. auto is simply opposite of the locally global or static for local variables. auto refer that a variable will create freshly and release after its block code executed each time. Formally all the local variables are defined with auto which assigned by the compiler; so, programmer doesn't need to assign it every time.

    int x = 10; // define x implicitly auto
    auto int y = 20; // define y explicitly auto

register

register keyword allow to store a variable in CPU register instead of RAM for fast access. Generally counter and commonly access variables are stored in register. As for this keyword variable stored in CPU register, those variable doesn't have any memory location or pointer. Because they are not in memory.

    register int i; // counter index store in register
    for(i=0; i<10; i++){
        printf("%d ",i);
    }

Register can store only integer, character and pointer. So careful before use, as because for other variable types the variable doesn't actually stored in register. Although CPU has a limited number register, when register classified variables are overloaded c compiler doesn't store overloaded variables. So, don;t worry about any error, c compiler will handle all this stuffs

😎