Variables in C


LIKE MOST PROGRAMMING LANGUAGES

, C is able to use and process named variables and their contents. Variables are simply names used to refer to some location in memory - a location that holds a value with which we are working. It may help to think of variables as a placeholder for a value. You can think of a variable as being equivalent to its assigned value..


  1. Variables in C are memory locations with help of which we can be assigned values and are given names .

  2. To store data in memory for later use,we use variables.

  3. In C, a variable must have to be declared before it can be used.

  4. You can declare Variables at the start of any block of code, but most are found at the start of each function.

  5. Most local variables are destroyed on return from that function and created when the function is called.

  6. A declaration begins with the type, followed by the name of one or more than one variables. 




    You can name a variable anything you like as long as it includes only letters, numbers or underscores and does not start with a number. It is a good idea to keep your variable names less than 32 characters long to save time on typing them out and for compiler compatibility reasons. Variables must always be declared at the top before any other commands are used. Now let's declare an integer variable called a and a character variable called b.:
    int main()
    {
    int a;
    char b;
    return 0;
    }
    C keeps a small set of keywords for its own use only.These keywords are given below:
    auto break case char const continue default do
    double else enum extern float for goto if
    int long register return short signed sizeof static
    struct switch typedef union unsigned void volatile while

    Identifiers in C

    Identifiers" or "symbols" are the names you supply for variables, types,labels, and functions in your program. Identifier names must differ in case and spelling from any keywords. You cannot use keywords as the identifiers; they are reserved for special use only. You create an identifier by specifying it in the declaration of a variable,function,or type. In this example which is given below result is an identifier for an integer variable, and printf and main are identifier names for functions.
    Identifiers provide names for the given language elements:

    1. Functions
    2. Function parameters
    3. Macros and macro parameters
    4. Type definitions
    5. Objects
    6. Labels
    7. Enumerated types and enumerators
    8. Structure and union names
    #include

    int main()
    {
    int result;

    if ( result != 0 )
    printf_s( "Bad file handle\n" );
    }


    Variable declarations

    Variables are of three different types which are as follows:
    1. Global Variable
    2. Local Variable
    3. Static Variable
    4. Global Variable:
      The C programming language has an extensive system for declaring variables of different types. The rules for the more complex types can be confusing at times, due to the decisions taken over their design. The principal decision is that the declaration of a variable should be similar, syntactically, to its use (declaration reflects use) . Declare it outside of all the functions if you want to declare a global variable. The function will use the variable that was declared within it and ignore the global one,if a variable of the same name is declared both within a function and outside of it.
      Local Variable:
      Inside the specific function that creates them,these variables only exist. They are unknown to to the main program and to the other functions. In this case,they are normally implemented using a stack. Local variables cease to exist once if the function that created them is completed. Each time a function is executed or called,they are recreated.
      Variable declarations show up in three places:

      • Outside a function. These declarations declare global variables which are visible throughout the program (i.e. they have global scope). Use of global variables is always a big mistake.

      • In the header of a function in the argument list . These variables are the parameters to the function. They are only visible inside the function body and their local scope), exist only from when the function is called to when the function returns (bounded extent---note that this is different from what happens in some garbage-collected languages like Scheme), and get their initial values from the arguments to the function when it is called.

      • At the start of any block delimited by curly braces only. Such variables are exist only when the containing function is active (bounded extent) and visible only within the block (local scope again). The convention in C is generally to declare all such local variables at the top of a function; this is different from the convention in C++ or Java, which encourage variables to be declared when they are first time used


    The following program demonstrate the use of global and local variables.

    #include

    int counter = 0; /* global because we are outside all blocks.*/
    int func(void);

    main()
    {
    counter++; /* global because it has not been declared within this block */
    printf("counter is %2d before the call to func\n", counter);

    func(); /* call a function. */
    printf("counter is %2d after the call to func\n", counter);
    }

    int func(void)
    {
    int counter = 10; /* local variable because it has declared within this block */
    */ printf("counter is %2d within func\n", counter);
    }


    Using static variables

    Another important feature of the variable scoping is the static variable. In a local function scope,a static variable exists only , but it does not lose its value when program execution leaves this scope. Consider the example which is given below:
    #include

    main()
    {
    Test();
    }
    function Test()
    {
    int a = 0;
    printf("a is %d within func\n", a)
    a++;
    }
    Since every time the function is called it sets a to 0 and prints "0",this function is quite useless . The a++ which increments the variable serves no purpose since as soon as the function exits then a variable disappears. The a variable is declared static to make a useful counting function which will not lose track of the current count:
    #include

    main()
    {
    Test();
    }
    function Test()
    {
    static int a = 0;
    printf("a is %d within func\n", a)
    a++;
    }


    Basic types

    There are 4 basic types of variable in C; they are: char, int, double and float
    Type name Meaning
    char The most basic unit addressable by the machine; typically a single octet. This is an integral type.
    int The most natural size of integer for the machine; typically a whole 16, 32 or 64 bit addressable word.
    float A single-precision floating point value.
    double A double-precision floating point value.



#vpsinghrajput

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment