C Language

C language



CONTINUE Statement in C
continue statement is a jump statement. The continue statement can be used only inside for loop, while loop and do-while loop. Execution of these statement does not cause an exit from the loop but it suspend the execution of the loop for that iteration and transfer control back to the loop for the next iteration.

BREAK Statement
The break statement is a jump instruction and can be used inside a switch construct, for loop, while loop and do-while loop. The execution of break statement causes immediate exit from the concern construct and the control is transferred to the statement following the loop. In the loop construct the execution of break statement terminates loop and further execution of the program is reserved with the statement following the body of the loop.

================================================================



STORAGE CLASSES

A storage class provides information about the scope (visibility) and lifetime of variables in a C program.
(A scope specifies the part of the program which a variable name is visible, that is the accessibility of the variable by its name. )
The storage class decides the portion of the program within which the variables are recognized.
The storage class determines the part of memory where storage is allocated for an object (particularly variables and functions) and how long the storage allocation continues to exist.
These specifiers precede the type that they modify.
There are following storage classes which can be used in a C program
  • auto
  • register
  • static
  • extern
The auto Storage Class
The auto storage class is the default storage class for all local variables.
Automatic variable is also called as local variable.
They are declared at the start of a program’s block such as in the curly braces ( { } ).  Memory is allocated automatically upon entry to a block and freed automatically upon exit from the block.
The scope of automatic variables is local to the block in which they are declared, including any blocks nested within that block. For these reasons, they are also called local variables.
No block outside the defining block may have direct access to automatic variables (by variable name).
Automatic variables may be specified upon declaration to be of storage class auto.  However, it is not required to use the keyword auto because by default, storage class within a block is auto.
{
   int mount;
   auto int month;
}
The example above defines two variables with the same storage class, auto can only be used within functions, i.e. local variables.

The register Storage Class
    Automatic variables are allocated storage in the main memory of the computer; however, for most computers, accessing data in memory is considerably slower than processing directly in the CPU.
    Registers are memory located within the CPU itself where data can be stored and accessed quickly.  Normally, the compiler determines what data is to be stored in the registers of the CPU at what times.
    However, the C language provides the storage class register so that the programmer can suggest to the compiler that particular automatic variables should be allocated to CPU registers, if possible.
    Variables which are used repeatedly or whose access times are critical may be declared to be of storage class register.
The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location).
Default value: If we don’t specify the value to the variable, then this will accept the Default value as a Garbage Value.
Local Scope: The variables those are declared as Register have a Local Scope Means we can Access the Value only that Location, where we have declared that variable.
{
   register int  miles;
}
The register should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.

The extern Storage Class
The extern storage class is used to give a reference of a global variable that is visible to ALL the program files.
    Declaration for external variable is as follows:
extern int var;
External variable are defined outside any function and memory is set aside for this type of variable once it is declared and remained until the end of the program.
These variables are also called global variables. These are declared outside the body of the function.
For most C implementations, an external variable is initialized to zero.
/* storage class and scope */
#include <stdio.h>

void funct1(void);
void funct2(void);

/* external variable, scope is global to main(), funct1() and funct2(), extern keyword is omitted here, coz just one file */
extern int globvar = 10;

int main()
{
    printf("\n****storage classes and scope****\n");
    /* external variable */
    globvar = 20;
    
    printf("\nVariable globvar, in main() = %d\n", globvar);
    funct1();
    printf("\nVariable globvar, in main() = %d\n", globvar);
    funct2();
    printf("\nVariable globvar, in main() = %d\n", globvar);
    return 0;
}

/* external variable, scope is global to funct1() and funct2() */
extern int globvar2 = 30;

void funct1(void)
{
    /* auto variable, scope local to funct1() and funct1() cannot access the external globvar */
    char globvar;
   
    /* local variable to funct1() */
    globvar = 'A';
    /* external variable */
    globvar2 = 40;
   
    printf("\nIn funct1(), globvar = %c and globvar2 = %d\n", globvar, globvar2);
}

void funct2(void)
{
    /* auto variable, scope local to funct2(), and funct2() cannot access the external globvar2 */
    double globvar2;
    /* external variable */
    globvar =  50;
    /* auto local variable to funct2() */
    globvar2 = 1.234;
    printf("\nIn funct2(), globvar = %d and globvar2 = %.4f\n", globvar, globvar2);
}

Output:



The static Storage Class
static is the default storage class for global variables.
The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.
    Static storage class can be specified for automatic as well as external variables such as:
