storage classes arrays & functions

Upload: jenishbhavsarjoshi

Post on 02-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Storage Classes Arrays & Functions

    1/8

    C Programming Functions/ Storage

    Classes

    By: JENISH BHAVSAR C.B.PATEL COMPUTER COLLEGE

    1

    Storage Class

    Scope, Visibility and Lifetime of variables

    Scope: The scope of a variable determines over what region of the program a variable is accessible. Visibility:Refers to the accessibility of a variable from the memory.

    Longevity (Lifetime): refers to the period during which a variable retains a given value during

    execution of a program.

    Storage Class A storage class defines the scope (visibility) and life time of variables and/or functions within a C

    Program. These specifiers precede the type that they modify.

    There are following storage classes which can be used in a C Program:

    o Automatic Variables/ Internal Variables (auto)

    o

    External Variables/ Global Variables (extern)

    o

    Static Variables (static)o Register Variables (register)

    Automatic Variables/ Internal Variables (auto)

    The autostorage class is the default storage class for all local variables.

    The features of a variable defined to have an autostorage class are as under:

    Storage Memory

    Default initial value An unpredictable value, which is often called a garbage value.

    Scope Local to the block in which the variable is defined.

    Life Till the control remains within the block in which the variable is defined.

    Automatic variables are declared inside a function in which they are to be utilized. They are created

    when the function is called and destroyed when the function is exited. Automatic variables are therefore private (local) to the function in which they are declared.

    All variables declared within a function are auto by default even if the storage class auto is not

    specified.

    #include

    void function1(void);

    void function2(void);

    void main( )

    {

    int m = 1000;

    function2();

    printf("%d\n",m); /* Third output */

    }void function1(void)

    {

    int m = 10;

    printf("%d\n",m); /* First output */

    }

    void function2(void)

    {

    int m = 100;

    function1();

    printf("%d\n",m); /* Second output */

    }

    In the above example, int m is an auto variable in all the three functions main(), function1(),function2().

  • 8/11/2019 Storage Classes Arrays & Functions

    2/8

    C Programming Functions/ Storage

    Classes

    By: JENISH BHAVSAR C.B.PATEL COMPUTER COLLEGE

    2

    When executed, main calls function2 which in turn calls function1. When main is active m = 1000;

    when function2 is called next m = 100 but within function2 function1 is called and hence m = 10

    becomes active.

    Hence, the output will be first 10 then call returned to function2 and hence 100 and then callreturned to main and hence 1000 gets printed.

    External Variables/ Global Variables (extern)

    The extern storage class allows to declare a variable as a Global/ External variables.

    The variables of this class can be referred to as 'global or external variables.' They are declared

    outside the functions and can be invoked at anywhere in a program.

    The features of a variable defined to have an externstorage class are as under:

    Storage Memory

    Default initial value Zero

    Scope Global

    Life As long as the programs execution doesnt come to an end. The extern storage class is used to give a reference of a global variable that is visible to ALL the

    program files.

    When we use 'extern' the variable cannot be initialized as all it does is point the variable name at a

    storage location that has been previously defined.

    int fun1(void);

    int fun2(void);

    int fun3(void);

    int x ; /* global */

    void main( )

    {

    x = 10 ; /* global x */

    printf("x = %d\n", x);printf("x = %d\n", fun1());

    printf("x = %d\n", fun2());

    printf("x = %d\n", fun3());

    }

    int fun1(void)

    {

    x = x + 10;

    }

    int fun2(void)

    {

    int x; /* local */

    x = 1;

    return (x);}

    int fun3(void)

    {

    x = x + 10; /* global x */

    }

    In the above example, the variable x is used in all functions but none except fun2, has a definition for

    x.

    Because x has been declared 'above' all the functions, it is available to each function without having

    to pass x as a function argument.

    Further, since the value of x is directly available, we need not use return(x) statements in fun1 and

    fun3.

    However, since fun2 has a definition of x, it returns its local value of x and therefore uses a returnstatement. In fun2, the global x is not visible. The local x hides its visibility here.

  • 8/11/2019 Storage Classes Arrays & Functions

    3/8

    C Programming Functions/ Storage

    Classes

    By: JENISH BHAVSAR C.B.PATEL COMPUTER COLLEGE

    3

    Static Variables (static)

    The value of static variables persists until the end of the program.

    A variable can be declared static using the keyword static. E.g.: static int x;

    A static variable may be either an internal or external type depending on the place of declaration. The features of a variable defined to have an staticstorage class are as under:

    Storage Memory

    Default initial value Zero

    Scope Local to block in which the variable is defined

    Life Value of variable persists between function calls

    Internal static variables are those which are declared inside a function. The scope of internal static

    variables extends upto the end of the function in which they are defined.

    A static variable is initialized only once, when the program is compiled. It is never initialized again.

    An external static variable is declared outside of all functions and is available to all the functions in

    that program.

    #includevoid stat(void);

    void main()

    {

    int i;

    for(i=1; i

  • 8/11/2019 Storage Classes Arrays & Functions

    4/8

    C Programming Functions/ Storage

    Classes

    By: JENISH BHAVSAR C.B.PATEL COMPUTER COLLEGE

    4

    #include

    #include

    void main()

    {

    register int i=10;

    clrscr();

    {

    register int i=20;

    printf("\n\t %d",i);

    }

    printf("\n\n\t %d",i);

    getch();

    }

    Operations on Arrays

    An array is a collection of items which can be referred to by a single name.

    An array is also called a linear data structure because the array elements lie in the computermemory in a linear fashion.

    The possible operations on array are:

    1.

    Insertion

    2. Deletion

    3. Traversal

    4. Searching

    5.

    Sorting

    6. Merging

    7. Updating

    Insertion and Deletion

    Inserting an element at the end of the linear array can be easily performed, provided the memory

    space is available to accommodate the additional element. If we want to insert an element in the middle of the array then it is required that half of the

    elements must be moved rightwards to new locations to accommodate the new element and keep

    the order of the other elements.

    Similarly, if we want to delete the middle element of the linear array, then each subsequent element

    must be moved one location ahead of the previous position of the element.

    Traversing

    Traversing basically means the accessing the each and every element of the array at least once.

    Traversing is usually done to be aware of the data elements which are present in the array.

    After insertion or deletion operation we would usually want to check whether it has been

    successfully or not, to check this we can use traversing, and can make sure that whether theelement is successfully inserted or deleted.

    Sorting

    We can store elements to be sorted in an array in either ascending or descending order.

    Searching

    Searching an element in an array, the search starts from the first element till the last element.The

    average number of comparisons in a sequential search is (N+1)/2 where N is the size of the array. If

    the element is in the 1st position, the number of comparisons will be 1 and if the element is in the

    last position, the number of comparisons will be N.

  • 8/11/2019 Storage Classes Arrays & Functions

    5/8

    C Programming Functions/ Storage

    Classes

    By: JENISH BHAVSAR C.B.PATEL COMPUTER COLLEGE

    5

    Merging

    Merging is the process of combining two arrays in a third array. The third array must have to be large

    enough to hold the elements of both the arrays. We can merge the two arrays after sorting them

    individually or merge them first and then sort them as the user needs.#include

    #include

    void main()

    {

    intch,h[10],n,p,i,t,j;

    printf("\n ENTER ARRAY= ");

    for(i=0;i

  • 8/11/2019 Storage Classes Arrays & Functions

    6/8

    C Programming Functions/ Storage

    Classes

    By: JENISH BHAVSAR C.B.PATEL COMPUTER COLLEGE

    6

    Built-In Functions in C

    Mathematical Functions in STDLIB.H

    1. abs(): returns the absolute value of an integer. (Given number is an integer)

    Syntax: abs(number) E.g.: a = abs(-5) O/p: 5

    2. fabs(): returns the absolute value of a floating-point number. (Given number is a float number)

    Syntax: fabs(number) E.g.: a = fabs(-5.3) O/p: 5.3

    Mathematical Functions in MATH.H

    3. pow(): Raises a number (x) to a given power (y).

    Syntax: double power(double x, double y) E.g. : pow(3, 2) O/p: 9

    4. sqrt() : Returns the square root of a given number.

    Syntax: double sqrt (double x) E.g. : sqrt(9) O/p: 3

    5. ceil() : Returns the smallest integer value greater than or equal to a given number.

    Syntax: double ceil(double x) E.g. y = ceil(123.54) O/p: y = 124

    6. floor() : Returns the largest integer value less than or equal to a given number.

    Syntax: ceil(double x) E.g. y = floor(123.54) O/p: y = 123

    7. log(): Returns the natural logarithm of a given number.

    Syntax:doublelog(double x) E.g. y = log(1.00) O/p: 0.0000

    8. exp(): Returns the value of e raised to the xthpower.

    Syntax:doubleexp(double x) E.g. y = exp(1.00) O/p: 2.718282

    Functions in CTYPE.H

    The ctype header is used for testing and converting characters. A control character refers to a character

    that is not part of the normal printing set.

    Theis...functions test the given character and return a nonzero (true) result if it satisfies the following

    conditions. If not, then 0 (false) is returned.

    1.

    isalnum()Checks whether a given character is a letter (A-Z, a-z) or a digit(0-9) and returns true elsefalse. Syntax: int isalnum(int character);

    2. isalpha()Checks whether a given character is a letter (A-Z, a-z) and returns true else false.

    Syntax: int isalnum(int character);

    3. isdigit()Checks whether a given character is a digit (0-9) and returns true else false.

    Syntax: int isdigit(int character);

    4. iscntrl() Checks whether a given character is a control character (TAB, DEL)and returns true else

    false.Syntax: int iscntrl(int character);

    5. isupper()Checks whether a given character is a upper-case letter(A-Z) and returns true else false.

    Syntax: int isupper(int character);

  • 8/11/2019 Storage Classes Arrays & Functions

    7/8

    C Programming Functions/ Storage

    Classes

    By: JENISH BHAVSAR C.B.PATEL COMPUTER COLLEGE

    7

    6. islower()Checks whether a given character is a lower-case letter(a-z) and returns true else false.

    Syntax: int islower(int character);

    7. isspace() Checks whether a given character is a whitespace character (space, tab, carriage return,

    new line, vertical tab, or formfeed)and returns true else false. Syntax: int isspace(int

    character);

    8. tolower()If the character is an uppercase character (A to Z), then it is converted to lowercase (a to

    z).

    Syntax: int tolower(int character); E.g.: tolower(A) O/p : a

    9. toupper()If the character is a lowercase character (a to z), then it is converted to uppercase (A to

    Z).

    Syntax: int toupper(int character); E.g.: toupper(a) O/p : A

    10.toascii()Converts a character into a ascii value.

    Syntax: short toascii(short character);

    What is the mean of #include ?

    This statement tells the compiler to search for a file stdio.h and place its contents at this point in

    the program. The contents of the header file become part of the source code when it is compiled.

    Full form of the different library header file: -

    File Name Full formstdio.h Standard Input Output Header file. It contains different standard input

    output function such as printf and scanf

    math.h Mathematical Header file. This file contains various mathematical

    function such cos, sine, pow (power), log, tan, etc.

    conio.h Console Input Output Header file. This file contains various function

    used in console output such as getch, clrscr, etc.

    stdlib.h Standard Library Header file. It contains various utility function such as

    atoi, exit, etc.

    ctype.h Character TYPE Header file. It contains various character testing and

    conversion functions

    string.h. STRING Header file. It contains various function related to stringoperation such as strcmp, strcpy, strcat, strlen, etc.

    time.h TIME handling function Header file. It contains various function related

    to time manipulation.

  • 8/11/2019 Storage Classes Arrays & Functions

    8/8

    C Programming Functions/ Storage

    Classes

    By: JENISH BHAVSAR C.B.PATEL COMPUTER COLLEGE

    8

    Different Library function: -

    1. getchar( ): - This function is used to read simply one character from standard input device. The

    format of it is: Syntax: variablename = getchar( ) ;

    e.g char name;name = getchar ( ) ;

    Here computer waits until we enter one character from the input device and assign it to character

    variable name. We have to press enter key after inputting one character, then we are getting the

    next result. This function is written in stdio.h file

    2. getche( ): - This function is used to read the character from the standard input device. The format of

    it is: Syntax: variablename = getche( ) ;

    e.g char name;

    name = getche( ) ;

    Here computer waits until we enter one character from the input device and assign it to character

    variable name. In getche( ) function there is no need to press enter key after inputting one characterbut we are getting next result immediately while in getchar( ) function we must press the enter key.

    This getche( ) function is written in standard library file conio.h.

    3. getch( ): - This function is used to read the character from the standard input device but it will not

    echo (display) the character which you are inputting. The format of it is:

    Syntax: variablename = getche( ) ;

    e.g char name;

    name = getch( ) ;

    Here computer waits until you enter one character from the input device and assign it to character

    variable name. In getch( ) function there is no need to press enter key after inputting one character

    but you are getting next result immediately while in getchar( ) function you must press the enter keyand also it will echo (display) the character which you are entering. This getch( ) function is written in

    standard library file conio.h.

    4. putchar( ): -This function is used to print one character on standard output device. The format of this

    function is: Syntax: putchar(variablename) ;

    e.g. char name;

    name=p;

    putchar(name);

    After executing the above statement you get the letter p printed on standard outputdevice. This

    function is written in stdio.h file.