c Language by SRIDHAR

Embed Size (px)

Citation preview

  • 7/28/2019 c Language by SRIDHAR

    1/144

    ** 'C'

    => Programming Language

    => Middle Level Language

    => Case Sensitive Language

    (Recognisation of upper and lower case

    alphabets will be different)

    ASCII Values

    (American Standard Code for Information Interchange)

    0 to 255

    Software:

    "Set of related Programs"

    1. Operating Systems

    "Acts as an interface media between the user and the system"

    2. Packages

    "Readymade set of programs"

    Ex: MS office

    3. Languages

    " No readymade programs, instead we

    have to create our own programs"

    Ex: 'C'

    Language:

    "Communication media"

    Binary / Machine Language( 0's & 1's)

    => Programming Languages:

    These are used as media by the programmer to interact / instruct the system.

  • 7/28/2019 c Language by SRIDHAR

    2/144

    1. Low Level Languages

    => Easy only for machines

    Ex: Assembly Language

    2. Middle Level Languages

    => Easy for both -> interface between low & High

    Ex: 'C' Language

    3. High Level Languages

    => Easy only for human beings

    Ex: BASIC, COBOL, PASCAL, C++, JAVA

    => 'C' Character Set:

    => Alphabets 1. Upper Case (A to Z)

    2. Lower Case (a to z)

    => Numerals / Digits 0 to 9

    => Special Characters

    1. Simple Keys

    + - * / | _ " ; : ' ? \ > < , . # % ^ & ! ~

    ( ) { } [ ] = .....

    2. Compound/Meta Keys

    >= > ....

  • 7/28/2019 c Language by SRIDHAR

    3/144

    Words:

    "Grouping up of characters,

    to convey some meaning"

    1. Reserved Words(Keywords)

    2. User Defined Words(Identifiers)

    Software

    |

    Set of related program

    |

    Set of ordered instructions/Statements

    |

    Grouping up of Expressions

    |

    Combination of Constants, Operators, Operands,

    Keywords, Function Calls, etc.

    (Collection of Tokens)

    Tokens (Building Blocks)

    It is a small individual unit used in a program

    1. Keywords

    2. Identifiers

    3. Literals

  • 7/28/2019 c Language by SRIDHAR

    4/144

    4. Operators

    5. Punctuators

    1. Keywords (Reserved Words):

    These are the pre-defined words of the language assigned with unique meanings or operations.

    'C' consists of totally 32 keywords (ANSI-C -> 36 keywords).

    int signed enum break

    char unsigned extern continue

    float static register goto

    double include auto switch

    long define if case

    short typedef else default

    return FILE

    while EOF

    for volatile

    do void

    struct const

    union sizeof

    Variable:\

    It is an entity (memory container) that holds the data stored

    Ex:

    2. Identifiers:

  • 7/28/2019 c Language by SRIDHAR

    5/144

    These are the user-defined words / labels defined for giving identity to different entities used in

    a program, such as variable names, function names, file names, fieldnames, typenames, etc.

    => Conventions for framing identifiers:

    1.Should begin with an alphabet followed by numerals may also be used.

    2. No special characters except under score ( _ ) is allowed

    3. Should be short, easy and meaningful.

    4. Should be unique i.e. non-similar

    5. Keywords should not be used as Identifiers

    6. Recognisation of Upper case and Lower case alphabets will be different

    7. It can be of maximum 40 characters long in length

    3. Literals:

    These are the constants that remains unchanged throughout the program execution.

    'C' classifies the literals into the following five types:

    i. Character Literals => Enclosure of single key within

    single quotations

    Ex: 'A' , '6', '#', '\n'

    ii. String Literals => Enclosure of group of characters

    within double quotations

    Ex: "Satyam", "123"

    iii. Integral Literals=> Rep. a numerical value without

    decimal fraction

    Ex: 250 , -32768

    iv. Floating Literals => Rep. a numerical value with

    decimal fraction

    Ex: 3.14159 , -124.3657

    v. Logical / Boolean Literals => Rep boolean values i.e. in the

    form of true (1) / false (0)

    Ex: 10>20 => 0

  • 7/28/2019 c Language by SRIDHAR

    6/144

    4. Operators:

    These are the special characters (symbols) which are assigned

    with unique operations to be executed when used along with operands.

    C has rich set of built-in operators, which are categorized into two classifications:

    1. Unary Operators: Used along with single operand

    Ex: ++ -- & - ! ~

    2. Binary Operators: Used along with two operands

    Ex: + - * / % < > = ...

    In C, operators are divided into different types, depending on the type of operations they

    perform:

    1. Arithmetical Operators: Used for mathematical calculations

    + (plus) => Addition a + b

    - (minus)=> Subtraction x - y

    * (Asterisk) => Multiplication r * m

    / (Slash) => Division a / b

    % (Percent) => Modular Value (Remainder) x % z

    2. Relational Operators: Used for making comparision between

    two similar type of data items

    > => Greater than

    < => Less than

    >= => Greater than or Equals to

    Less than or Equals to

    == => Equals to

  • 7/28/2019 c Language by SRIDHAR

    7/144

    != => Not Equals to

    3. Logical Operators: Used for joining two conditions as single

    And ( && ) => all must be satisfied

    Or ( || ) => Either one should be satisfied

    Not ( ! ) => Neither should be satisfied

    4. Bitwise Operators: Used to manipulate the data in the form of bits

    ~ (Tilda) => One's Compliment

    Shift Left

    >> => Shift Right

    & (Ambersent) => Bitwise AND

    | (Pipe) => Bitwise OR

    ^ (Carrot) => Bitwise Exclusive OR (XOR)

    5. Assignment Operators: Used for assigning RHS into LHS operands

    = => Assigns RHS into LHS

    Ex: c=a+b;

    += => Adds RHS into LHS Operand

    Ex: x=x+10; -> x+=10;

    -= => Substract RHS from LHS

    Ex: r=r-5; -> r-=5;

    *= => Multiplies RHS with LHS

    Ex: a=a*10; -> a*=10;

  • 7/28/2019 c Language by SRIDHAR

    8/144

    /= => Divides LHS by RHS

    Ex: m=m/2; -> m/=2;

    %= => Modularizes LHS mod RHS

    Ex: g=g%2; -> g%=2;

    5. Punctuators:

    These are the special characters used for grouping or ungrouping the elements of a program.

    i. { } => Braces

    -> Used for grouping set of statements as a single block

    ->

    ->

    ii. ( ) => Paranthesis

    -> Used for giving or representing the data for the functions

    ->Used for representing higher priority in evaluation of an expression

    iii. [ ] => Brackets

    ->

    ->

    iv. ; => Semi Colon -> Used as statement terminator

    ->

    v. , => Comma

    -> Used as separator between the elements of a program

  • 7/28/2019 c Language by SRIDHAR

    9/144

    ->

    vi. . => Decimal Period

    ->

    vii. # => Hash

    -> Used for including the required header files

    -> Also used for defining constant identifiers

    Data Types

    Classification of the data into different types depending on the type of keys used in framing the

    data, for exact memory allocation and faster memory access.

    1. Primitive Data Types:

    These are pre-defined data types of C language

    1. int :

    => It is used for storing numerical value without decimal fraction

    => It only allows numerals from 0 to 9

    => It also allows two special characters + and -

    => It is one word in length

    => It occupies two bytes (16 bits) of memory

    => It's value ranges between -32768 to 32767

    Ex: 256, -4576, +226

    2. char :

    => It is used for storing any single key / character

    => It is one word in length

  • 7/28/2019 c Language by SRIDHAR

    10/144

    => It holds the ASCII value of the given character

    => It occupies one byte (8 bits) of memory

    => It's value ranges between -128 to 127

    Ex: A -> 65, 8 -> 56, #

    3. float :

    => It is used for storing numerical value with decimal fraction

    => It allows numerals from 0 to 9

    => It also allows three special characters i.e.

    +, - and decimal ( . )

    => It is two words in length

    => By default it takes 6 decimal digits

    => It occupies 4 bytes (32 bits) of memory

    => It's value ranges between 3.4E-38 to 3.4E+38

    Ex: 123.5774, -2458.33

    4. double:

    => It is used for storing numerical value with

    decimal fraction

    => It allows numerals from 0 to 9

    => It also allows three special characters i.e.

    +, - and decimal ( . )

    => It is two words in length

    => By default it takes 8 decimal digits

    => It occupies 8 bytes (64 bits) of memory

    => It's value ranges between 1.7E-308 to 1.7E+308

    5. void:

  • 7/28/2019 c Language by SRIDHAR

    11/144

    => It is blank i.e. no data so no memory allocation

    => It is used in function as return data type.

    Expressions:

    It is combination of constants, operators, operands, punctuators, function calls, keywords, etc.,

    which when executed returns any value/data.

    In 'C', expressions are classified into five types depending on the type of data it returns.

    1. Character Expressions (< ExpC >):

    Ex: toupper('a')

    2. String Expressions (< ExpS >):

    Ex: "abc" + "def"

    3. Integral Expressions (< ExpN >):

    Ex: a+20, 25/10=>2

    4. Floating Expressions (< ExpN >):

    Ex: 3.14159*r*r

    5. Logical / Boolean Expression (< ExpL >):

    Ex: a>b => 1/0

    Statements:

    "Grouping up of expressions according to the symantic rules of 'C' language, which when

    executed performs an act or an operation.

    In 'C', statements are of the following categories:

    1. Comment-entry statements

    2. Input Statements

    3. Output Statements

  • 7/28/2019 c Language by SRIDHAR

    12/144

    4. Declaration Statements

    i. Variable Declarations

    a. Local Declarations

    b. Global Declarations

    ii. Structure Declarations

    iii. Type definitions

    5. Pre-processor directive Statements

    6. Control Structures

    i. Branching Statements

    a. goto

    b. continue

    c. return

    d. break

    ii. Conditional Statements

    a. if

    b. if else

    c. switch statements

    iii. Loop / Iterative Controls

    a. while statement

    b. for statement

    c. do while statement

    d. True / Continuous Statement

    7. Null Statements

    8. Definition Statements, etc.

  • 7/28/2019 c Language by SRIDHAR

    13/144

    => Evolution of Programming Languages:

    Binary Language ( 0's & 1's)

    |

    Assembly Language (Low-Level Language)

    |

    High Level Languages

    ||

    Procedure Oriented Programming (POP)

    (Emphasis is on task)

    ||

    Object Oriented Programming (OOP)

    (Emphasis is on data)

    =>Structure of 'C' Program:

    [ Descriptive / Comment-Entry Statements (Documentation Section) ]

    Pre-Processor Directive Statement ( Linkage Section )

    [ Global Declarations / Definitions ]

    Body

    [1. Local Declaration Statements ]

    2. Executable Statements

    Syntax:

    main ( )

    {

    [Declaration Statements;]

  • 7/28/2019 c Language by SRIDHAR

    14/144

    Executable Statements;

    }

    [Function1 ( )

    {

    Declaration Statements;

    Executable Statements;

    }

    ..

    ..

    ..]

    =>Descriptive / Comment-Entry Statements:

    These are non-executable and optional statements (Compulsory to be used) that are used for

    giving description about the current program, such as purpose, environment used, date and time of

    program creation, name of the programmer and so on.

    These statements can appear any where in the program for any number of times.

    Syntax:

    1. /* Description */

    => Single Line Comment

    2. /* Description

    Description

    Description */

    => Multi Line Comment

    Ex:

    1. /* Sample C Program */

  • 7/28/2019 c Language by SRIDHAR

    15/144

    2. /* Program to find sum of two numbers

    Written by Dennis Ritchie */

    => Pre-processor Directive Statements:

    Library Files

    (Header Files)

    ....

    These are the statements that are used for including i.e. linking the necessary header file /s

    within the current program using "#include" directive, depending on the type of functions we use in the

    program.

    Syntax:

    # include < Header Filename >

    Ex:

    # include

    # include

    # include

  • 7/28/2019 c Language by SRIDHAR

    16/144

    These statements are also used for defining constant type of variables with the required

    expression, by using "#define" directive

    Syntax:

    # define

    Ex:

    # define pi 3.14159

    # define size 100

    # define Hi "Good Evening"

    =>Global Declarations / Definitions: (optional statements)

    Statements used for declaration of global version of variables, constants, functions or also used

    for defining global version of type definitions, which can be accessed throughout the program and gets

    released as soon as the program ends.

    -> Declaration of global version of variables:

    Syntax:

    ;

    Ex:

    int x;

    char ch;

    float p;

    double d;

    int a,b,c;

    => Body:

    As 'C' is a modular programming language, where a single program can be split into n number of

    modules known as functions.

    Body is the main function of the C program that gets executed first by C compiler as soon as the

    programmer runs the program. To differentiate the body from the other functions, it is given heading as

    "main ( )". Beginning and Ending of a block

  • 7/28/2019 c Language by SRIDHAR

    17/144

    are represented by " { " and " } " respectively. Every statement in C should be terminated by " ; ".

    Body consists of two types of statements:

    1. Declaration Statements

    2. Executable Statements

    Syntax:

    main ( )

    {

    [Declaration Statements ;]

    Executable Statements ;

    }

    Ex:

    main()

    {

    printf (" Hi, Welcome to the World of Satyam ") ;

    }

    1. Declaration Statements:

    These are used for declaration of local version of variables that can be accessed only within the

    main function.

    Syntax:

    ;

    2. Executable Statements:

    These are the statements that when executed performs an act or an operation, such as takes

    input, gives output, checks condition and so on.

  • 7/28/2019 c Language by SRIDHAR

    18/144

    => Functions: (Self Contained block or program)

    It is a module or block of a program, that contains set of statements related to perform a

    particular sub task, having a unique identity known as function name.

    /* Sample C Program */

    #include

    main()

    {

    int a,b,c;

    a=10;

    b=20;

    c=a+b;

    printf(" Sum = %d" , c);

    }

    => Different C Compilers:

    |

    These are the translator programmes that are used

    for converting the source code into binary code and

    vice versa.

    => TC -> Turbo C -> Turbo Corporation

    QC -> Quick C -> Micro Soft Corporation

    UNIX & C -> A T & T Bell Laboratories

    BC -> Borland C -> Borland Corporation

  • 7/28/2019 c Language by SRIDHAR

    19/144

    ANSI - C -> American National Standard Institute

    Getting Started with Turbo C programming:

    1. Start => Run & type

    C:\Tc\Tc.exe => Click on OK button

    2. Start => Run & type

    Command => Click on OK button

    => CD\

    C:\>

    => Type CD TC

    C:\TC>

    => Type TC and press enter key

    C:\TC> TC

    3. Double click on Turbo C Short icon on the Desktop (if exist)

    4. Select Start => Programs => Turbo C

    => Press Alt+Enter

    Notes:

    1. To Save the Program : Alt+F(File) => Save

    (Default extension for C files will be ".C") OR Press F2

    (Sample . C)

    2. To compile the program: Alt+C(Compile)=>Compile OR Alt+F9

    (Automatically creates object file with ".OBJ") (Sample.OBJ)

    3. To zoom / unzoom the current window: Press F5

    4. To toggle between the message window and edit window: Press F6

  • 7/28/2019 c Language by SRIDHAR

    20/144

    5. To run/execute the program: Alt+R(Run)=> Run OR Press Ctrl+F9

    (Automatically creates output file with ".EXE") (Sample.Exe)

    Source Code =>Compiler => Object Code => Linker => Output Code

    (.C) (.OBJ) (.EXE)

    6. To toggle between the output window and the editor Alt+F5

    7. To write a new program: Alt+F(File)=> New

    8. To opening an existing program: Alt+F(File)=> Load OR Press F3

    9. To quit the editor: Alt+F(File)=>Quit OR Press Alt+X

    /* Sample C Program */

    #include

    main( )

    {

    printf(" Hi, Good Morning ") ;

    }

    => Output Statements:

    These are the statements that gives output to the user by the program.

    => printf ( ): print Formatted

    It is the built-in C formatted output function that prints

    the output of the given expressions on the monitor in the specified

    format. It is defined in file.

    Syntax:

  • 7/28/2019 c Language by SRIDHAR

    21/144

    printf ( [ " Format Specifiers " , ] ) ;

    Where

    Expression => a Constant, an Operand, an operator,

    a formula, a function call, a keyword, etc.

    Format Specifiers:

    These are the codes that are used for representing

    the type of data to be taken as input or the type of output to be given by the

    program, to C compiler.

    "%d" => int

    "%c" => char

    "%f" => float

    "%s" => string

    "%l" => long

    "%u" => unsigned

    "%lf" => double

    "%ld" => long int

    "%ud" => unsigned int

    Ex: 1. printf(" Good Evening ");

    2. int x =10 , y = 20;

    printf(" %d & %d " , x , y );

    => 10 & 20

    3. printf ( " x = %d and y = %d " , x , y ) ;

    => x =10 and y = 20

  • 7/28/2019 c Language by SRIDHAR

    22/144

    4. c = x + y ;

    printf ( " Sum = %d " , c ) ;

    OR

    printf ( " Sum = %d " , x + y ) ;

    => Sum = 30

    5. int x =10;

    char ch = 'A' ;

    float p =123.57888 ;

    printf ( " x = %d ch = %c p = %f " , x , ch , p ) ;

    => x = 10 ch = A p = 123.578878

    /* To find sum of two nos */

    #include

    main()

    {

    int a=40 , b=20;

    printf(" Sum = %d " , a+b);

    }

    => WAP to find sum, difference, product and division of two nos where

    x=50 and y=5

    /* To find the area of a circle */

    #include

    #define pi 3.14159

  • 7/28/2019 c Language by SRIDHAR

    23/144

    main()

    {

    int r=5;

    printf(" Area of a Circle : %f ",pi*r*r);

    }

    /* To find sum, difference, product and division of two nos */

    #include

    main()

    {

    int x=50 , y=5;

    printf("\n Sum = %d " , x+y);

    printf("\n Difference = %d " , x-y);

    printf("\n Product = %d\n",x*y);

    printf(" Division = %d " , x/y);

    }

    -> printf( "\n Sum = %d\n Difference = %d\n Product = %d\n Division = %d ",

    x+y , x-y , x*y , x/y);

    => Input Statements:

    These are the statements that are used for taking the

    necessary input from the user through keyboard.

    => scanf ( ): scan format

  • 7/28/2019 c Language by SRIDHAR

    24/144

    It is a built-in C formatted input function that scans or

    gets the required specified type of data as input from the user and

    stores them into the given identifiers (variables) address. It is

    defined in file.

    Syntax:

    scanf( " format specifiers " , & );

    Where

    Identifier => Variable name

    & (Ambersent ) => Reference / Address Operator

    format specifiers => codes used for rep. the data type

    Ex:

    1. int a,b;

    scanf( "%d%d" , &a , &b );

    2. int n;

    scanf("%d" , &n);

    3. int x;

    char ch;

    float p;

    scanf( "%d%c%f" , &x , &ch , &p ) ;

    /* To find sum, difference, product and division of two given nos */

    #include

    main()

    {

    int a,b;

  • 7/28/2019 c Language by SRIDHAR

    25/144

    printf("\n Enter two values :");

    scanf("%d%d",&a,&b);

    printf("\n Sum = %d ",a+b);

    printf("\n Difference = %d\n",a-b);

    printf(" Product = %d \n",a*b);

    printf(" Division = %d ", a/b);

    }

    /* To find the area of a circle */

    #include

    main()

    {

    int r;

    printf(" Enter radius of a Circle :");

    scanf("%d" , &r);

    printf(" Area of Circle = %f " , 3.14159*r*r ) ;

    }

    => wap to find the area and perimeter of a rectangle

    /* To find the area and perimeter of a rectangle */

    #include

    main()

    {

    int l,b; clrscr();

    printf(" Enter length and breadth of a rectangle :");

  • 7/28/2019 c Language by SRIDHAR

    26/144

    scanf("%d%d", &l, &b);

    printf(" Area of a Rectangle = %d ", l*b);

    printf("\n Perimeter of a Rectangle = %d ", 2*(l+b));

    }

    /* To swap the two given numbers */

    #include

    main ( )

    {

    int a , b , c ; => int a , b;

    printf(" Enter two numbers :");

    scanf( "%d%d" , &a , &b );

    printf(" Before swapping values are a=%d and b=%d" , a , b );

    c=a; => a=a+b;

    a=b; => b=a-b;

    b=c; => a=a-b;

    printf("\n After swapping values are a=%d and b=%d " , a , b );

    }

    => clrscr ( ):

    It is a built-in C console function that clears the output window's screen. It is defined in

    file

    Syntax:

    clrscr ( );

    'C' Language

  • 7/28/2019 c Language by SRIDHAR

    27/144

    => Programming Language

    => Middle Level Language

    => Case Sensitive Language

    => Modular / Block Programming

    (Large programs can be split into blocks)

    1. Easy readability and easy understandability

    2. Easy debugging of errors

    3. Easy sharing of work

    => Procedure Oriented Programming (POP)

    => Emphasis is on task

    => Large programs are split into functions

    => General Purpose programming

    => Structural programming

    -> Syntax & Symantic rules

    -> Memory resident programming feature

    Definition:

    'C' is a general purpose structured modularized procedure

    oriented case sensitive middle level programming language, that

    acts as an interface between low level and high level languages.

    History of C Language:

    High-Level Languages (ALGOL) - early 1960's

    |

    BCPL - Martin Ritchard - early 1964's

  • 7/28/2019 c Language by SRIDHAR

    28/144

    (Basic Combined Programming Language)

    |

    "B" (BASIC) Language - Ken Thompson - 1967

    |

    "C" Language - Dennis M Ritchie - 1972

    at A T & T Bell Laboratories, Murray Hills,

    New Jersy (USA)

    |

    "C with Classes" - Bjarne Stroustrup - 1982

    |

    Renamed it as "C++" - 1983

    /* To read and print a character */

    #include

    main ( )

    {

    char x;

    clrscr( );

    printf(" Enter an alphabet :");

    scanf("%c", &x);

    printf(" Given alphabet = %c" , x ) ;

    }

    => getchar ( ):

    It is a built-in C Character input function that gets or reads a single character / key as input and

    assigns it into the left hand side character variable. It is defined in file

  • 7/28/2019 c Language by SRIDHAR

    29/144

    Syntax:

    =getchar ( );

    Ex:

    char m;

    m=getchar ( );

    => putchar ( ):

    It is a built-in C Character output function that puts or prints the given character expression on

    the monitor. It is defined in file.

    Syntax:

    putchar();

    Ex:

    1. putchar(m);

    2. putchar( 'S' );

    => S

    /* To read and print a character using getchar ( ) and putchar ( )*/

    #include

    main ( )

    {

    char x;

    clrscr( );

    printf(" Enter an alphabet :");

    x=getchar( );

    printf(" Given alphabet = ");

    putchar(x);

  • 7/28/2019 c Language by SRIDHAR

    30/144

    }

    #include

    main()

    {

    int a , b; clrscr();

    printf( " Enter two numbers :");

    scanf("%d%d" , &a, &b);

    if(a>b)

    {

    printf(" Biggest = %d " , a);

    printf("\n Smallest = %d " , b);

    }

    else

    printf(" Biggest = %d \n Smallest = %d ", b , a);

    getch();

    }

    /* To convert the given upper case alphabet into lower case */

    #include

    main ( )

    {

    char ch;

    clrscr();

  • 7/28/2019 c Language by SRIDHAR

    31/144

    printf(" Enter an upper case alphabet :");

    ch=getchar( );

    ch=ch+32;

    printf(" Lower case alphabet = %c" ,ch);

    }

    => To convert the given lower case alphabet into upper case

    => Conditional / Selection Statements:

    Condition:

    => Making comparision between two

    similar type of data items

    1. Simple Conditions:

    Ex: a>b

    2. Compound/ Complex Conditions:

    Ex: a>b && a>c

    These are the statements that are used for decision making within a program

    by imposing a condition and performing either executions, i.e. these statements are

    used for altering the sequential execution of the program.

    i. Simple if

    ii. if else

    iii. Nested if else / if else if ladder

    iv. switch Statement

  • 7/28/2019 c Language by SRIDHAR

    32/144

    i. Simple if:

    In this notation we only specify what to be executed when the condition

    is true i.e. no alternative execution is specified.

    Syntax:

    if ()

    {

    Statement 1;

    Statement 2;

    ...................

    Statement n;

    }

    Ex:

    if ( a % 2 == 0 )

    {

    printf(" Given number is Even ");

    }

    if( a % 2 != 0 )

    {

    printf(" Given number is Odd ");

    }

    ii. if else:

    In this notation we specify either executions to be executed depending

    on the evaluation of the condition.

    Syntax:

    if ()

  • 7/28/2019 c Language by SRIDHAR

    33/144

    {

    Statement 1;

    Statement 2; --> True Block

    ...................

    Statement n;

    }

    else

    {

    Statement 1;

    Statement 2; --> False Block

    ....................

    Statement n;

    }

    Ex:

    if ( a % 2 == 0)

    {

    printf(" Given number is Even ");

    }

    else

    {

    printf(" Given number is Odd ");

    }

    /* To find the biggest among the two given numbers */

  • 7/28/2019 c Language by SRIDHAR

    34/144

    #include

    main()

    {

    int x , y; clrscr();

    printf(" Enter two numbers :");

    scanf("%d%d" , &x ,&y);

    if( x>y )

    printf(" Biggest = %d " , x);

    if( y>=x ) => else

    printf(" Biggest = %d " , y);

    getch();

    }

    => To find biggest and smallest among two given numbers

    => To find whether the given number is even or odd

    /* To find whether the given alphabet is an upper case or lower case alphabet */

    #include

    main()

    {

    char x ; clrscr();

    printf(" Enter an alphabet :");

    x=getchar( );

    if( x>=65 && x if ( x>= 'A' && x

  • 7/28/2019 c Language by SRIDHAR

    35/144

    printf(" Given alphabet is of Upper case ");

    else

    printf(" Given alphabet is of Lower Case ");

    getch( );

    }

    #include

    main()

    {

    int a,b,c; clrscr();

    printf(" Enter three numbers :");

    scanf("%d%d%d", &a,&b,&c);

    /* Compound Conditions */

    if(a>b && a>c)

    printf(" Biggest = %d ",a);

    else

    if(b>c)

    printf(" Biggest = %d ",b);

    else

    printf(" Biggest = %d ",c);

    if(a

  • 7/28/2019 c Language by SRIDHAR

    36/144

    printf("\n Smallest = %d ",b);

    else

    printf("\n Smallest = %d ", c);

    getch();

    }

    => Character Functions:

    These are the functions that are used for manipulation of the character type

    of data. These functions are defined in file.

    1. toupper ( ):

    ->Returns the converted given character expression from lower case to upper case.

    Syntax: ( x - 32 )

    toupper();

    Ex:

    1. x = toupper( x ) ;

    2. toupper( 'b' );

    => B

    2. tolower( ):

    -> Returns the converted character of the given character expression from

    upper case to lower case.

    Syntax: ( x + 32 )

    tolower()

    Ex:

  • 7/28/2019 c Language by SRIDHAR

    37/144

    1. ch = tolower( ch );

    2. tolower( 'G' ) ;

    -> g

    3. isupper( ):

    -> Used for checking whether the given character expression is representing

    an upper case alphabet or not. If yes return 1 (true) otherwise returns 0 (false).

    Syntax: ( x >= 65 && x 0

    4. islower( ):

    -> Used for checking whether the given character expression is representing

    an lower case alphabet or not. If yes return 1 (true) otherwise returns 0 (false).

    Syntax: (x >= 97 && x

  • 7/28/2019 c Language by SRIDHAR

    38/144

    2. islower( 'j' )

    -> 1

    5. isalpha( ):

    -> Used for checking whether the given character expression is representing

    an alphabet or not. If yes return 1 (true) otherwise returns 0 (false).

    Syntax: ( (x >= 65 && x =97 && x 0

    6. isdigit( ):

    -> Used for checking whether the given character expression is representing

    a digit or not. If yes return 1 (true) otherwise returns 0 (false).

    Syntax: (x >= '0' && x < = '9' )

    isdigit ()

    Ex:

    1. if( isdigit(x) )

    printf(" Given character is a digit ");

    else

    printf(" Given character is not a digit");

    2. isdigit( 'j' )

  • 7/28/2019 c Language by SRIDHAR

    39/144

    -> 0

    7. isspace( ):

    -> Used for checking whether the given character expression is representing

    a space or not. If yes return 1 (true) otherwise returns 0 (false).

    Syntax: (x==' ')

    isspace ()

    Ex:

    1. if( isspace (x) )

    printf(" Given character is a space ");

    else

    printf(" Given character is not a space");

    2. isspace( ' ' )

    -> 1

    => Write a program to find whether the given character is an alphabet or not

    3. Nested Conditional Statements:

    -> Grouping or using one or more conditional statements within an

    another conditional statement, is referred as Nested Conditional Statements.

    Syntax:

    if()

    if()

    {

    Statements; -> 1-T & 2-T

    }

  • 7/28/2019 c Language by SRIDHAR

    40/144

    else

    {

    Statements; -> 1-T & 2-F

    }

    else

    if()

    {

    Statements; -> 1-F & 3-T

    }

    else

    {

    Statements; -> 1-F & 3-F

    }

    /* To find biggest among three given numbers */

    /* Simple Conditions */

    /* Nested Conditional Statements */

    #include

    main( )

    {

    int a , b , c ;

    clrscr( );

    printf(" Enter three values :");

    scanf( "%d%d%d" , &a , &b , &c ) ;

  • 7/28/2019 c Language by SRIDHAR

    41/144

    if(a>b)

    if(a>c)

    printf(" Biggest = %d",a);

    else

    printf(" Biggest = %d",c);

    else

    if(b>c)

    printf(" Biggest = %d",b);

    else

    printf(" Biggest = %d",c);

    getch( );

    }

    => To find biggest and smallest among three given nos

    => if else if Ladder:

    Grouping up of if statements after else part of the conditional statements is referred as if else if

    ladder.

    Syntax:

    if()

    {

    Statements; -> 1-T

    }

    else

    if()

    {

  • 7/28/2019 c Language by SRIDHAR

    42/144

    Statements; -> 1-F & 2-T

    }

    else

    if()

    {

    Statements; -> 1-F , 2-F & 3-T

    }

    .....

    else

    {

    Statements; -> No condition is true

    }

    /* To find biggest among three given numbers */

    /* Compound Conditions */

    /* if else if Ladder */

    #include

    main( )

    {

    int a,b,c;

    clrscr( );

    printf(" Enter three values :");

    scanf("%d%d%d" , &a , &b , &c ) ;

    if(a>b && a>c)

  • 7/28/2019 c Language by SRIDHAR

    43/144

    printf(" Biggest = %d",a);

    else

    if(b>c)

    printf(" Biggest = %d",b);

    else

    printf(" Biggest = %d",c);

    getch( );

    }

    => Wap to find whether the given number is positive, negative or zero

    /* To check whether the given character is an alphabet, a digit, a space or a special

    character*/

    #include

    #include

    main( )

    {

    char x; clrscr( );

    printf(" Enter a character :");

    x=getchar( );

    if( isalpha(x) )

    printf(" Given character is an alphabet ");

    else

    if( isdigit(x) )

    printf(" Given character is a digit ");

  • 7/28/2019 c Language by SRIDHAR

    44/144

    else

    if( isspace(x) )

    printf(" Given character is a space ");

    else

    printf(" Given character is a special character ");

    getch( );

    }

    => Ternary / Conditional Operator ( ?: ):

    It is a special built-in 'C' operator that is used for evaluation of the given

    condition and perform either action or returns either value.

    Syntax: () ? :

    Ex:

    (a>b) ?a :b

    OR

    printf(" Biggest = %d" , (a>b) ?a :b );

    2. (a>b) ?printf(" Biggest = %d",a); : printf(" Biggest =%d " ,b);

    3. (a>b) ? (a>c) ? a : c :(b>c) ? b : c -> Nesting of Ternary Operators

    -> Simple Conditions

    4. (a>b && a>c) ? a : (b>c) ? b : c -> Compound Conditions

    /* To find biggest and smallest among three given numbers using Ternary Operator */

    #include

    main( )

  • 7/28/2019 c Language by SRIDHAR

    45/144

    {

    int x , y , z ; clrscr( );

    printf(" Enter three numbers :");

    scanf("%d%d%d" , &x , &y , &z );

    printf(" Biggest = %d" , (x>y && x>z) ?x :(y>z) ?y :z );

    printf("\n Smallest = %d", (x

  • 7/28/2019 c Language by SRIDHAR

    46/144

    printf(" Two");

    else

    if(n==3)

    printf(" Three ");

    else

    if(n==4)

    printf(" Four");

    else

    if(n==5)

    printf(" Five ");

    else

    if(n==6)

    printf(" Six");

    else

    if(n==7)

    printf(" Seven");

    else

    if(n==8)

    printf(" Eight");

    else

    if(n==9)

    printf(" Nine");

    else

    printf(" Invalid Digit, Try again");

    getch();

  • 7/28/2019 c Language by SRIDHAR

    47/144

    }

    4. switch Statement:

    It is a selection statement that is used for performing one among the given

    n number of alternative executions depending on the output yielded by the given

    expression.

    Syntax:

    switch()

    {

    case :

    Statements; break;

    case :

    Statements; break;

    case :

    Statements; break;

    .....

    .....

    case :

    Statements; break;

    default:

    Statements;

    }

    /* To print the given digit in characters using switch statement */

    #include

    main( )

  • 7/28/2019 c Language by SRIDHAR

    48/144

    {

    int n; clrscr( );

    printf(" Enter a digit :");

    scanf("%d",&n);

    switch(n)

    {

    case 0: printf(" Zero"); break;

    case 1: printf(" One "); break;

    case 2: printf(" Two"); break;

    case 3: printf(" Three"); break;

    case 4: printf(" Four"); break;

    case 5: printf(" Five"); break;

    case 6: printf(" Six"); break;

    case 7: printf(" Seven"); break;

    case 8: printf(" Eight"); break;

    case 9: printf(" Nine"); break;

    default: printf(" Invalid digit, try again");

    }

    getch();

    }

    /* To check whether the given alphabet is a vowel or a consonent */

    #include

    main( )

    {

  • 7/28/2019 c Language by SRIDHAR

    49/144

    char m;

    clrscr( );

    printf(" Enter an alphabet :");

    m=getchar( );

    switch(m)

    {

    case 'A' :

    case 'a' :

    case 'E' :

    case 'e' :

    case 'I' :

    case 'i' :

    case 'O' :

    case 'o' :

    case 'U' :

    case 'u' : printf(" Given alphabet is a Vowel "); break;

    default: printf(" Given alphabet is a Consonent ");

    }

    getch( );

    }

    => Escape Sequence Characters:

    These are the codes that are used for formatting the output of a program by

    executing the printable characters and printing the executable characters. These

    codes should begin with Backward slash ( \ ) followed by a character and should be

  • 7/28/2019 c Language by SRIDHAR

    50/144

    specified within double quotations.

    "\a" => Audible Bell (Beep sound)

    "\b" => Backspace key

    "\r" => Cariage Return

    "\t" => horizontal Tab ( 8 spaces)

    "\v" => Vertical Tab

    "\n" => New line character

    "\\" => Prints \ character

    "\"" => Prints " character

    "\' " => Prints ' character

    "\?" => Prints ? character

    '\0' => Null Character

    Ex: printf("\"Hi, Good evening\" "); => " Hi, Good Evening"

    /* To display the address of Satyam institute */

    #include

    main()

    {

    clrscr();

    printf("\n\n\n\n\n\n\n\n\n\n\t\t\t\t\"SATYAM COMPUTER EDUCATION");

    printf("\n\t\t\t #17-7-77, Laxmi Linga Rao Arcade,");

    printf("\n\t\t\t 4th Floor, Above Hero Honda Showroom,");

    printf("\n\t\t\t\tMankamma Thota, KARIMNAGAR\"");

    getch();

  • 7/28/2019 c Language by SRIDHAR

    51/144

    }

    => gotoxy( ):

    It is a built-in 'C' function that is used to move the cursor to the specified

    position on the screen, where the position is specified in terms of column and row,

    where column can be in between 0 to 79 and row can be in between 0 to 23.

    Syntax: gotoxy(,);

    Ex: gotoxy(25,12);

    /* To display the address of Satyam institute */

    #include

    main()

    {

    clrscr();

    gotoxy(24,10); printf("\"SATYAM COMPUTER EDUCATION");

    gotoxy(20,11); printf("#17-7-77, Laxmi Linga Rao Arcade,");

    gotoxy(20,12); printf("4th Floor, Above Hero Honda Showroom,");

    gotoxy(24,13); printf("Mankamma Thota, KARIMNAGAR\"");

    gotoxy(28,9); printf(" N. Kamalakar Rao");

    getch();

    }

    Loop /Iterative Control Statements

    These are the statements that are used for repeatation of the given set

    of statement's execution for required n number of times.

    'C' provides four different types of loop statements, which can also be

    referred as Iterative control statements:

  • 7/28/2019 c Language by SRIDHAR

    52/144

    1. while Statement

    2. for Statement

    3. do while Statement

    4. True / Continuous Statement

    1. while Statement:

    It is a loop / iterative control statement that repeats the execution of the

    given set of statements for repeated n number of times, until the given condition is

    met or satisfied.

    Syntax:

    Initialization of Loop Variable;

    while() -> Loop Exit Test

    {

    Statement 1;

    Statement 2;

    . . . . . . . . . . .

    Statement n;

    Modification of Loop Variable;

    }

    Ex:

    /* To generate whole numbers from 0 to 20 */

    #include

    main( )

    {

    int n=2;

  • 7/28/2019 c Language by SRIDHAR

    53/144

    clrscr( );

    while(n=1)

    {

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

    n=n-1;

    }

    getch( );

    }

    => To generate even numbers from 2 to 100

    => To generate odd numbers from 99 to 1 and find their total

  • 7/28/2019 c Language by SRIDHAR

    54/144

    /* To generate all ASCII codes and characters in page format */

    #include

    main( )

    {

    int n=0;

    clrscr( );

    while(n

  • 7/28/2019 c Language by SRIDHAR

    55/144

    {

    printf("\n %d * %d = %d " , m , n , m*n ) ;

    n=n+1;

    }

    getch( );

    }

    => To generate addition table for the given number

    /* To generate fibonnacci series below 100 */

    #include

    main()

    {

    int a=0 ,b=1; clrscr();

    while(b Increment & Decrement Operators:

    C has two special built-in operators by name Increment and Decrement operators, which are

    denoted as "++" and "--" respectively.

  • 7/28/2019 c Language by SRIDHAR

    56/144

    ++ => Increment Operator

    -- => Decrement Operator

    Ex: n=n+1; --> n++; / ++n;

    x=x-1; --> x-- ; / --x;

    Increment Operator (++) is used for increasing i.e. raising the given operands

    value by one.

    Decrement Operator (--) is used for decreasing i.e. reducing the given operands

    value by one.

    Both the above operators are classified into two types:

    1. Pre 2. Post

    In Pre-Incrementing / Decrementing, the value of the given operand is first

    raised / reduced and later the modified value is used in the given execution.

    In Post-Incrementing / Decrementing, the value of the given operand is first

    used in the given execution (unmodified) and later it is raised / reduced.

    Pre-Increment Post-Increment

    Syntax: Syntax:

    ++; ++;

    Ex: Ex:

    int x=3,y; int x=3,y;

    y=++x; -> x=4 y=x++; ->y=3

  • 7/28/2019 c Language by SRIDHAR

    57/144

    y=4 x=4

    Pre-Decrement Post-Decrement

    Syntax: Syntax:

    --; --;

    Ex: Ex:

    int m,n=7; int m,n=7;

    m=--n; m=n--;

    -> n=6 m=7

    m=6 n=6

    /* To generate whole numbers from 20 to 0 */

    #include

    main( )

    {

    int n=20; clrscr( );

    while(n>=0)

    printf("\n %d", n-- );

    getch( );

    }

    /* To extract the individual digits of the given value */

    #include

    main()

    {

  • 7/28/2019 c Language by SRIDHAR

    58/144

    int n , r; clrscr();

    printf(" Enter a value :");

    scanf("%d" , &n ) ;

    while(n>0)

    {

    r = n % 10 ;

    printf( " \n %d " , r ) ;

    n = n / 10;

    }

    getch();

    }

    /* To find the reverse value for the given number */

    #include

    main()

    {

    int n , r , rev=0 ; clrscr();

    printf(" Enter the required value :");

    scanf("%d" , &n);

    while(n>0)

    {

    r=n%10;

    rev=rev*10+r ;

    n=n/10;

    }

  • 7/28/2019 c Language by SRIDHAR

    59/144

    printf("\n Reverse Value = %d " , rev );

    getch();

    }

    => Nested while Statements:

    Grouping or using one or more while statements with in an another while statement, is referred

    as Nested while statements.

    Syntax:

    while()

    {

    Statements;

    while()

    {

    m n => mxn

    .................

    }

    Statements;

    }

    /* Sample program for nested while statements - output */

    #include

    main()

    {

    int x=1 , y ; clrscr();

    while( x

  • 7/28/2019 c Language by SRIDHAR

    60/144

    y=1;

    while( y Write programs for the following outputs

    1. 3 3 2. 1 1

    2 3 2 2

    1 3 3 3

    3 2 1 1

    2 2 2 2

    1 2 3 3

    3 1 1 1

    2 1 2 2

    1 1 3 3

    3. 4.

    1 1 2 3 4 5

  • 7/28/2019 c Language by SRIDHAR

    61/144

    1 2 1 2 3 4

    1 2 3 1 2 3

    1 2 3 4 1 2

    1 2 3 4 5 1

    ..........

    1 2 3 4 5 6 7 ....n

    3.

    /* output3 */

    #include

    main()

    {

    int x=1 , y , n ; clrscr();

    printf(" Enter value for n:");

    scanf("%d" , &n ) ;

    while( x

  • 7/28/2019 c Language by SRIDHAR

    62/144

    }

    getch( );

    }

    /* To generate multiplication tables from 1 to 10 */

    #include

    main()

    {

    int x=1,y; clrscr();

    while(x

  • 7/28/2019 c Language by SRIDHAR

    63/144

    {

    int n , r , n1 , s=0 ;

    clrscr( );

    printf(" Enter a value :");

    scanf("%d" , &n ) ;

    n1=n;

    while( n>0)

    {

    r = n%10;

    s=s + r * r * r ;

    n= n / 10;

    }

    if(s==n1)

    printf(" Given number is an Armstrong ");

    else

    printf(" Given number is not an Armstrong");

    getch();

    }

    /* To generate all Armstrong nos */

    #include

    main()

    {

    int n=100 , r , n1 , s ;

    clrscr( );

  • 7/28/2019 c Language by SRIDHAR

    64/144

    while(n0)

    {

    r=n1%10;

    s=s+r*r*r;

    n1=n1/10;

    }

    if(s==n)

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

    n++;

    }

    getch();

    }

    /* To find the Hail Stone Value for the given number */

    #include

    main()

    {

    int n ,count ; clrscr();

    printf(" Enter a number :");

    scanf("%d" , &n);

    for(count=0 ; n>1 ; count++)

  • 7/28/2019 c Language by SRIDHAR

    65/144

    {

    if ( n % 2 == 0 )

    n=n / 2;

    else

    n= n * 3 + 1 ;

    }

    printf(" Hail Stone Value = %d",count);

    getch();

    }

    2. for Statements:

    It is also an iterative control / loop statement that repeats the execution of the

    given set of statements for repeated n number of times, until the given condition is met or satisfied.

    This statement is same as while statement with only difference in the place of

    specifying the initializations, condition and modification, which are specified as parameters with for

    statement.

    Syntax:

    for( Initializations ; ; Modifications )

    {

    Statement 1;

    .....................

    Statement n;

    }

    /* To generate natural numbers from 1 to 20 */

    #include

  • 7/28/2019 c Language by SRIDHAR

    66/144

    main()

    {

    int n; clrscr();

    for(n=1 ; n

  • 7/28/2019 c Language by SRIDHAR

    67/144

    printf(" Enter a number :");

    scanf("%ld" ,&n );

    for(f=1 ; n>=1 ; n--)

    f=f*n;

    printf(" \n Factorial Value = %ld" , f);

    getch( );

    }

    /* To find whether the given number is prime or not */

    #include

    main( )

    {

    int n , f , d ; clrscr();

    printf(" Enter the required number :");

    scanf("%d" , &n ) ;

    for(f=0 , d=1 ; d

  • 7/28/2019 c Language by SRIDHAR

    68/144

    }

    => To find whether the given value is pallindrome number or not

    2.

    /* To generate an output using nested for statements */

    #include

    main()

    {

    int x,y,sp=20,k; clrscr();

    for(x=1 ; x

  • 7/28/2019 c Language by SRIDHAR

    69/144

    printf(" %d",y);

    printf("\n");

    sp+=2;

    }

    getch();

    }

    => Nested for Statements:

    Grouping or using one or more for statements within an another for statement is referred as

    Nested for statements.

    Syntax:

    for( Initializations1 ; ; Modifications1 )

    {

    Statement 1;

    for( Initializations2 ; ; Modifications2 )

    {

    Statement 1;

    m n --> mxn

    Statement n;

    }

    Statement n;

    }

    /* To generate an output using nested for statements */

    #include

  • 7/28/2019 c Language by SRIDHAR

    70/144

    main()

    {

    int x , y ; clrscr();

    for( x=1 ; x

  • 7/28/2019 c Language by SRIDHAR

    71/144

    1 *

    1 2 * *

    1 2 3 * * *

    1 2 3 4 * * * *

    1 2 3 4 5 * * * * *

    /* To generate an output using nested for statements */

    #include

    main()

    {

    int x , y , sp=20 , k ; clrscr();

    for(x=1 ; x

  • 7/28/2019 c Language by SRIDHAR

    72/144

    1 2 3

    1 2 3 4 5

    1 2 3 4 5 6 7

    1 2 3 4 5 6 7 8 9

    /* To generate all prime numbers below the given number */

    #include

    main( )

    {

    int x, n , f , d ; clrscr( );

    printf(" Enter the maximum prime required number :");

    scanf("%d" , &x);

    for(n=1 ; n

  • 7/28/2019 c Language by SRIDHAR

    73/144

    1. * * * * * * * * *

    * * * * * * *

    * * * * *

    * * *

    *

    /* To generate the following output */

    #include

    main()

    {

    int x,y,sp=10,k; clrscr();

    for(x=9 ; x>=1 ; x-=2)

    {

    for(k=1 ; k

  • 7/28/2019 c Language by SRIDHAR

    74/144

    1 2 3

    1 2 3 4 5

    1 2 3 4 5 6 7

    1 2 3 4 5 6 7 8 9

    1 2 3 4 5 6 7

    1 2 3 4 5

    1 2 3

    1

    3.

    1

    1 2 1

    1 2 3 2 1

    1 2 3 4 3 2 1

    1 2 3 4 5 4 3 2 1

    /* To generate the following output */

    #include

    main()

    {

    int x,y,sp=10,k; clrscr();

    for(x=1 ; x

  • 7/28/2019 c Language by SRIDHAR

    75/144

    printf(" %d",y);

    for(y-=2; y>=1; y--)

    printf(" %d",y);

    printf("\n");

    sp--;

    }

    getch();

    }

    => Wap to generate multiplication tables from the given starting number till the given ending number

    3. do while Statement:

    It is also a loop / iterative control statement that repeats the execution of the given set of

    statements for repeated n number of times until the given condition is met or satisfied.

    This statement is similarly to while statement, with only difference in the place of condition

    evaluation i.e. in while the condition is evaluated at the beginning of the loop statement where as in do

    while the condition is evaluated at the end of the loop statement, so in do while the loop statements

    gets executed at least for once.

    Syntax:

    do

    {

    Statement 1;

    Statement 2;

    .....................

    Statement n;

  • 7/28/2019 c Language by SRIDHAR

    76/144

    }while() ;

    Ex:

    int n=0; int n=0;

    while(n>=20) do

    { {

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

    n++; n++;

    } }while (n>=20);

    => No Output => 0

    => Write a program to generate the following menu

    GENERATE MENU

    1. EVEN NUMBERS

    2. ODD NUMBERS

    3. ASCII CODES & CHARS

    4. FIBONNACCI SERIES

    5. ARMSTRONG NOS

    6. PRIME NOS

    7. EXIT

    ENTER U'R CHOICE:

    /* To generate a menu */

  • 7/28/2019 c Language by SRIDHAR

    77/144

    #include

    main()

    {

    int choice,n,n1,r,s;

    do

    {

    clrscr();

    gotoxy(35,7); printf(" GENERATE MENU");

    gotoxy(30,9); printf(" 1. EVEN NUMBERS");

    gotoxy(30,10); printf(" 2. ODD NUMBERS");

    gotoxy(30,11); printf(" 3. ASCII CHARS & CODES");

    gotoxy(30,12); printf(" 4. FIBONNACCI SERIES");

    gotoxy(30,13); printf(" 5. ARMSTRONG NUMBERS");

    gotoxy(30,14); printf(" 6. PRIME NUMBERS");

    gotoxy(30,15); printf(" 7. EXIT");

    gotoxy(30,17); printf(" ENTER UR CHOICE:");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1: for(n=2 ; n

  • 7/28/2019 c Language by SRIDHAR

    78/144

    n+=2;

    } break;

    case 3: for(n=0 ; n

  • 7/28/2019 c Language by SRIDHAR

    79/144

    case 6: for(n=1 ; n

  • 7/28/2019 c Language by SRIDHAR

    80/144

    ....................

    Statement n;

    }

    /* Sample program for true statement */

    #include

    main()

    {

    int n=0; clrscr();

    while(1)

    {

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

    n++;

    if(n>20) break;

    }

    getch();

    }

    => To find sum of the given 10 numbers into an array

    #include

    main()

    {

    int n[10] , id , s ; clrscr();

    printf(" Enter ten values :");

    for( id=0; id

  • 7/28/2019 c Language by SRIDHAR

    81/144

    printf(" Given numbers are ");

    for( id=0, s=0 ; id

  • 7/28/2019 c Language by SRIDHAR

    82/144

    [Subscript];

    Where

    typename => Any primary data type name

    Arrayname => Common identity for the array

    Subscript => size i.e. no. of elements

    Ex:

    int n[5];

    char name[20];

    float p[4];

    double d[10];

    int a[10], b[5];

    When needed we can represent the entire array including all the elements together by a single

    common identity known as Arrayname, or if necessary we can also derepresent the individual elements

    of an array using Arrayname followed by index numbering, which is automatically assigned by the

    Compiler starting from 0 (zero) and ending at Subscript-1.

    /* To read and print 5 values into an array */

    #include

    main()

    {

    int x[5] , id ; clrscr();

    printf(" Enter five values :");

    for(id=0 ; id

  • 7/28/2019 c Language by SRIDHAR

    83/144

    for(id=0 ; id for ( id=4 ; id>=0 ; id - - )

    printf("\n %d" , x[id] );

    getch( );

    }

    => To find sum of the given 10 numbers into an array

    /* To count total number of even and total number of odd

    occured within the given array of 10 values */

    #include

    main()

    {

    int x[10] , id , e , d ; clrscr();

    printf(" Enter ten values :");

    for(id=0 , d=0 , e=0 ; id

  • 7/28/2019 c Language by SRIDHAR

    84/144

    getch();

    }

    => To count total number of positive, negative and zero's occured within the given array of 10 numbers

    /* To search for the given element within the given array of n numbers */

    #include

    main()

    {

    int x[50] , n , i , ele ; clrscr( );

    printf(" How many values to be accepted :");

    scanf("%d" , &n ) ;

    printf(" Enter %d values " , n ) ;

    for(i=0 ; i

  • 7/28/2019 c Language by SRIDHAR

    85/144

    if( i==n )

    printf(" Given element is not found ");

    else

    printf(" Given element is found at %d position" , i+1 ) ;

    getch();

    }

    /* To sort the given array of n numbers in ascending order */

    #include

    main()

    {

    int x[50] , n , i , j , temp ; clrscr();

    printf(" How many values to be accepted :");

    scanf("%d" , &n ) ;

    printf(" Enter %d values " , n ) ;

    for(i=0 ; i

  • 7/28/2019 c Language by SRIDHAR

    86/144

    x[ j ]=temp;

    }

    printf(" Ascending Order of Given Values are :");

    for(i=0 ; i To sort the given n number of elements in an array in descending order

    => To store the first 10 even numbers into an array and print them

    /* To store the first 10 even numbers into an array and print them*/

    #include

    main()

    {

    int n[10] , i , e ; clrscr();

    for(i=0 , e=2 ; i

  • 7/28/2019 c Language by SRIDHAR

    87/144

    2. Double Dimensional Arrays:

    These are the matrix type of variables where the memory for the elements is allocated in two

    dimensional format i.e. in the form of rows and columns, so while declaring these variables we need to

    specify two subscripts (sizes) i.e. row subscript and column subscript

    Syntax: Row Column

    [Subscript][Subscript];

    Ex:

    int x[3] [3];

    char str[10] [20] ;

    float p[3] [4];

    int a[3] [2] , b[4] [3] ;

    When needed we can represent the entire array including all the elements together by a single

    common identity known as Arrayname, or if necessary we can also derepresent the individual elements

    of an array using Arrayname followed by row index numbering and column index numbering, which

    automatically starts from 0 (zero) and ending at Subscript - 1.

    /* to read elements into 3x3 array and print them in martrix format */

    #include

    main()

    {

    int x[3][3] , r , c ; clrscr();

    printf(" Enter elements into 3x3 matrix\n ");

    for(r=0 ; r

  • 7/28/2019 c Language by SRIDHAR

    88/144

    printf(" Given Matrix is \n");

    for( r=0 ; r Write programs to generate the following matrices of 4x4 sizes

    1. Null Matrix 2. Identity Matrix 3. Upper Triangular Matrix

    0 0 0 0 1 0 0 0 0 5 5 5

    0 0 0 0 0 1 0 0 0 0 5 5

    0 0 0 0 0 0 1 0 0 0 0 5

    0 0 0 0 0 0 0 1 0 0 0 0

    4.Lower Triangular Matrix 5.

    0 0 0 0 1 5 5 5

    7 0 0 0 7 1 5 5

    7 7 0 0 7 7 1 5

    7 7 7 0 7 7 7 1

    /* To generate identity matrix of 4x4 size */

    #include

    main()

  • 7/28/2019 c Language by SRIDHAR

    89/144

    {

    int n[4][4] , r , c ; clrscr();

    for( r=0 ; r

  • 7/28/2019 c Language by SRIDHAR

    90/144

    scanf("%d" , & n[r][c] ) ;

    printf(" Transpose Matrix is \n");

    for(r=0 ; r To find the addition matrix for the given two mxn and pxq size matrices

    /* To find the product matrix for the given two matrices of

    mxn and pxq sizes respectively */

    #include

    main()

    {

    int A[10][10],B[10][10],S[10][10];

    int r,c,m,n,p,q,k; clrscr();

    printf(" Enter the order of first matrix in terms of rows and columns");

    scanf("%d%d",&m,&n);

    printf(" Enter the order of second matrix in terms of rows and columns");

  • 7/28/2019 c Language by SRIDHAR

    91/144

    scanf("%d%d",&p,&q);

    if(n==p)

    {

    printf(" Enter elements into first %d x %d matrix \n",m,n);

    for(r=0; r

  • 7/28/2019 c Language by SRIDHAR

    92/144

    }

    }

    else

    printf(" Invalid matrices orders so product matrix is not possible");

    getch();

    }

    3. Multi-Dimensional Arrays:

    These are the array variables where the memory for the elements is allocated in three or more

    dimensional formats, so while declaring these variables we need to specify three or more subscripts.

    Syntax:

    [Subscript1][Subscript2].....;

    Ex:

    int n[3][2][3];

    => Initialization of Arrays:

    Initializing an array is nothing but assigning the required initial values for the elements of an

    array while declaring it, within the declaration statement, may be single dimensional or double

    dimensional arrays.

    -- Single Dimensional Array Initialization --

    Syntax:

    [Subscript]= { Data1, Data2, ..... , Datan } ;

    Ex:

    int n[5] = { 10, 46, 7 , 84 , 22 } ;

    int x[ ]={ 10 , 20 , 30 , 40 , 50 , 60 } ;

    char str [ ] = { 'C' , 'O' , 'M' , 'P' , 'U' , 'T' , 'E' , 'R' } ;

  • 7/28/2019 c Language by SRIDHAR

    93/144

    -- Double Dimensional Array Initialization --

    Syntax: Row Column

    [Subscript][Subscript] ={ Data1, Data2, .... , Datan } ;

    Ex:

    int n[3][3]={ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;

    or

    int n[ ] [ ]={ { 1 , 2 , 3 } ,

    { 4 , 5 , 6 } ,

    { 7 , 8 , 9 } } ;

    String Manipulations

    A string is group of characters represented as a single notation. As 'C' doesnot provides any

    built-in data type by name string, we are adopting Arrays concept and creating a character array for

    storing and manipulating strings, may be of single dimensional format or double dimensional format. In

    'C', we are using "%s" format specifier to represent a string. The end of the string is represented by Null

    character ('\0')

    /* To read and print a string */

    #include

    main( )

    {

    char name[20]; clrscr();

    printf(" Pls enter your name :");

    scanf("%s",name);

    printf(" Hi %s, Glad 2 C U ", name);

    getch( );

  • 7/28/2019 c Language by SRIDHAR

    94/144

    }

    => gets( ): get string

    Built-in 'C' string input function that gets or reads a string (collection of characters including

    white spaces) as input from the user through keyboard and stores it within the given character array.

    Defined in file

    Syntax:

    gets();

    Ex:

    char x[20];

    gets(x);

    => puts( ): put string

    Built-in 'C' string output function that puts or prints the given string expression on the monitor.

    Defined in file

    Syntax:

    puts();

    Ex:

    1. puts(name);

    2. puts(" Hi, Good Evening ");

    /* To read and print a string using string functions*/

    #include

    main( )

    {

    char name[20]; clrscr();

    puts(" Pls enter your name :");

    gets(name);

  • 7/28/2019 c Language by SRIDHAR

    95/144

    puts( name);

    getch( );

    }

    => strlen( ): string length

    => Returns the length i.e. number of characters available in the given string expression. Defined

    in file

    Syntax:

    strlen();

    Ex:

    1. strlen(name);

    2. strlen(" Satyam Computers");

    => 17

    /* To find the length of the given name */

    #include

    #include

    main()

    {

    char name[20];

    int len ; clrscr();

    puts(" Pls Enter UR Name :");

    gets(name);

    len = strlen(name);

    printf("Hi %s , Length of Your Name is %d " , name , len) ;

    getch();

    }

  • 7/28/2019 c Language by SRIDHAR

    96/144

    => strcpy( ): String Copy

    Built in 'C' string function that assigns/copies the given source string expression into the given

    character array. Defined in file

    Syntax:

    strcpy ( , ) ;

    Ex:

    char x[20] , y[20] ;

    gets(x);

    strcpy( y , x ) ;

    2. char result[5] ;

    strcpy(result , "pass" ) ;

    /* To copy the given string from first array into second array */

    #include

    #include

    main()

    {

    char x[20] , y[20];

    clrscr();

    puts(" Pls Enter The Req. String :");

    gets(x);

    strcpy( y , x ) ;

    printf(" Given string is %s " , x ) ;

    printf("\n Copied string is %s " , y ) ;

    getch();

    }

  • 7/28/2019 c Language by SRIDHAR

    97/144

    => strcmp( ): String Comparision

    Built-in 'C' string function that compares the two given string expressions and returns positive

    value when first string is greater than second, negative value when second string is greater than first or

    zero when both the strings are same. Defined in file

    Syntax:

    strcmp(,);

    Ex:

    1.char x[20],y[10]; gets(x); gets(y);

    strcmp(x,y);

    2. strcmp("ABC","abc");

    => -32

    /* To compare the two given strings */

    #include

    #include

    main()

    {

    char x[20],y[20];

    int check; clrscr();

    puts(" Pls Enter The First String :");

    gets(x);

    puts(" Pls Enter The Second String :");

    gets(y);

    check=strcmp(x,y);

  • 7/28/2019 c Language by SRIDHAR

    98/144

    if(check>0)

    printf(" %s string is greater than %s",x,y);

    else if(check strcat( ): String Concatenation

    Built-in 'C' string function that is used for contenation i.e. joining of the two given strings as a

    single string. Defined in file

    Syntax:

    strcat(,);

    Ex:

    char x[20];

    strcpy(x,"Satyam ");

    strcat(x," Computers");

    /* To join the two given strins as single */

    #include

    #include

    main()

    {

    char x[20],y[10]; clrscr();

    puts(" Pls Enter The First Part of the String :");

  • 7/28/2019 c Language by SRIDHAR

    99/144

    gets(x);

    puts(" Pls Enter The Last Part of the String :");

    gets(y);

    strcat(x,y);

    printf(" Concatenated string is %s",x);

    printf("\n Last Part of the String is %s",y);

    getch();

    }

    FUNCTIONS

    (Self contained block or program used for a particular sub task)

    As 'C' is a modular programming language, function is a block or module of a program consisting

    of set of ordered statements related to perform a particular sub task, having a unique identity known as

    function name.

    Features:

    => Easy readability and understandability

    => Easy Debugging of the errors

    => Provides Modular programming feature

    => Reusability of the code

    => Sharing of the work

    => Reduces the code and saves time and money, etc.

    In 'C', functions are classified into three types:

    1. Library Functions

  • 7/28/2019 c Language by SRIDHAR

    100/144

    2. Macros / Defined Functions

    3. User Defined Functions

    1. Library Functions:

    These are the pre-defined functions of the 'C' language that are available within the compiler in

    the form of library files known as Header files.

    Ex:

    printf( ), scanf( ), getchar( ), clrscr( ), strlen( ), etc.

    2. Macros / Defined Functions:

    These are the defined functions consisting of single executable expression, which are to be

    defined using "#define" directive, as pre-processor directive statement.

    Syntax:

    #define ([])

    Ex:

    #define Sum(a,b) (a+b)

    #define Area(r) (3.14159*r*r)

    #define Big(x,y) (x>y)?x:y

    /* To find area of a circle by defining a Macro */

    #include

    #define Area(x) (3.14159*x*x)

    main( )

    {

    int r; clrscr();

    printf(" Enter radius of a Circle :");

  • 7/28/2019 c Language by SRIDHAR

    101/144

    scanf("%d" , &r);

    printf(" Area of Circle = %f", Area(r));

    getch();

    }

    /* To find biggest and smallest among two given numbers by defining Macros using Ternary Operators

    */

    #include

    #define Max(a,b) (a>b)?a:b

    #define Min(a,b) (a Write a program to find biggest and smallest among three given numbers by defining macros using

    ternary operators

    3. User-Defined Functions:

    These are the functions that are defined by the user for his own need or requirement, in the

    form of separate module / block by grouping the necessary set of statements related to perform a

    particualr sub task.

  • 7/28/2019 c Language by SRIDHAR

    102/144

    Each function is assigned with a unique identity known as functionname. Functions may or may

    not takes parameters and similarly they may or may not returns value / data.

    To manipulate user defined functions in 'C', follow the following three steps:

    1. Function Declaration (Prototype Statements)

    2. Function Definition

    3. Function Call / Invoke

    1. Function Declaration:

    The first step to manipulate user defined functions is to declare the functions, to give the

    necessary specification to 'C' compiler about the functionname, number and type of parameters and

    type of return value.

    The statements used for declaration of functions are known as function prototype statements.

    Syntax: Formal

    [] ( [] );

    Where

    rt-type => return data type name (default it takes "int" type)

    Functionname => Function identity

    Parameters list => The type of data items passed from the calling point

    Ex:

    1. int Sum(int,int);

    OR

    Sum(int , int);

    2. float Area( int );

    3. int Big(int , int , int);

    4. int Reverse( int );

  • 7/28/2019 c Language by SRIDHAR

    103/144

    2. Function Definition:

    The second step after function prototype statements is to define the functions, which is nothing

    but associating the necessary code(statements) to the functions in the form of separate blocks/modules,

    may be before the main() or after the main() block.

    Function definition consists of two parts:

    1. Function Header Row

    2. Function Body

    Syntax: Formal

    [] ( [] ) -> Fn Header Row

    {

    Statement 1;

    .................... -> Fn Body

    Statement n;

    }

    Ex:

    int Sum(int x,int y)

    {

    return(x+y);

    } OR

    Sum(x , y)

    int x , y;

    {

    return(x+y);

    }

  • 7/28/2019 c Language by SRIDHAR

    104/144

    3. Function Call / Invoke :

    The last step in manipulation of user defined functions is to call or invoke the functions defined

    within the main() block and pass the necessary data in the form of parameters, for their execution.

    Syntax: Actual

    ( [] );

    Ex:

    c=Sum(10,20); OR printf(" Sum = %d ", Sum(a,b));

    => Parameters :

    These are the arguments that represents the data, to be transferred between two blocks of a

    program. In 'C', parameters are classified into two types:

    1. Actual Parameters

    2. Formal Parameters

    Actual Parameters are the arguments that are used for passing the necessary data to the

    function called from its calling point, which may a constant, an operand, an operator, a formula, a sub

    function call.

    Formal Parameters are the arguments that are used for receiving the data passed from the

    calling point, which are used within the function header row (fn definition). As the formal parameters

    are used for the first time within the function, they needed to be declared there and then. The number

    and type of formal parameters should be equal and same as that of actual parameters passed from the

    calling point.

    /* To find the area of a circle by defining a UDF */

    #include

    float Area(int); /* Fn Prototype Statement */

    main( )

  • 7/28/2019 c Language by SRIDHAR

    105/144

    {

    int r;

    clrscr();

    printf(" Enter radius of a Circle :");

    scanf("%d",&r);

    printf(" Area of a Circle = %f" , Area(r) ); /* Fn Call */

    getch();

    }

    float Area(int x) /* Fn Definition */

    {

    return ( 3.14159 * x * x );

    }

    /* To find biggest and smallest among two given nos by defining UDFs */

    #include

    main()

    {

    int m,n; clrscr();

    printf(" Enter two numbers :");

    scanf("%d%d",&m,&n);

    printf(" Biggest = %d ", Big(m,n) );

    printf("\n Smallest = %d ", Small(m,n) );

    getch();

    }

  • 7/28/2019 c Language by SRIDHAR

    106/144

    Big(int x,int y)

    {

    if(x>y)

    return(x);

    else

    return(y);

    }

    Small(int m,int n)

    {

    if(m To find the area and perimeter of a rectangle by defining UDFs

    => To find the smallest among three given numbers by defining UDF

    Type of User Defined Functions:

    Depending on the parameters and return value of the functions, they are divided into four

    types:

    1. Functions with parameters and with return value.

    2. Functions with parameters and no return value.

    3. Functions with no parameters but returns value.

  • 7/28/2019 c Language by SRIDHAR

    107/144

    4. Functions with no parameters and no return value.

    /* To swap the two given numbers by defining a UDF */

    /* Fn with parameters but no return value */

    #include

    swap(int x,int y)

    {

    int z;

    z=x;

    x=y;

    y=z;

    printf("\n After swapping values are %d and %d ",x,y);

    }

    main()

    {

    int a,b; clrscr( );

    printf(" Enter two values :");

    scanf("%d%d" , &a, &b);

    printf(" Before swapping values are %d and %d ",a,b);

    swap(a,b);

    getch();

    }

  • 7/28/2019 c Language by SRIDHAR

    108/144

    /* To find the factorial value for the given number by defining UDF */

    #include

    int fact(int x)

    {

    int f;

    for(f=1;x>=1; x--)

    f=f*x;

    return(f);

    }

    main()

    {

    int n; clrscr();

    printf(" Enter the required number :");

    scanf("%d" , &n);

    printf(" Factorial Value = %d ", fact(n) );

    getch();

    }

    /* To generate fibonnacci series below n by defining UDF */

    #include

    fibo(int n)

    {

  • 7/28/2019 c Language by SRIDHAR

    109/144

    int a,b;

    for(a=0 , b=1 ; b=65 && x=97 && x

  • 7/28/2019 c Language by SRIDHAR

    110/144

    int is_digit(char x)

    {

    if(x>=48 && x

  • 7/28/2019 c Language by SRIDHAR

    111/144

    if(is_digit(m))

    printf(" Given character is a digit ");

    else

    if(is_space(m))

    printf(" Given character is a blank space ");

    else

    printf(" Given character is a special character ");

    getch();

    }

    => Passing of Arrays as Parameters to Functions:

    In 'C', when we are in need to pass group of same type of data items as parameters to a single

    function, rather than passing them individually one by one, we can pass them together as a single

    parameter in the form of an array, which may be single or double dimensional format.

    To pass Single dimensional array as parameter to a function, use only the arrayname as actual

    parameter at the function calling point and declare same type of dummy array without subscript as

    formal parameter within the function header row. Here the dummy array is not a copy of the actual

    array, instead it is the reference for the actual array i.e. directly the original elements are manipulated

    by the dummy array (Call by Reference).

    /* To read and print 5 values into an array by defining UDFs */

    #include

    read_array(int x[])

    {

    int i;

    for(i=0 ; i

  • 7/28/2019 c Language by SRIDHAR

    112/144

    }

    main()

    {

    int n[5]; clrscr();

    printf(" Enter 5 values :\n");

    read_array(n);

    printf(" Given values are ");

    display_array(n);

    getch();

    }

    display_array(int x[])

    {

    int i;

    for(i=0 ; i Program to read 10 numbers into an array and find their sum by defining UDFs

    /* To find biggest among n given numbers into an array by defining UDFs */

    #include

    read_array(int m[ ],int n)

    {

  • 7/28/2019 c Language by SRIDHAR

    113/144

    int i;

    for(i=0 ; i

  • 7/28/2019 c Language by SRIDHAR

    114/144

    return(big);

    }

    /* To sort the n given numbers into an array in ascending order

    by defining UDFs */

    #include

    read_array(int m[],int n)

    {

    int i;

    for(i=0 ; i

  • 7/28/2019 c Language by SRIDHAR

    115/144

    display_array(x,n);

    getch();

    }

    sort_array(int m[],int n)

    {

    int i,j,temp;

    for(i=0 ; i

  • 7/28/2019 c Language by SRIDHAR

    116/144

    When we are in need to pass double dimensional array as parameter to a function, use only the

    array name as actual parameter at the function calling point and same type of double dimensional

    dummy array as formal parameter without specifying the row subscript but giving the column subscript.

    (Call by reference)

    /* To read and print elements into 3x4 matrix by defining UDFs */

    #include

    read_matrix(int m[][4])

    {

    int r,c;

    for(r=0 ; r

  • 7/28/2019 c Language by SRIDHAR

    117/144

    display_matrix(int m[][4])

    {

    int r,c;

    for(r=0 ; r

  • 7/28/2019 c Language by SRIDHAR

    118/144

    main( )

    {

    int a , b; clrscr( );

    printf(" Enter two numbers :");

    scanf("%d%d" , &a , &b);

    printf(" Sum = %d ", Sum(a,b) );

    getch( );

    }

    int Sum(int x, int y)

    {

    return(x+y);

    }

    --> Where

    a & b => Local to main ( )

    x & y => Local to Sum ( )

    2. Global Variables:

    These are the variables that are globalized i.e. their accessing is limited to the entire program

    that is in all the blocks / modules and gets released (dead) as soon as the program's execution is ended.

    These variables needed to be declared outside the blocks at the beginning of the program as Global

    Declaration statements.

    /* Sample program for global variables */

    #include

    int a , b;

  • 7/28/2019 c Language by SRIDHAR

    119/144

    main( )

    {

    clrscr( );

    printf(" Enter two numbers :");

    scanf(" %d%d ", &a , &b);

    printf(" Smallest = %d ", Small( ) );

    getch( );

    }

    int Small( )

    {

    if(a Where

    a & b => Global Variables for the entire program

    3. Static Variables:

    These are the variables whose memory remains unchangable through out the program

    execution for all the other remaining declaration of the variables with the same name.

    To declare static variables, preceed the variables declaration with "static" keyword.

  • 7/28/2019 c Language by SRIDHAR

    120/144

    Syntax:

    static ;

    ex:

    static float p;

    static int a,b;

    /* Sample program for static variables */

    #include

    main()

    {

    static int a,b;

    clrscr();

    printf(" Enter two numbers :");

    scanf("%d%d",&a,&b);

    printf(" Product of a and b = %d",product(a,b));

    getch();

    }

    int product(int a, int b)

    {

    return(a*b);

    }

    ---> Where

    a & b => static variables

  • 7/28/2019 c Language by SRIDHAR

    121/144

    /* Sample program for static variables */

    #include

    main()

    {

    static int x=50;

    clrscr();

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

    sample();

    getch();

    }

    sample( )

    {

    int x;

    x=x+5;

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

    }

    => Recursions:

    It is the concept of calling a function within it self for repeated n number of times, to reduce the

    code and to increase the execution speed of the program. In recursive function, we need to specify a

    condition to stop the recursive execution otherwise the program execution will enter into infinity loop.

    /* To find the factorial value for the given number */

    /* By using Non-recursive concept */

  • 7/28/2019 c Language by SRIDHAR

    122/144

  • 7/28/2019 c Language by SRIDHAR

    123/144

    printf(" Enter a number :");

    scanf("%d", &n);

    printf( " Factorial Value = %d",fact(n));

    getch();

    }

    int fact(int x)

    {

    if(x==1)

    return(1);

    else

    return(x * fact(x-1));

    }

    /* To swap the two given numbers by using global variables */

    /* Functions with no parameters and no return value */

    #include

    int x , y;

    swap( )

    {

    int temp;

    temp=x;

    x=y;

  • 7/28/2019 c Language by SRIDHAR

    124/144

    y=temp;

    }

    main()

    {

    clrscr();

    printf(" Enter two numbers :");

    scanf("%d%d",&x,&y);

    printf(" Before swapping x = %d and y = %d" , x , y );

    swap( );

    printf("\n After swapping x = %d and y = %d " , x , y );

    getch( );

    }

    POINTERS

    These are of non-primitive derived data type concept.

    Pointers are special variables that holds the address of the data stored, rather than directly

    holding the data (Unlike the ordinary variables). Pointers are mainly used for the following features:

    1. It increases the program execution speed, as it is directly pointing the address

    2. It is provides memory resident programming feature, using which we can directly manipulate

    or use the system's internal memory.

    3. It is also used for managing dynamic type of memory, which is allocated at the time of

    execution of the program and which is non-static in size.

    4. It is also used for increasing the size of an array at the time of program execution.

    5. It saves the time and also saves the memory

    6. It is also used for creation of new variables at run time, etc.

  • 7/28/2019 c Language by SRIDHAR

    125/144

    Follow the following format for declaration of pointer variables:

    Syntax

    *;

    Where

    typename => any primitive data type name

    * (Asterisk) => Pointer operator / Value at Address operator / Indirect operator

    Ex:

    int *p;

    float *f,*d;

    char *st;

    /* Sample program for pointers */

    #include

    main()

    {

    int n,*ptr; clrscr();

    ptr=&n;

    printf(" Enter a value :");

    scanf("%d",ptr);

    printf("\n Value at n = %d",n);

    printf("\n Address of n = %ld",&n);

    printf("\n Value at ptr = %ld",ptr);

    printf("\n Value at Address = %d", *(&n));

    printf("\n Value at pointer = %d",*ptr);

    getch();

  • 7/28/2019 c Language by SRIDHAR

    126/144

    }

    /* To read and print a character using pointer */

    #include

    main()

    {

    char ch,*p; clrscr();

    p=&ch;

    printf(" Enter a character :");

    *p=getchar();

    printf(" Given character = %c",*p);

    getch();

    }

    /* To read and print 5 values into an array using pointers */

    #include

    main()

    {

    int n[5],*p,i; clrscr();

    p=n; /* p=&n[0]; */

    printf(" Enter 5 values :");

    for(i=1 ; i

  • 7/28/2019 c Language by SRIDHAR

    127/144

    }

    printf(" Given values are :");

    for(p=n , i=1 ; i for(n-- , i=1 ; i To find sum of the given 10 numbers into an array using pointers

    /* To find biggest among the given 10 numbers into an array using pointers */

    #include

    main()

    {

    int n[10],*p,i,big; clrscr();

    p=n; /* p=&n[0]; */

    printf(" Enter 10 values :");

    for(i=1 ; i

  • 7/28/2019 c Language by SRIDHAR

    128/144

    printf(" \n Biggest = %d",big);

    getch();

    }

    => Function Calls:

    In 'C', the user defined functions can be called or invoked in two different methods, they are:

    1. Call by Value (Pass by Value)

    2. Call by Reference (Pass by Reference)

    In Call by Value method, we create dummy variables as formal parameters within the function

    header row and receive the copy of the actual parameters passed from the calling point, manipulating

    which within the function does not affects the original actual parameters data/value.

    /* To swap the two given numbers by defining a UDF */

    /* Using Call by Value method */

    #include

    swap(int x,int y)

    {

    int z;

    z=x;

    x=y;

    y=z;

    printf("\n After swapping values are %d and %d ",x,y);

    }

  • 7/28/2019 c Language by SRIDHAR

    129/144

    main()

    {

    int a,b; clrscr( );

    printf(" Enter two values :");

    scanf("%d%d" , &a, &b);

    printf(" Before swapping values are %d and %d ",a,b);

    swap(a,b);

    getch();

    }

    In Call by Reference method, we create pointer type of variables as formal parameters within

    the function header row and receives the reference (address) of the actual parameters from the calling

    point, manipulating which within the function directly affects the original actual parameters data/value.

    /* To swap the two given numbers by defining a UDF */

    /* Using Call by Reference method */

    #include

    swap(int *x, int *y)

    {

    int z;

    z=*x;

    *x=*y;

    *y=z;

  • 7/28/2019 c Language by SRIDHAR

    130/144

    }

    main()

    {

    int a,b; clrscr( );

    printf(" Enter two values :");

    scanf("%d%d" , &a, &b);

    printf(" Before swapping values are a = %d and b = %d ",a,b);

    swap(&a,&b);

    printf("\n After swapping values are a = %d and b = %d ", a, b);

    getch();

    }

    STRUCTURES

    These are of non-primitive user defined data type concept, which is encapsulating (grouping)

    collection of different type of data items together for storing related information, which can be referred

    by a single common identity known as Structure name or Tag name.

    In general, a structure can also be defined as "Collection of different type of data items grouped

    together for storing related information". The different data items grouped in a structure can be

    referred as members.

    To manipulate structure in 'C', we have to follow the following three steps:

    1. Structure Definition

    2. Declaration of Structure Variables

    3. Accessing the individual members

  • 7/28/2019 c Language by SRIDHAR

    131/144

    1. Defining a Structure:

    As structures are of user-defined data type concept, first we have to define the structure to give

    necessary specifications to the 'C' compiler, about the structure name and scopes of the members

    (member names and their types).

    Syntax:

    struct

    {

    ;

    ;

    .........................

    ;

    };

    Ex:

    struct Sample

    {

    int x;

    char y;

    float z;

    }; (Allocates 7 Bytes for Sample Structure Variable)

    The members of a structure can not only be of primitive data type, instead if required they can

    also be of non-primitive user defined data type, may be of pointers, arrays, sub-structures.

    Ex: 1.

    struct Student

    {

  • 7/28/2019 c Language by SRIDHAR

    132/144

    int rno,s1,s2,s3,total;

    char name[20],result[5];

    float avg;

    }; (Allocates 39 bytes for student structure variable)

    2.

    struct Emp

    {

    int eno;

    char *name;

    float basic;

    };

    3.

    struct date

    {

    int dd,mm,yy;

    };

    struct Stu

    {

    int rno;

    char name[20];

    struct date dob; --> Nesting of structures

    };

    2. Declaration of Structure Variables:

  • 7/28/2019 c Language by SRIDHAR

    133/144

    After defining a structure, we have to declare the variabels pertaining to the structure defined,

    for storing the data. The variables of a particular structure type are referred as structure variables

    (Objects).

    Syntax:

    struct ;

    Ex:

    struct Sample s;

    struct Student x;

    struct Emp e1,e2,e3;

    struct Stu x,y;

    When we are in need of three or more variables of same structure type, rather than declaring

    them individually one by one, we can declare them as array type may be of single or double dimensional

    format.

    Syntax:

    struct [Subscript]...;

    Ex:

    struct Student a[5];

    struct Emp e[3][2];

    3. Accessing the Individual Members of a Structure Variable:

    The last step in manipulation of structure is to access the individual members of a structure

    variable, by using structure variable name followed by member accessing operator ( . ) and member

    name.

    Syntax:

    .

    Ex:

    s.x x.rno e1.eno

  • 7/28/2019 c Language by SRIDHAR

    134/144

    s.y x.name e1.name

    s.z x.s1 ..... x.result .......x.avg e1.basic

    x.rno x.name x.dob.dd x.dob.yy x.dob.mm

    a[i].rno a[i].name ..... a[i].avg

    e[r][c].eno e[r][c].name e[r][c].basic

    /* To read and print a single student record using structures */

    #inc