03 helloworld in ‘c’

Upload: niharranjanroy

Post on 03-Jun-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 03 HelloWorld in C

    1/15

    Introduction to programming in C

    Nihar Ranjan Roy

  • 8/11/2019 03 HelloWorld in C

    2/15

    History of C Programming language

    The C programming language was designed by Dennis Ritchie at BellLaboratories in the early 1970s

    In 1983 the American National Standards Institute (ANSI) began thestandardization process

    In 1989 the International Standards Organisation (ISO) continued thestandardisation process

    In 1990 a standard was finalised, known simply as Standard C Everything before this is known as K&R C Traditionally used for

    systems programming.

    Main source of documentation of K& R C The C Programming Language , by Brian Kernighan and Dennis Ritchie,

    2nd Edition, Prentice Hall Referred to as K&R

    Nihar Ranjan Roy 2

  • 8/11/2019 03 HelloWorld in C

    3/15

    Characteristics of C Language

    It recognizes the difference between lowercase and uppercase letters. It can be written with free format

    Can write as many instructions as in a single line Its a structured programming language

    During execution the control falls from the line at the top to the

    Program consists of smaller units called functions.

    Nihar Ranjan Roy 3

  • 8/11/2019 03 HelloWorld in C

    4/15

    Building a C program#includeint main(void){...

    }

    Text Editor

    Compiler 010010011110101

    Programmer

    Source Code

    Object Code

    ___________ ________________ ________________ ________________

    _

    Linker

    Loader

    1110100001001.111010000111101 Executable

    Code

    Results

    Nihar Ranjan Roy 4

  • 8/11/2019 03 HelloWorld in C

    5/15

    Writing and editing programs Use any text editor (Ascii) available on your computer.

    Example: windows (Edit,TC IDE,Notepad), Linux( Vi,Vim,emacs etc.) Compiling (windows)

    With IDE ( Turbo C) Press Alt+F9

    Command prompt ssue t e o ow ng comman tcc ename.c

    Linking Running the program

    Windows With IDE

    Press CTRL+F9 Command Prompt

    Issue the following command filename (hit enter) Linux

    Issue the following command ./a.out or ./outputfilename ( hit enter)

    Nihar Ranjan Roy 5

  • 8/11/2019 03 HelloWorld in C

    6/15

    Structure of a C program

    /* Preprocessor directives */

    /* Global declarations */ #include int main(void)

    int main(void){/*Local declarationsStatements*/}

    /* other functions as required */

    printf("Hello");return 0;

    }

    Nihar Ranjan Roy 6

  • 8/11/2019 03 HelloWorld in C

    7/15

    My First C Program

    #include /* comment */

    int main(void)

    tells compiler about stst anddard iinput and ooutput functions (i.e. printf + others)

    main function

    printf("Hello");return 0;

    }begin

    end

    flag success

    to operatingsystem

    Nihar Ranjan Roy 7

  • 8/11/2019 03 HelloWorld in C

    8/15

    Command Prompt(Windows) : Compiling & Running

    Nihar Ranjan Roy 8

  • 8/11/2019 03 HelloWorld in C

    9/15

    Turbo C IDE

    Nihar Ranjan Roy 9

  • 8/11/2019 03 HelloWorld in C

    10/15

    The Format of C

    Statements are terminated withsemicolons

    Indentation is ignored by the compiler. C is case sensitive - all keywords and

    Standard Library functions are lowercase. Strin s are laced in double uotes

    #include

    /* comment */

    int main(void){

    rintf "Hello" Newlines are handled via \n

    Nihar Ranjan Roy 10

    return 0;}

  • 8/11/2019 03 HelloWorld in C

    11/15

    Problem

    Write a program in C which asks the user what is your name?. Acceptsusers name and greets him as Hello,xxxx where xxxx is the name ofthe user.

    What is your name?

    Nihar Ranjan Roy 11

    N arHello, Nihar

  • 8/11/2019 03 HelloWorld in C

    12/15

    2nd

    Program

    char name [30] ;declares a variable named name, which

    can hold a string of up to 29characters (letter, digit, punctuation,etc.).

    #includevoid main(){

    printf ("What's your name? ");

    Nihar Ranjan Roy 12

    read your name into the variable name.

    scanf("%s",name);printf("Hello, %s\n",name);}

  • 8/11/2019 03 HelloWorld in C

    13/15

    Problem

    Write a program which accepts two integer numbers and displays itssum.

    Enter two numbers:6 7The sum is 13

    Nihar Ranjan Roy 13

  • 8/11/2019 03 HelloWorld in C

    14/15

    3rd

    Program

    #includevoid main (){int a,b,sum;

    We have made five changes to theoriginal program. We have

    1. replaced the line defining name with onedefining other variables (a, b, and sum, alintegers)

    Nihar Ranjan Roy 14

    pr n n er wo num ers: ;scanf("%d %d",&a,&b);sum = a + b;printf("The sum is %d \n",sum);}

    . c ange t e message n t e pr ntstatement

    3. changed the format string and variablelist in the scanf statement

    4. added the assignment statement sum = ab;

    5. changed the format string and argumentlist in the final printf statement

  • 8/11/2019 03 HelloWorld in C

    15/15

    Conclusion & Queries

    Nihar Ranjan Roy 15