Basic of C Program

Embed Size (px)

Citation preview

  • 8/12/2019 Basic of C Program

    1/86

    BASICS OF CPROGRAMMING

    By Prof. Ahlam S. A. Ansari

  • 8/12/2019 Basic of C Program

    2/86

    Structure of a C Program

    Documentation

    Header Files

    Symbolic Constants

    Global & Constant Variable Declaration

    Main Function

    Other Functions

    By- Prof. Ahlam Ansari2

  • 8/12/2019 Basic of C Program

    3/86

    Structure of Main function

    void main()

    { Initialization Statements clrscr(); Input Statements Processing Statements

    Output Statements getch();

    }

    By- Prof. Ahlam Ansari3

  • 8/12/2019 Basic of C Program

    4/86

    Fundamentals of C Programming

    CharacterSet

    Constants,Variables

    &Keywords

    Instructions Programs

    By- Prof. Ahlam Ansari4

  • 8/12/2019 Basic of C Program

    5/86

    Character Set

    Alphabets :Uppercase: A B C .................................... X Y ZLowercase: a b c ...................................... x y zDigits :0 1 2 3 4 5 6 8 9Special Characters :

    By- Prof. Ahlam Ansari5

    , < > . _ ( ) ; $ : % [ ] # ?' & { } " ^ ! * / | - \ ~ +

  • 8/12/2019 Basic of C Program

    6/86

    Fundamentals of C Programming

    CharacterSet

    Constants,Variables

    &Keywords

    Instructions Programs

    By- Prof. Ahlam Ansari6

  • 8/12/2019 Basic of C Program

    7/86

    Token

    Token

    Keyword Constant Identifier String Operator

    By- Prof. Ahlam Ansari7

    The smallest individual unit in a program isknown as a TOKEN.

  • 8/12/2019 Basic of C Program

    8/86

    Keywords

    auto double int struct

    break else long switch

    case enum register typedef

    char extern return union

    continue for signed void

    do if static whiledefault goto sizeof volatile

    const float short unsigned

    By- Prof. Ahlam Ansari8

    Keywords are the reserved words used in programming. Each keywords has fixedmeaning and that cannot be changed by user.

  • 8/12/2019 Basic of C Program

    9/86

    Identifiers

    In C programming, identifiers are names given to Centities, such as variables, functions, structures etc.Identifier are created to give unique name to C

    entities to identify it during the execution of program.

    By- Prof. Ahlam Ansari9

    Example:int sum;

    Here sum is an IDENTIFIER

    Example:struct student

    Here student is a IDENTIFIER

    Example: void add()Here add is a IDENTIFIER

  • 8/12/2019 Basic of C Program

    10/86

    Rules for writing identifier

    1. An identifier can be composed of letters, digits andunderscore '_' only.

    2. Uppercase and lowercase letters are distinct.3. The first letter of identifier should be either a letter

    or an underscore. But, it is discouraged to start anidentifier name with an underscore though it islegal.

    4. There is no rule for the length of an identifier.

    By- Prof. Ahlam Ansari10

  • 8/12/2019 Basic of C Program

    11/86

    Constant

    Constant

    Numeric

    Integer

    Ex. Whole No (15, -90)

    Real

    Ex. Decimal No (-12.33,34.44)

    Backslash

    Ex.\n,\t,\a,\v,\b,\f,\\,\

    Symbolic

    Ex. #define PI 3.14 ,#define A 3*3

    Character

    Single Character

    Ex. h, 6, @

    String

    Ex. C- Program

    By- Prof. Ahlam Ansari11

    Constants are the terms that can't be changed during theexecution of a program.

  • 8/12/2019 Basic of C Program

    12/86

    Defining Constant

    Method 1:const float pi=3.14;

    #include

    void main(){

    const float pi=3.14;

    .

    .

    .

    }

    By- Prof. Ahlam Ansari12

  • 8/12/2019 Basic of C Program

    13/86

    Continued...

    Method 2:#define pi 3.14

    #include

    #define pi 3.14void main()

    {

    .

    .

    .

    }

    By- Prof. Ahlam Ansari13

  • 8/12/2019 Basic of C Program

    14/86

    Backslash Constant

    \n New Line Character#includevoid main(){

    printf("Hello\n Class");}

    By- Prof. Ahlam Ansari14

    Output:

    HelloClass_

  • 8/12/2019 Basic of C Program

    15/86

    Backslash Constant

    \t Tab Character#includevoid main(){

    printf("Hello\t Class");}

    By- Prof. Ahlam Ansari15

    Output:

    Hello Class_

  • 8/12/2019 Basic of C Program

    16/86

    Backslash Constant

    \b BackspaceCharacter

    #includevoid main(){

    printf("Hello\b");}

    By- Prof. Ahlam Ansari16

    Output:

    Hell_

  • 8/12/2019 Basic of C Program

    17/86

    Backslash Constant

    \a Alert Character#includevoid main(){

    printf("Hello\a");}

    By- Prof. Ahlam Ansari17

    Output:

    Hello_Note: After output a

    System sound beep isheard.

  • 8/12/2019 Basic of C Program

    18/86

    Backslash Constant

    \r Return CarriageCharacter

    #includevoid main(){

    printf("Hello\r");}

    By- Prof. Ahlam Ansari18

    Output:

    _ello

  • 8/12/2019 Basic of C Program

    19/86

    Backslash Constant

    \f Form FeedCharacter

    #includevoid main(){

    printf("Hello\n Class\f");}

    By- Prof. Ahlam Ansari19

    Output:

    _elloClass

  • 8/12/2019 Basic of C Program

    20/86

    Backslash Constant

    \\ , \ and \ Character#includevoid main(){

    printf(\\ \ \");}

    By- Prof. Ahlam Ansari20

    Output:

    \ _

  • 8/12/2019 Basic of C Program

    21/86

    Variable

    Variables are memory location in computer'smemory to store data. To indicate the memorylocation, each variable should be given a unique

    name called identifier. Variable names are just thesymbolic representation of a memory location.

    By- Prof. Ahlam Ansari21

    Example:int num;

    Here num is a VARIABLE

  • 8/12/2019 Basic of C Program

    22/86

    Rules for writing Variable

    1. Variable can be composed of letters, digits andunderscore '_' only.

    2. Uppercase and lowercase letters are distinct.3. The first letter of variable should be either a letter

    or an underscore. But, it is discouraged to start anidentifier name with an underscore though it is

    legal.4. There is no rule for the length of an variable.

    By- Prof. Ahlam Ansari22

  • 8/12/2019 Basic of C Program

    23/86

    Scope of a VariableBy- Prof. Ahlam Ansari23

    Global VariablesThese variables aredeclared outside allfunctions.

    Life time of a globalvariable is the entireexecution period of the

    program.

    Can be accessed by any

    function defined below thedeclaration, in a file.

    /* Compute Area and Perimeter of acircle */

    #include float pi = 3.14159; /* Global */

    main() {

    float rad; /* Local */

    printf( Enter the radius );scanf(%f , &rad);

    if ( rad > 0.0 ) {float area = pi * rad * rad;float peri = 2 * pi * rad;

    printf( Area = %f\n , area ); printf( Peri = %f\n , peri );

    }else

    printf( Negative radius\n);

    printf( Area = %f\n , area );}

  • 8/12/2019 Basic of C Program

    24/86

    Continued....By- Prof. Ahlam Ansari24

    Local VariablesThese variables aredeclared inside somefunctions.

    Life time of a localvariable is the entireexecution period of thefunction in which it isdefined.

    Cannot be accessed by any

    other function.In general variablesdeclared inside a blockare accessible only inthat block.

    /* Compute Area and Perimeter of acircle */

    #include float pi = 3.14159; /* Global */

    main() {float rad, area, peri; /* Local */

    printf( Enter the radius );scanf(%f , &rad);

    if ( rad > 0.0 ) {area = pi * rad * rad;

    peri = 2 * pi * rad;

    printf( Area = %f\n , area ); printf( Peri = %f\n , peri );

    }else

    printf( Negative radius\n);

    printf( Area = %f\n , area );}

  • 8/12/2019 Basic of C Program

    25/86

    Tips for Good Programming PracticeBy- Prof. Ahlam Ansari25

    2. Programmer must write the program with proper indentation.

    1. Programmer can choose the name of identifier whatever theywant. However, if the programmer choose meaningful name for anidentifier, it will be easy to understand and work on, particularly in

    case of large program.

  • 8/12/2019 Basic of C Program

    26/86

    Exercise- Find out the different tokens

    /*Program to calculate area of circle*///Developed By: Ahlam Ansari#include#includevoid main(){

    float radius=2.2, area;const float pi=3.14;clrscr();area=pi*radius*radius;printf(\n The area of circle with radius %fcm is %fcm.sq ,radius, area);getch();

    }

    By- Prof. Ahlam Ansari26

  • 8/12/2019 Basic of C Program

    27/86

    Exercise- Find out the different tokens

    /*Program calculate area of triangle*///Developed By: Ahlam Ansari#include#include#define half 0.5void main(){

    int base=2, height=4;float area;clrscr();area=half*base*height;printf(The area of triangle is %fcm.sq , area);getch();

    }

    By- Prof. Ahlam Ansari27

  • 8/12/2019 Basic of C Program

    28/86

    Fundamentals of C Programming

    CharacterSet

    Constants,Variables

    &Keywords

    Instructions Programs

    By- Prof. Ahlam Ansari28

  • 8/12/2019 Basic of C Program

    29/86

    Instruction/ Statement

    Instruction/Statement

    TypeDeclaration

    Statement

    Input- OutputStatement

    OperationStatement

    ControlStatement

    By- Prof. Ahlam Ansari29

  • 8/12/2019 Basic of C Program

    30/86

    Instruction/ Statement

    Instruction/Statement

    TypeDeclaration

    Statement

    Input- OutputStatement

    OperationStatement

    ControlStatement

    By- Prof. Ahlam Ansari30

  • 8/12/2019 Basic of C Program

    31/86

    Type Declaration Statement

    Data Type

    In- Built/ Basic/Primitive Data

    Type

    Ex. Int, float, char,long, double,

    void, long double

    Derived DataType

    Ex. Array,Function, Pointer

    User DefinedData Type

    Ex. Structure,Union,

    Enumeration

    By- Prof. Ahlam Ansari31

  • 8/12/2019 Basic of C Program

    32/86

    Syntax:datatype identifier1[,identifiers2,...,identifiern];

    Example:int a,b,c;float x;char ch=C;

    By- Prof. Ahlam Ansari32

  • 8/12/2019 Basic of C Program

    33/86

    More Examples:int sum=10;float radius=20.1;

    char ch=A;int add();void display();

    struct student {..};long ar[50];int *ptr;

    By- Prof. Ahlam Ansari33

  • 8/12/2019 Basic of C Program

    34/86

    Type Format Specifier Memory Allocation Typical Range

    Bytes Bits

    char %c 1byte 8 bits -127 to 127 or 0 to 255

    unsigned char %c 1byte 8 bits 0 to 255

    signed char %c 1byte 8 bits -127 to 127

    int %d or %i 2 bytes 16 bits -2147483648 to 2147483647unsigned int %u 2 bytes 16 bits 0 to 4294967295

    signed int %d or %i 2 bytes 16 bits -2147483648 to 2147483647

    short int %hd 2 bytes 16 bits -32768 to 32767

    unsigned short int %u 2 bytes 16 bits 0 to 65,535

    signed short int %hd 2 bytes 16 bits -32768 to 32767

    long int %ld 4 bytes 32 bits -2,147,483,647 to 2,147,483,647

    signed long int %ld 4 bytes 32 bits same as long int

    unsigned long int %u 4 bytes 32 bits 0 to 4,294,967,295

    float %f 4 bytes 32 bits +/- 3.4e +/- 38 (~7 digits)

    double %lf 8 bytes 64 bits +/- 1.7e +/- 308 (~15 digits)

    long double %Lf 10 bytes 80 bits +/- 1.7e +/- 308 (~15 digits)

    By- Prof. Ahlam Ansari34

  • 8/12/2019 Basic of C Program

    35/86

    Tips to calculate range

    Example:1. Unsigned char -> 8bitsMinimum Range 0Maximum Range 28 1 => 256-1=> 255

    2. Signed char -> 8bits

    Minimum Range -(28 /2 ) => -(256/2) => -128Maximum Range (28 /2) 1 => 128-1=> 127

    By- Prof. Ahlam Ansari35

    f

  • 8/12/2019 Basic of C Program

    36/86

    Exercise Find the size of thedatatypes#include void main(){

    int a; float b; double c; char d;printf("Size of int=%d bytes\n",sizeof(a));printf("Size of float=%d bytes\n",sizeof(b));printf("Size of double=%d bytes\n",sizeof(c));printf("Size of char=%d byte\n",sizeof(d));

    }

    By- Prof. Ahlam Ansari36

  • 8/12/2019 Basic of C Program

    37/86

    More Format Specifier

    %o The unsigned octal format specifier.%s The string format specifier.%x The unsigned hexadecimal formatspecifier.%% Outputs a percent sign.

    By- Prof. Ahlam Ansari37

  • 8/12/2019 Basic of C Program

    38/86

    The operands of a binary operator must have a the same type and theresult is also of the same type.Integer division:

    c = (9 / 5)*(f - 32)

    The operands of the division are both int and hence the result also wouldbe int. For correct results, one may writec = (9.0 / 5.0)*(f - 32)

    In case the two operands of a binary operator are different, butcompatible, then they are converted to the same type by the compiler.The mechanism (set of rules) is called Automatic Type Casting.

    c = (9.0 / 5)*(f - 32)

    It is possible to force a conversion of an operand. This is called ExplicitType casting .c = ((float) 9 / 5)*(f - 32)

    By- Prof. Ahlam Ansari38

    Data-Type Conversion

  • 8/12/2019 Basic of C Program

    39/86

    char

    By- Prof. Ahlam Ansari39

    char can be converted to short int.

    Note

    sizeof (char) sizeof (short int)

  • 8/12/2019 Basic of C Program

    40/86

    long int

    int

    shortint

    By- Prof. Ahlam Ansari40

    AutomaticData-TypePromotion

    sizeof (short) sizeof (int) sizeof (long)

    Lower data types are converted to the higher data types and result is of higher type.

    Note

  • 8/12/2019 Basic of C Program

    41/86

    long double

    double

    float

    By- Prof. Ahlam Ansari41

    AutomaticData-TypePromotion

    Lower data types are converted to the higher data types and result is of higher type.

    Note

    sizeof (float) sizeof (double) sizeof (long double)

    E i Wh t i th lt t

  • 8/12/2019 Basic of C Program

    42/86

    Exercise What is the resultantdata-type

    float f; double d; long l;int i; short s;

    1. d + f f will be converted to doubl e

    2. i / s s will be converted to i nt3. l / i i is converted to l ong ; l ong result

    By- Prof. Ahlam Ansari42

    It i g ll g d ti

  • 8/12/2019 Basic of C Program

    43/86

    Type CastingBy- Prof. Ahlam Ansari43

    long double

    double

    float

    long int

    intshort

    int

    char

    The general form of a type casting operator is (type-name) expression.It is used to convert cross data-types.

    It is generally a good practiceto use explicit casts than to relyon automatic type conversions.

  • 8/12/2019 Basic of C Program

    44/86

    Syntax & Example

    Syntax:

    (Data-Type)Variable_name/Expression

    Example:C = (float)9 / 5 * ( f 32 )

    X = (long) (9.0 * 5 / 2)

    By- Prof. Ahlam Ansari44

    1. f l oat to i nt conversion causes truncation of fractional part2. doubl e to f l oat conversion causes rounding of digits3. l ong i nt to i nt causes dropping of the higher order bits.

    Note

  • 8/12/2019 Basic of C Program

    45/86

    Exercise Find the output

    #include void main(){

    int i = 17;char c = 'c'; /* ascii value is 99 */float sum;

    sum = (float)(i + (int)c);printf("Value of sum : %f\n", sum );

    }

    Output: Value of sum : 116.000000

    By- Prof. Ahlam Ansari45

  • 8/12/2019 Basic of C Program

    46/86

    Instruction/ Statement

    Instruction/Statement

    TypeDeclarationStatement

    Input- OutputStatement

    OperationStatement

    ControlStatement

    By- Prof. Ahlam Ansari46

  • 8/12/2019 Basic of C Program

    47/86

    Input Output Statement

    1. scanf() and printf()include void main()

    {int num;printf("Enter the number : ");

    scanf(%d, &num);printf("You entered: %d", num);}

    By- Prof. Ahlam Ansari47

  • 8/12/2019 Basic of C Program

    48/86

    2. getchar() and putchar()include void main()

    {char ch;printf("Enter a character : ");

    ch=getchar(stdin);putchar(ch,stdout);}

    By- Prof. Ahlam Ansari48

  • 8/12/2019 Basic of C Program

    49/86

    3. getc() and putc()include void main()

    {char ch;printf("Enter a character : ");

    ch=getc ();putc(ch);}

    By- Prof. Ahlam Ansari49

  • 8/12/2019 Basic of C Program

    50/86

    4. gets() and puts()include void main()

    {char str[50];printf("Enter a string : ");

    gets(str);puts(str);}

    By- Prof. Ahlam Ansari50

  • 8/12/2019 Basic of C Program

    51/86

    5. getch() and putch()include void main()

    { char ch;printf("Enter a character : ");ch=getch();putch(ch);

    }

    By- Prof. Ahlam Ansari51

  • 8/12/2019 Basic of C Program

    52/86

    6. getche()include void main()

    { char ch;printf("Enter a character : ");c=getche();putch(ch);

    }

    By- Prof. Ahlam Ansari52

    Difference between getc() getch()

  • 8/12/2019 Basic of C Program

    53/86

    Difference between getc(), getch(),getche() and getchar()

    getc(): Reads a character only after Enter key ispressed.getch(): Reads a character and never waits for Enterkey. Character gets processed after getting any keypressed.getche(): It works same as getch() but it echoes onscreen.getchar(): It works differently from the other two.Whenever you press any key these are kept in Buffer.After hitting enter the first character gets processedand it echoes on the screen.

    By- Prof. Ahlam Ansari53

  • 8/12/2019 Basic of C Program

    54/86

    Instruction/ Statement

    Instruction/Statement

    TypeDeclarationStatement

    Input- OutputStatement

    OperationStatement

    ControlStatement

    By- Prof. Ahlam Ansari54

  • 8/12/2019 Basic of C Program

    55/86

  • 8/12/2019 Basic of C Program

    56/86

    Example of Arithmetic Operator

    #include void main()

    {

    int a=9,b=4,c;

    c=a+b;

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

    c=a-b;

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

    c=a*b;

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

    printf("a/b=%d\n",c);

    c=a%b;

    printf("Remainder when a divided by b=%d\n",c);

    }

    By- Prof. Ahlam Ansari56

  • 8/12/2019 Basic of C Program

    57/86

    Example of Assignment Operator

    #include void main(){

    int a=9,b=4,c;printf(A=%d and B= %d\n",a,b);c=a;a=b;b=c;printf(A=%d and B= %d\n",a,b);

    }

    By- Prof. Ahlam Ansari57

    Example of Short Hand Assignment

  • 8/12/2019 Basic of C Program

    58/86

    Example of Short Hand AssignmentOperator

    Operator Example Same as

    = a=b a=b

    += a+=b a=a+b

    -= a-=b a=a-b*= a*=b a=a*b

    /= a/=b a=a/b

    %= a%=b a=a%b

    By- Prof. Ahlam Ansari58

  • 8/12/2019 Basic of C Program

    59/86

    Example of Logical Operator

    Operator Meaning of Operator Example

    && Logial AND If c=5 and d=2 then,((c==5) && (d>5)) returns false.

    || Logical ORIf c=5 and d=2 then,(c==5) || (d>5)) returns true.

    ! Logical NOT If c=5 then,!(c==5) returns false.

    By- Prof. Ahlam Ansari59

  • 8/12/2019 Basic of C Program

    60/86

    Example of Unary Operator

    #include void main(){

    int a=-9,b=4;printf(A=%d and B= %d\n",a,b);a=-a;b=-b;printf(A=%d and B= %d\n",a,b);

    }

    By- Prof. Ahlam Ansari60

  • 8/12/2019 Basic of C Program

    61/86

    Example of Relational Operator

    Operator Meaning of Operator Example

    == Equal to 5==3 returnsfalse (0)

    > Greater than 5>3 returns true(1)

    < Less than 5=Greater than orequal to

    5>=3 returns true(1)

  • 8/12/2019 Basic of C Program

    62/86

    Example of Bitwise Operator

    Operators Meaning of operators Example

    & Bitwise AND 5&3 returns (1)

    | Bitwise OR 5|3 returns (7)

    ^ Bitwise exclusive

    OR5^3 returns (6)

    ~ Bitwise complement ~5 returns (10)

    >3 return (1)

    By- Prof. Ahlam Ansari62

  • 8/12/2019 Basic of C Program

    63/86

    Bitwise AND

    Variable b 3 b2 b1 b0X =5 0 1 0 1

    Y= 3 0 0 1 1

    z = x & y 0 0 0 1

    By- Prof. Ahlam Ansari63

    x y x & y0 0 00 1 01 0 0

    1 1 1

  • 8/12/2019 Basic of C Program

    64/86

    Bitwise OR

    Variable b 3 b2 b1 b0X =5 0 1 0 1

    Y= 3 0 0 1 1

    z = x | y 0 1 1 1

    By- Prof. Ahlam Ansari64

    x y x | y0 0 00 1 11 0 1

    1 1 1

  • 8/12/2019 Basic of C Program

    65/86

    Bitwise XOR

    Variable b 3 b2 b1 b0X =5 0 1 0 1

    Y= 3 0 0 1 1

    z = x ^ y 0 1 1 0

    By- Prof. Ahlam Ansari65

    x y x ^ y0 0 00 1 11 0 1

    1 1 0

  • 8/12/2019 Basic of C Program

    66/86

    Bitwise NOT

    Variable b 3 b2 b1 b0X =5 0 1 0 1

    z = ~x 1 0 1 0

    By- Prof. Ahlam Ansari66

    x ~x0 11 0

  • 8/12/2019 Basic of C Program

    67/86

    Bitwise LEFT SHIFTBy- Prof. Ahlam Ansari67

    b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10

    b11

    b12

    b13

    b14

    b15

    Z=10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0Z

  • 8/12/2019 Basic of C Program

    68/86

  • 8/12/2019 Basic of C Program

    69/86

    Example of ternary operator

    #include void main(){

    int num;char result[5];printf("Enter a number ");scanf("%d",&num);result= (num % 2 == 0)?even:odd;printf(%d is %s,num,result);

    }

    By- Prof. Ahlam Ansari69

  • 8/12/2019 Basic of C Program

    70/86

    include void main(){

    int num;printf("Enter integer number : ");scanf(%d, &num);num%2==0 ? printf("%d is a even number",num) :printf("%d is an odd number",num);

    }

    By- Prof. Ahlam Ansari70

    Example of Increment Decrement

  • 8/12/2019 Basic of C Program

    71/86

    Example of Increment DecrementOperator

    S no Operator type Operator Description

    1 Pre increment++i Value of i is incrementedbefore assigning it to

    variable i.

    2 Post - incrementi++ Value of i is incrementedafter assigning it to variable

    i.

    3 Pre decrement i Value of i is decremented

    before assigning it tovariable i.

    4 Post _decrementi Value of i is decrementedafter assigning it to variable

    i.

    By- Prof. Ahlam Ansari71

    d

  • 8/12/2019 Basic of C Program

    72/86

    Operator Precedence

    Operator Description Associativity

    ( )[ ].

    ->++ --

    Parentheses (function call)(see Note 1)Brackets (array subscript)Member selection via objectname

    Member selection via pointer Postfix increment/decrement(see Note 2)

    left-to-right

    ++ --+ -

    ! ~(type )*&

    sizeof

    Prefix increment/decrementUnary plus/minusLogical negation/bitwisecomplementCast (convert value totemporary value of type )Dereference

    Address (of operand)Determine size in bytes onthis implementation

    right-to-left

    By- Prof. Ahlam Ansari72

    C i d

  • 8/12/2019 Basic of C Program

    73/86

    Continued....By- Prof. Ahlam Ansari73 Operator Description Associativity

    * / % Multiplication/division/modulusleft-to-right

    + - Addition/subtraction left-to-right

    > Bitwise shift left, Bitwise shiftrightleft-to-right

    < >=

    Relational less than/less thanor equal toRelational greaterthan/greater than or equal to

    left-to-right

    == != Relational is equal to/is notequal toleft-to-right

    & Bitwise AND left-to-right

    ^ Bitwise exclusive OR left-to-right

    | Bitwise inclusive OR left-to-right

    && Logical AND left-to-right

    | | Logical OR left-to-right

    ? : Ternary conditional right-to-left

    C i d

  • 8/12/2019 Basic of C Program

    74/86

    Continued....By- Prof. Ahlam Ansari74

    Operator Description Associativity

    =+= -=*= /=

    %= &=^= |=

    =

    Assignment Addition/subtractionassignmentMultiplication/division

    assignmentModulus/bitwise ANDassignmentBitwise exclusive/inclusiveOR assignmentBitwise shift left/rightassignment

    right-to-left

    , Comma (separateexpressions)left-to-right

    F d l f C P i

  • 8/12/2019 Basic of C Program

    75/86

    Fundamentals of C Programming

    CharacterSet

    Constants,Variables

    &Keywords

    Instructions Programs

    By- Prof. Ahlam Ansari75

    S d d Lib F i

  • 8/12/2019 Basic of C Program

    76/86

    Standard Library Functions

    Library functions in C language are inbuilt functions which are grouped togetherand placed in a common place called library.

    Each library function in C performs specific operation.

    We can make use of these library functions to get the pre-defined output instead ofwriting our own code to get those outputs.

    These library functions are created by the persons who designed and created Ccompilers.

    All C standard library functions are declared in many header files which are savedas file_name.h.

    Actually, function declaration, definition for macros are given in all header files.

    We are including these header files in our C program using#include command to make use of the functions those are declaredin the header files.

    When we include header files in our C program using #includecommand, all C code of the header files are included in C program. Then, this C

    program is compiled by compiler and executed.

    By- Prof. Ahlam Ansari76

    di h

  • 8/12/2019 Basic of C Program

    77/86

    stdio.h

    S.no Function Description

    1 printf()This function is used to print the character, string,float, integer, octal and hexadecimal values onto theoutput screen

    2 scanf() This function is used to read a character, string,numeric data from keyboard.

    3 getc() It reads character from file

    4 gets() It reads line from keyboard

    5 getchar() It reads character from keyboard

    6 puts() It writes line to o/p screen

    7 putchar() It writes a character to screen

    By- Prof. Ahlam Ansari77

    i h

  • 8/12/2019 Basic of C Program

    78/86

    conio.h

    S.no Function Description

    1 clrscr() This function is used to clear theoutput screen.

    2 getch() It reads character from keyboard

    3 getche() It reads character from keyboard andechoes to o/p screen

    By- Prof. Ahlam Ansari78

    t i h

  • 8/12/2019 Basic of C Program

    79/86

    string.h

    S no String functions Description

    1 strcat(str1, str2) Concatenates str2 at the end of str1.

    2 strcpy(str1, str2) Copies str2 into str1

    3 strlen(strl) gives the length of str1.

    4 strcmp(str1, str2)Returns 0 if str1 is same as str2. Returns

    0 if str1 >str2.

    5 strchr(str1,char) Returns pointer to first occurrence of charin str1.

    6 strstr(str1, str2) Returns pointer to first occurrence of str2in str1.

    7 strcmpi(str1,str2)Same as strcmp() function. But, this

    function negotiates case. A and a aretreated as same.

    By- Prof. Ahlam Ansari79

    C ti d

  • 8/12/2019 Basic of C Program

    80/86

    Continued..

    S no

    Stringfunctions

    Description

    8 strdup() duplicates the string9 strlwr() converts string to lowercase10 strncat() appends a portion of string to another

    11 strncpy() copies given number of characters of onestring to another

    12 strrchr() last occurrence of given character in astring is found13 strrev() reverses the given string

    14 strset() sets all character in a string to givencharacter15 strupr() converts string to uppercase

    By- Prof. Ahlam Ansari80

    th h

  • 8/12/2019 Basic of C Program

    81/86

    math.h

    S no Function Description

    1 floor ( ) This function returns the nearest integer which is lessthan or equal to the argument passed to this function.

    2 round ( )

    This function returns the nearest integer value of thefloat/double/long double argument passed to thisfunction. If decimal value is from .1 to .5 , itreturns integer value less than the argument. If decimalvalue is from .6 to .9 , it returns the integer valuegreater than the argument.

    3 ceil ( )This function returns nearest integer value which isgreater than or equal to the argument passed to thisfunction.

    4 sin ( ) This function is used to calculate sine value.

    5 cos ( ) This function is used to calculate cosine.

    6 cosh ( ) This function is used to calculate hyperbolic cosine.

    7 exp ( ) This function is used to calculate the exponential e to

    the xth

    power.

    By- Prof. Ahlam Ansari81

    C ti d

  • 8/12/2019 Basic of C Program

    82/86

    Continued...By- Prof. Ahlam Ansari82

    S no Function Description

    8 tan ( ) This function is used to calculate tangent.

    9 tanh ( ) This function is used to calculate hyperbolic tangent.

    10 sinh ( ) This function is used to calculate hyperbolic sine.

    11 log ( ) This function is used to calculates natural logarithm.

    12 log10 ( ) This function is used to calculates base 10 logarithm.

    13 sqrt ( ) This function is used to find square root of the argumentpassed to this function.

    14 pow ( ) This is used to find the power of the given number.

    15 trunc() This function truncates the decimal value from floatingpoint value and returns integer value.

    tdlib h

  • 8/12/2019 Basic of C Program

    83/86

    stdlib.hBy- Prof. Ahlam Ansari83

    S no Function Description

    1. abs()

    This function returns the absolute value of aninteger . The absolute value of a

    number is always positive. Only integer values aresupported in C.2. abort() It terminates the C program

    3. rand() This function returns the random integer numbers

    4. delay() This function Suspends the execution of the program

    for particular time

    ct pe h

  • 8/12/2019 Basic of C Program

    84/86

    ctype.hBy- Prof. Ahlam Ansari84

    S.no Function Description

    1 isalpha() checks whether character is alphabetic

    2 isdigit() checks whether character is digit

    3 isalnum() checks whether character is alphanumeric

    4 isspace() checks whether character is space

    5 islower() checks whether character is lower case

    6 isupper() checks whether character is upper case

    7 tolower() checks whether character is alphabetic & convertsto lower case

    8 toupper() checks whether character is alphabetic & convertsto upper case

    time h

  • 8/12/2019 Basic of C Program

    85/86

    time.hBy- Prof. Ahlam Ansari85

    S.no Function Description

    1 setdate() This function used to modify the system date

    2 getdate() This function is used to get the CPU time

    3 clock() This function is used to get current system time

    4 time() This function is used to get current system time asstructure

    Assignment

  • 8/12/2019 Basic of C Program

    86/86

    Assignment

    1. Write at least THREE Programs based on the topiccovered.

    2. Write notes on data-types.3. Explain Data-type Conversion.4. Write notes on operators used in C programming.5. Explain any five library functions.

    By- Prof. Ahlam Ansari86