Slide 03 - Introduction to C

Embed Size (px)

Citation preview

  • 8/10/2019 Slide 03 - Introduction to C

    1/18

    Computer Science Department FTSM

    Introduction to C

    Knowledge:

    Understand the complete structure of C programs in Linuxenvironment

    Skill:Edit, compile and execute C programs

  • 8/10/2019 Slide 03 - Introduction to C

    2/18

    TK1913-C Programming 2

    Introduction /* Task: This program calculates BMI */#include #define WEIGHT 60.0#define HEIGHT 1.53

    void main(void) {float bmi;

    bmi = WEIGHT / (HEIGHT * HEIGHT);

    if (bmi > 25.0)printf( \nYour BMI is %.2f. Need to lose weight! \ n, bmi);

    }

    Resembles AlgebraicExpression

    Augmented by certainEnglish keywords

    eg. if , else, while

    C is a High-level LanguageCan also be used at lower-level

    This flexibility allows it to beused for SYSTEMPROGRAMMING (eg.Operating systems likeUNIX and WINDOWS)

    APPLICATIONPROGRAMMING

    (Billing System,Sistem Semut ? )

    C has small instruction set,though the actualimplementations include

    extensive li brary functions

    myfunction.h

    C encourages usersto write additional

    library

    functions of their own

    Finally, C programs are highlyportable.

    They can be executed on differentplatforms without having to be

    recompiled (or with little modification)

  • 8/10/2019 Slide 03 - Introduction to C

    3/18

  • 8/10/2019 Slide 03 - Introduction to C

    4/18

    TK1913-C Programming 4

    Editing C Program in Linux Use vi editor

    $vi program_name.c

    Use pico editor $pico program_name.c

    Use emacs editor

    $emacs program_name.c

    Example: $pico myprogram.c

    This is only an example ofprogram name. You can give

    any name that you like.

  • 8/10/2019 Slide 03 - Introduction to C

    5/18

    TK1913-C Programming 5

    Compiling C Program in Linux To compile C GNU: $gcc program_name.c

    An error message will be displayed if there isany syntax error Needs to be rectified until

    there is no more errorsIf succeeded, the output will be generated intothe execution file, namely a.out

    To display the output on the screen: $a.out

    Example: $gcc myprogram.c$a.out

  • 8/10/2019 Slide 03 - Introduction to C

    6/18

    TK1913-C Programming 6

    Another method to compile C GNU:$gcc o file_name program_name.c

    An error message will be displayed if there is anysyntax error Needs to be rectified until there isno more errors

    If succeeded, the output will be generated intothe execution file, namely file_name

    To display the output on the screen: $file_name

    Compiling C Program in Linux

    Example: $gcc output myprogram.c$output

    This is only an example ofexecution file name. You can give

    any name that you like.

  • 8/10/2019 Slide 03 - Introduction to C

    7/18

    TK1913-C Programming 7

    Don t WorryBe Happy Got confused?? Dont

    worry, youll acquire thisskill during the labsession. Ensure you know

    your lab group andschedulegood luck!

  • 8/10/2019 Slide 03 - Introduction to C

    8/18

    TK1913-C Programming 8

    History of C LanguageC was originated from 2 programming languages, namelyBCPL and B BCPL was developed by Martin Richards in year 1967. It

    was intended as a language to develop operating systemsand compilersB was developed by Ken Thompson in year 1970s. It wasused to develop UNIX operating system at Bell

    LaboratoriesC was developed by Dennis Ritchie in year 1972. Itreplaced B as the language to develop UNIX operatingsystem at Bell Laboratories

  • 8/10/2019 Slide 03 - Introduction to C

    9/18

    TK1913-C Programming 9

    C Program StructurePreprocessorInstruction

    void main (void){

    }

    Statement

    Global Declaration

    Local Declaration

  • 8/10/2019 Slide 03 - Introduction to C

    10/18

    TK1913-C Programming 10

    Example of C Program

    #include

    void main(void) {

    printf(Welcome to UKM!);

    }

    program

    Main function

    statementPreprocessor instruction

  • 8/10/2019 Slide 03 - Introduction to C

    11/18

    TK1913-C Programming 11

    Preprocess Instruction2 types of preprocess instruction that arenormally used:

    #include

    #define

    #include is used to include certain files into the program. Those files need to be included

    because they contain the necessary informationfor compilation (e.g. stdio.h file containsinformation about printf function)

  • 8/10/2019 Slide 03 - Introduction to C

    12/18

    TK1913-C Programming 12

    Preprocess Instruction#define is used to declare macro constants

    Example:

    #include #define PI 3.141593

    void main(void) {

    float luas;luas = PI * 7 * 7;printf(Luas %.2f:, luas);

    }

    Macro constant

    Before preprocessExample:#include #define PI 3.141593

    void main(void) {float luas;luas = 3.141593 * 7 * 7;printf(Luas %.2f:, luas);

    }

    After preprocess

  • 8/10/2019 Slide 03 - Introduction to C

    13/18

    TK1913-C Programming 13

    Main Function

    Every C program must have a mainfunction, called main()

    The execution of C program starts frommain() function

  • 8/10/2019 Slide 03 - Introduction to C

    14/18

    TK1913-C Programming 14

    StatementSentence -like action steps that are written in the

    body of the functionIn C, all statements must be ended with ; symbol

    Example: A statement to display a string ofcharacters on the screen by using printf() function

    printf(Welcome to UKM);

    Output:

    Welcome to UKM

  • 8/10/2019 Slide 03 - Introduction to C

    15/18

    TK1913-C Programming 15

    CommentYou could include comments in your program to easeunderstanding

    Comments will be ignored during compilation

    A block of comment is labeled with /* (start) and */ (end) compiler will ignore any text written after /* symbol till */ symbol is found

    Nested comments (comment within comment) are notallowed, for example:

    /* my comment /* subcomment */ my comment continues */

  • 8/10/2019 Slide 03 - Introduction to C

    16/18

  • 8/10/2019 Slide 03 - Introduction to C

    17/18

    TK1913-C Programming 17

    Example of C Program

    Welcome to

    Fakulti Teknologi dan Sains Maklumat

    What will the output be?

  • 8/10/2019 Slide 03 - Introduction to C

    18/18