+ All Categories
Home > Documents > C PGMMING

C PGMMING

Date post: 04-Apr-2018
Category:
Upload: kaleeswaran-einstein
View: 215 times
Download: 0 times
Share this document with a friend

of 110

Transcript
  • 7/30/2019 C PGMMING

    1/110

    C Programming Basics

    1/17/2013 1

  • 7/30/2019 C PGMMING

    2/110

    Structure of C Language

    1. Documetation Section [optional]

    /*------------*/

    2. Link Section

    #include

    3. Definition of constants [optional]#define variable value

    4. Global variable declaration [optional] datatype variable

    5. main()

    { declaration;executable statements; }

    6. Sub functions

    1/17/2013 2

  • 7/30/2019 C PGMMING

    3/110

    Data types

    The following are the datatypes available in c-Language.

    * Primary Datatypes

    * Derived Datatypes

    * User defined Datatypes

    Derived Datatypes - Arrays, stringsUser defined Datatypes - structures, unions

    1/17/2013 3

  • 7/30/2019 C PGMMING

    4/110

    Primary Datatypes

    Type Size in bits Range

    char 8[1 byte] -128 to 127

    Unsigned char 8[1 byte] 0 to 255

    signed char 8[1 byte] -128 to 127

    int 16[2 bytes] -32,768 to 32,767Unsigned short int 8[1byte] 0 to 65,535

    signed short int 8[1 byte] 0 to 65,535

    long int 32[4 bytes] -2,147,483,647 to 2,147,483,646

    signed long int 32[4 bytes] -2,147,483,647 to 2,147,483,646

    unsigned long int 32[4 bytes] 0 to 4,294,967,295

    float 32[4 bytes] 6 digits of precisiondouble 64[8 bytes] 10 digits of precision

    long double 128[16 bytes] 10 digits of precision

    1/17/2013 4

  • 7/30/2019 C PGMMING

    5/110

    # include

    # include

    Void main(){

    Int a=32769;

    Printf(%d,a);----------------- -32767}

    1/17/2013 5

  • 7/30/2019 C PGMMING

    6/110

    # include

    # include

    Void main(){

    Unsigned Int a=32769;

    Printf(%d,a);}

    1/17/2013 6

  • 7/30/2019 C PGMMING

    7/110

    Derived Datatypes - Arrays,

    strings

    User defined Datatypes structures,

    unions

    1/17/2013 7

  • 7/30/2019 C PGMMING

    8/110

    printf()

    * Used to display the output by using

    conversion specifiers

    * Returns no.of characters printed on the

    screen.

    1/17/2013 8

  • 7/30/2019 C PGMMING

    9/110

    prints the simple message on the

    screen

    #include

    main()

    { printf("Welcome to c world"); }

    1/17/2013 9

  • 7/30/2019 C PGMMING

    10/110

    #include

    main()

    {int i;

    i=printf("welcome%d",i);

    printf(%d",i);}

    1/17/2013 10

  • 7/30/2019 C PGMMING

    11/110

    Output

    Welcome 7

    1/17/2013 11

  • 7/30/2019 C PGMMING

    12/110

    B

    1/17/2013 12

  • 7/30/2019 C PGMMING

    13/110

    scanf() used to read the data from the input

    device (keyboard).

    scanf() Returns no.of inputs taken from the

    keyboard

    syntax: scanf("conversion specifier",variable);

    1/17/2013 13

  • 7/30/2019 C PGMMING

    14/110

    main()

    {

    int i,a,b;i=scanf(%d%d,&a,&b);

    printf(%d",i);

    }

    1/17/2013 14

  • 7/30/2019 C PGMMING

    15/110

    output

    2

    1/17/2013 15

  • 7/30/2019 C PGMMING

    16/110

    D

    1/17/2013 16

  • 7/30/2019 C PGMMING

    17/110

    1/17/2013 17

  • 7/30/2019 C PGMMING

    18/110

    Operators in C

    1/17/2013 18

  • 7/30/2019 C PGMMING

    19/110

    Arithmetic Operators+,-,*,/,%

    Logical operators

    &&,||,! Relational Operators

    >,=,

  • 7/30/2019 C PGMMING

    20/110

    Operator Precedence

    1/17/2013 20

  • 7/30/2019 C PGMMING

    21/110

    1/17/2013 21

  • 7/30/2019 C PGMMING

    22/110

    #define square(x) x*x

    main()

    {

    int i;

    i = 64/square(4);

    printf("%d",i);

    }

    Answer --------------- 4

    1/17/2013 22

  • 7/30/2019 C PGMMING

    23/110

    1. D

    2. A

    1/17/2013 23

  • 7/30/2019 C PGMMING

    24/110

    A

    1/17/2013 24

  • 7/30/2019 C PGMMING

    25/110

    B

    1/17/2013 25

  • 7/30/2019 C PGMMING

    26/110

    D

    6 will be printed

    1/17/2013 26

  • 7/30/2019 C PGMMING

    27/110

    Increment Operator(++)

    Pre increment

    Post increment

    1/17/2013 27

  • 7/30/2019 C PGMMING

    28/110

    Decrement Operator(--)

    Pre decrement

    Post decrement

    1/17/2013 28

  • 7/30/2019 C PGMMING

    29/110

    1/17/2013 29

  • 7/30/2019 C PGMMING

    30/110

    B

    1/17/2013 30

  • 7/30/2019 C PGMMING

    31/110

    #include

    int main()

    {

    int i=2;

    int j=++i + ++i + i++;

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

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

    }

    1/17/2013 31

  • 7/30/2019 C PGMMING

    32/110

    1/17/2013 32

  • 7/30/2019 C PGMMING

    33/110

    C

    1/17/2013 33

  • 7/30/2019 C PGMMING

    34/110

    Bitwise Operators

    >> --------------- RIGHT SHIFT

  • 7/30/2019 C PGMMING

    35/110

    x = y / 4; same as x = y >> 2;

    x = y / 32; same as x = y >> 5;

    x = y * 8; x = y

  • 7/30/2019 C PGMMING

    36/110

    1/17/2013 36

    How to swap two numbers using

    http://www.gamedev.net/index.php?app=core&module=attach&section=attach&attach_rel_module=ccs&attach_id=3637
  • 7/30/2019 C PGMMING

    37/110

    How to swap two numbers using

    bitwise operators?

    Program:#include

    main() {

    int i = 65;

    int k = 120;

    printf("\n value of i=%d k=%d before swapping", i, k);

    i = i ^ k;

    k = i ^ k;

    i = i ^ k;

    printf("\n value of i=%d k=%d after swapping", i, k);}

    1/17/2013 37

  • 7/30/2019 C PGMMING

    38/110

    Explanation:

    i = 65; binary equivalent of 65 is 0100 0001

    k = 120; binary equivalent of 120 is 0111 1000

    i = i^k;

    i...0100 0001

    k...0111 1000

    ---------

    val of i = 0011 1001

    ---------

    k = i^k

    i...0011 1001k...0111 1000

    ---------

    val of k = 0100 0001 binary equivalent of this is 65

    ---------(that is the initial value of i)

    i = i^k

    i...0011 1001

    k...0100 0001

    ---------

    val of i = 0111 1000 binary equivalent of this is 120

    ---------(that is the initial value of k)

    1/17/2013 38

    Write a program to swap two

  • 7/30/2019 C PGMMING

    39/110

    Write a program to swap twonumbers without using a temporary

    variable. Program:

    #include

    main() {

    int a, b;

    printf("Enter values of a and b: \n"); scanf("%d %d", &a, &b);

    printf("Before swapping a=%d, b=%d", a,b n);

    /*Swapping logic */

    a = a + b;

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

    printf("After swapping a=%d b=%d", a, b);

    }

    1/17/2013 39

  • 7/30/2019 C PGMMING

    40/110

    Int k= printf("Hello World"))

    Printf(%d,k);

    1/17/2013 40

  • 7/30/2019 C PGMMING

    41/110

    Answer : 11

    Length of the hello world is 11.

    1/17/2013 41

  • 7/30/2019 C PGMMING

    42/110

    Eg1: 14

  • 7/30/2019 C PGMMING

    43/110

    Program: Program to illustrate left shift and rightshift operations.

    #include

    int main(void)

    {

    int x=10,y=10;

    printf("left shift of 10 is %d \n",x1); return 0;

    }

    1/17/2013 43

  • 7/30/2019 C PGMMING

    44/110

    Output:

    left shift of 10 is 20

    right shift of 10 is 5

    Explanation:

    Left shift (by 1 position) multiplies a number

    by two. Right shift divides a number by 2.

    1/17/2013 44

  • 7/30/2019 C PGMMING

    45/110

    A

    1/17/2013 45

  • 7/30/2019 C PGMMING

    46/110

    Control Structures

    1/17/2013 46

  • 7/30/2019 C PGMMING

    47/110

    No Error

    Case statements

    accepts expressions

    1/17/2013 47

  • 7/30/2019 C PGMMING

    48/110

    D

    1/17/2013 48

  • 7/30/2019 C PGMMING

    49/110

    D

    1/17/2013 49

  • 7/30/2019 C PGMMING

    50/110

    A

    1/17/2013 50

  • 7/30/2019 C PGMMING

    51/110

    NO ERROR

    1/17/2013 51

  • 7/30/2019 C PGMMING

    52/110

    D

    1/17/2013 52

  • 7/30/2019 C PGMMING

    53/110

    Arrays

    Variables

    Arrays

    1/17/2013 53

  • 7/30/2019 C PGMMING

    54/110

    C

    1/17/2013 54

  • 7/30/2019 C PGMMING

    55/110

    D

    Val[0][3]

    1/17/2013 55

  • 7/30/2019 C PGMMING

    56/110

    A

    C

    B

    1/17/2013 56

  • 7/30/2019 C PGMMING

    57/110

    12345678901234567890123456789012345678901234567890

    --------------------------------------------------hello ####### %10s

    hello ####### %-10s

    00064 ####### %05d

    64 ####### %5d

    64 ####### %-5d

    100 ####### %5o

    64.00 ####### %5.2f

    1/17/2013 57

  • 7/30/2019 C PGMMING

    58/110

    A

    1/17/2013 58

  • 7/30/2019 C PGMMING

    59/110

    c

    1/17/2013 59

  • 7/30/2019 C PGMMING

    60/110

    B

    1/17/2013 60

  • 7/30/2019 C PGMMING

    61/110

    Functions

    1/17/2013 61

  • 7/30/2019 C PGMMING

    62/110

    1/17/2013 62

  • 7/30/2019 C PGMMING

    63/110

    1/17/2013 63

  • 7/30/2019 C PGMMING

    64/110

    C

    1/17/2013 64

  • 7/30/2019 C PGMMING

    65/110

    1/17/2013 65

  • 7/30/2019 C PGMMING

    66/110

    B

    1/17/2013 66

  • 7/30/2019 C PGMMING

    67/110

    C

    1/17/2013 67

  • 7/30/2019 C PGMMING

    68/110

    USER DEFINED FUNCTION

    1/17/2013 68

  • 7/30/2019 C PGMMING

    69/110

    1/17/2013 69

  • 7/30/2019 C PGMMING

    70/110

    Pointers

    1/17/2013 70

  • 7/30/2019 C PGMMING

    71/110

    C

    B

    1/17/2013 71

  • 7/30/2019 C PGMMING

    72/110

    B

    1/17/2013 72

  • 7/30/2019 C PGMMING

    73/110

    1200 10

    1/17/2013 73

  • 7/30/2019 C PGMMING

    74/110

    1

    1/17/2013 74

  • 7/30/2019 C PGMMING

    75/110

    B

    1/17/2013 75

  • 7/30/2019 C PGMMING

    76/110

    NO

    1/17/2013 76

  • 7/30/2019 C PGMMING

    77/110

    1/17/2013 77

  • 7/30/2019 C PGMMING

    78/110

    1/17/2013 78

    NO. SINCE ARITHMATIC OPERATION IS NOT ALLOWED ON VOID POINTER

  • 7/30/2019 C PGMMING

    79/110

    1/17/2013 79

    D

  • 7/30/2019 C PGMMING

    80/110

    1/17/2013 80

    A

  • 7/30/2019 C PGMMING

    81/110

    D

    1/17/2013 81

  • 7/30/2019 C PGMMING

    82/110

    A

    1/17/2013 82

  • 7/30/2019 C PGMMING

    83/110

    C

    1/17/2013 83

    A

  • 7/30/2019 C PGMMING

    84/110

    1/17/2013 84

    C

  • 7/30/2019 C PGMMING

    85/110

    1/17/2013 85

    A,B

  • 7/30/2019 C PGMMING

    86/110

    1/17/2013 86

    C

  • 7/30/2019 C PGMMING

    87/110

    1/17/2013 87

    A

  • 7/30/2019 C PGMMING

    88/110

    1/17/2013 88

  • 7/30/2019 C PGMMING

    89/110

    1/17/2013 89

    A,C

  • 7/30/2019 C PGMMING

    90/110

    1/17/2013 90

    A,B,C

  • 7/30/2019 C PGMMING

    91/110

    1/17/2013 91

    D

  • 7/30/2019 C PGMMING

    92/110

    1/17/2013 92

    A,C,D

  • 7/30/2019 C PGMMING

    93/110

    1/17/2013 93

    C

  • 7/30/2019 C PGMMING

    94/110

    1/17/2013 94

    C

  • 7/30/2019 C PGMMING

    95/110

    FILES

    1/17/2013 95

  • 7/30/2019 C PGMMING

    96/110

    FILE IS A DATA TYPE

    1/17/2013 96

  • 7/30/2019 C PGMMING

    97/110

    FILE *fopen(const char *filename, const char*mode);

    Used to open a file and returns the starting

    address Returns NULL if file cant be opened

    int fclose(FILE *stream); Used to close the file

    1/17/2013 97

  • 7/30/2019 C PGMMING

    98/110

    int fprintf(FILE *stream, const char *format,...);

    This acts exactly like printfexcept that the data is

    written to stream, rather than stdout. It normallyreturns the number of characters written, or a

    negative value for an error. Int

    Used to store the data.

    fprintf(fp,formatspecifiers,variables);

    1/17/2013 98

    http://personal.ee.surrey.ac.uk/Personal/R.Bowden/C/printf.htmlhttp://personal.ee.surrey.ac.uk/Personal/R.Bowden/C/printf.html
  • 7/30/2019 C PGMMING

    99/110

    fscanf(FILE *stream, const char *format, ...);

    This acts exactly like scanfexcept that the data

    is read from stream, rather than stdin. It

    normally returns the number of input itemsconverted and assigned, or EOF for an error.

    Used to get the data.

    fscantf(fp,formatspecifiers,variables with &symbol);

    1/17/2013 99

    To read the character by character

    http://personal.ee.surrey.ac.uk/Personal/R.Bowden/C/scanf.htmlhttp://personal.ee.surrey.ac.uk/Personal/R.Bowden/C/scanf.html
  • 7/30/2019 C PGMMING

    100/110

    To read the character by character

    Char Getc(filepointer)

    Char fgetc(filepointer);

    Used to read the character from the file

    Returns EOF at the end.

    Both are same, but fgetc returns EOF when any

    error or EOF is reached.

    Putc(filepointer,char); Used to store the character into file.

    1/17/2013 100

  • 7/30/2019 C PGMMING

    101/110

    Fgets()

    Used to read the string by string from file

    Fgets(variable, length,filepointer)

    It also returns EOF when reached to end

    1/17/2013 101

  • 7/30/2019 C PGMMING

    102/110

    How can we read/write structures from/todata files?

    By using fread() and fwrite() functions one

    can read or write data to data files.

    Fread(&emp,sizeof(emp),1,filepointer)

    Fwrite(&emp,sizeof(emp),1,filepointer)

    1/17/2013 102

  • 7/30/2019 C PGMMING

    103/110

    Fseek()

    Used to position the file pointer

    Fseek(fp,negitive/positivenumber,location);

    Location may be 0 if it is beginning of the file(SEEK_SET)

    1 if it is from the current position ( SEEK_CUR)

    2 if it is from the current position ( SEEK_END)

    Negitive number indicates backward direction and

    positive indicates forward direction

    1/17/2013 103

  • 7/30/2019 C PGMMING

    104/110

    1/17/2013 104

    B,C

  • 7/30/2019 C PGMMING

    105/110

    1/17/2013 105

    B,C,D

  • 7/30/2019 C PGMMING

    106/110

    1/17/2013 106

    B,D

  • 7/30/2019 C PGMMING

    107/110

    1/17/2013 107

    A,B

  • 7/30/2019 C PGMMING

    108/110

    1/17/2013 108

    A,B,C

  • 7/30/2019 C PGMMING

    109/110

    1/17/2013 109

    A

  • 7/30/2019 C PGMMING

    110/110

    C


Recommended