static extern varx;
The static automatic variables, as with local variables, are accessible only within the function in which it is defined. Static automatic variables exist until the program ends in the same manner as external variables.
Default value: if we don’t specify the value to the variable, then this will accept the Default value as a 0.




EXAMPLE:
/* Program which sums integers, using static variables */

#include <stdio.h>

void sumIt(void);

void main()
{
   int i =0;

   printf("Enter 5 numbers to be summed\n");

   for(i = 0; I < 5; ++i)
         sumIt();

   printf("Program completed\n");
}

void sumIt(void)
{
    static int sum = 0;
     int num;

     printf("\nEnter a number: ");
     scanf("%d", &num);

     sum+=num;

     printf("The current sum is: %d",sum);

}

 

================================================================


FUNCTIONS
A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.
Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A function is a self contained block of statements that perform a task of same kind.
The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location within a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.
Structure of a Function
There are two main parts of the function. The function header and the function body.









int sum(int x, int y)
{
            int ans = 0;   //holds the answer that will be returned
            ans = x + y;   //calculate the sum
            return ans                //return the answer
}

 

Function Header
 









Function Body
 


 

Function Header
In the first line of the above code
int sum(int x, int y)
It has three main parts
1.     The name of the function i.e. sum
2.     The parameters of the function enclosed in parenthesis
3.     Return value type i.e. int
Function Body
Whatever is written with in { } in the above example is the body of the function.
Function Prototypes
The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like
int sum (int x, int y);
The only difference between the header and the prototype is the semicolon ; there must the a semicolon at the end of the prototype.
Why Functions Are Used?
Two reasons
1.     Writing functions avoids rewriting the same code over and over.
2.      Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code in to modular functions also makes the pro-gram easier to design and understand.
Function Declaration
void starline ( );

Calling the Function

A Function call looks like:
starline ( );
Function Definition
The definition contains the actual code for the function.
void starline ( )
   {
   for ( intj=0, j < 45; j++ )
   cout << "*" ;
   cout << endl;
   }

There are basically two types of functions
·        Library functions Ex. printf ( ), scanf ( ) etc.
·        User defined function

Any function by default returns an int value.
Categories:
Ø Functions with no return type and no argument
Ø Functions with return type and no argument
Ø Functions with no return type and arguments
Ø Functions with return type and arguments



Passing Parameters to a Function
There are two ways to pass parameters to a function:
  • Pass by Value: mechanism is used when you don't want to change the value of passed paramters. When parameters are passed by value then functions in C create copies of the passed in variables and do required processing on these copied variables.
  • Pass by Reference mechanism is used when you want a function to do the changes in passed parameters and reflect those changes back to the calling function. In this case only addresses of the variables are passed to a function so that function can work directly over the addresses.
Pass By Value:

EXAMPLE:

#include <stdio.h>

/* function declaration goes here.*/
void swap( int p1, int p2 );

int main()
{
   int a = 10;
   int b = 20;

   printf("Before: Value of a = %d and value of b = %d\n", a, b );
   swap( a, b );
   printf("After: Value of a = %d and value of b = %d\n", a, b );
}

void swap( int p1, int p2 )
{
    int t;

    t = p2;
    p2 = p1;
    p1 = t;
   printf("Value of a (p1) = %d and value of b(p2) = %d\n", p1, p2 );
}



OUTPUT:
Before: Value of a = 10 and value of b = 20
Value of a (p1) = 20 and value of b(p2) = 10
After: Value of a = 10 and value of b = 20


PASS BY REFERENCE:

Example:
#include <stdio.h>
 
/* function declaration goes here.*/
void swap( int *p1, int *p2 );
 
int main()
{
   int a = 10;
   int b = 20;
 
   printf("Before: Value of a = %d and value of b = %d\n", a, b );
   swap( &a, &b );
   printf("After: Value of a = %d and value of b = %d\n", a, b );
}
 
void swap( int *p1, int *p2 )
{
    int t;
 
    t = *p2;
    *p2 = *p1;
    *p1 = t;
   printf("Value of a (p1) = %d and value of b(p2) = %d\n", *p1, *p2 );
}


OUTPUT:
Before: Value of a = 10 and value of b = 20
Value of a (p1) = 20 and value of b(p2) = 10
After: Value of a = 20 and value of b = 10


 

 

No comments:

JAVA Project Specifications Lecture Notes: SE OS JAVA

X