02 writing first c program

Upload: poh-junyee

Post on 04-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 02 Writing First C Program

    1/76

    Topic 2

    Writing First C Program

  • 8/14/2019 02 Writing First C Program

    2/76

    Smallest C Program

    A C program consists of one or more blocks of codecalled functions .

    Minimum number of functions in a program is one.

    The name of one of the functions must be main.

    The main function is where program executionbegins.

  • 8/14/2019 02 Writing First C Program

    3/76

    Smallest C Program

    When a C program runs, it gives back a result toindicate whether program execution is successful.0 (zero) shows successful execution.Non-zero shows there was some problem.

  • 8/14/2019 02 Writing First C Program

    4/76

    Data Type int and Constant

    The numbers (0,1,2, etc.) are integers i.e. wholenumbers which do not have decimal point.

    Integer values are one category of constants i.e.

    values that appear in a program.

    In C, this type of numbers belong to a group calledint i.e. this type of data or this data type is called inttype.

  • 8/14/2019 02 Writing First C Program

    5/76

    The main Function

    Smallest C program has these lines:

    int main(void){

    return 0;}

    Function header marksthe beginning of a function.

    Indicates that the functionname is main and thefunction gives back aninteger type value (int) andthe function does not needany information (void).

  • 8/14/2019 02 Writing First C Program

    6/76

    The main Function

    Smallest C program has these lines:

    int main(void){

    return 0;}

    Opening curly brace -indicates the start of the

    body of the function.

    Closing curly brace -indicates the end of thebody of the function.

  • 8/14/2019 02 Writing First C Program

    7/76

    The main Function

    Smallest C program has these lines:

    int main(void){

    return 0;}

    The return statementterminates the execution

    of the function and returnscontrol to the operatingsystem. It also givesback the functions result.

    Zero indicates theprogram executed withouterror.

  • 8/14/2019 02 Writing First C Program

    8/76

    Statements

    A statement causes an action to be performed by theprogram.

    It translates directly into one or more machine

    language instructions.

  • 8/14/2019 02 Writing First C Program

    9/76

    Statements

    int main(void){

    return 0;}

    Notice the statement endswith a semicolon (;).

  • 8/14/2019 02 Writing First C Program

    10/76

    Statements

    This program can run but it doesnt do anything.

    int main(void){

    return 0;}

  • 8/14/2019 02 Writing First C Program

    11/76

    Input-Process-Output

    The simplest program that does some useful workshould:get some input data,process that data, and

    produce some output result.

    ProcessInput Output

  • 8/14/2019 02 Writing First C Program

    12/76

    Working with Data

    A program usually processes data.

    But how do we store the data?In memory cells

    How does the program refer to these memory cells?By giving names to the memory cells.Example names are x, y, number, n1, n2.

  • 8/14/2019 02 Writing First C Program

    13/76

    Declaration Statement

    Before we can use a memory cell in the program, weneed to tell the compiler the name and the type ofdata it will store.

    We do this using a declaration.Example:

    int x;Declaration that tells thecompiler the name of the

    memory cell is x and the typeof data it will store is integer.

  • 8/14/2019 02 Writing First C Program

    14/76

    Declaration Statement

    We can declare more than one name in onedeclaration.Example:

    int x, y, z;

    Tells compiler theprogram uses 3 memorycells with the names x, yand z to store integervalues.

    Notice the names are

    separated by commas(,).

  • 8/14/2019 02 Writing First C Program

    15/76

    Assignment Statement

    How do we tell the program to store a value in thememory cell?By writing an assignment statement.Example:

    x = 5;Assignment statement thattells the program to store/assignthe value 5 in the memory cellwith the name x.

    = is the assignment operator. It does not mean equal.

  • 8/14/2019 02 Writing First C Program

    16/76

    Assignment Statement

    How do we tell the program to store a value in thememory cell?By writing an assignment statement.Example:

    x = 5; Think of it like this:x 5which you can read as:

    put the value 5 in the memorycell with the name x

    or simply:assign 5 to x

    or x becomes 5.

  • 8/14/2019 02 Writing First C Program

    17/76

  • 8/14/2019 02 Writing First C Program

    18/76

    Assignment Statement

    How do we tell the program to copy a value from onememory cell to another?Use assignment statement.Example:

    x = 5;y = x;

    5x

  • 8/14/2019 02 Writing First C Program

    19/76

    Assignment Statement

    How do we tell the program to copy a value from onememory cell to another?Use assignment statementExample:

    x = 5;y = x;

    5x

    y

    Copy valueof x into y

  • 8/14/2019 02 Writing First C Program

    20/76

    Assignment Statement

    How do we tell the program to copy a value from onememory cell to another?Use assignment statementExample:

    x = 5;y = x;

    5x

    5y

    Copy valueof x into y

  • 8/14/2019 02 Writing First C Program

    21/76

    Complete Program

    int main(void){

    int x;int y;

    x = 5;y = x;

    return 0;}

  • 8/14/2019 02 Writing First C Program

    22/76

    Complete Program

    int main(void){

    int x;int y;

    x = 5;y = x;

    return 0;}

    Function Header

    Function Body

  • 8/14/2019 02 Writing First C Program

    23/76

    Complete Program

    int main(void){

    int x;int y;

    x = 5;y = x;

    return 0;}

    Declarations

    Statements

  • 8/14/2019 02 Writing First C Program

    24/76

    Complete Program

    int main(void){

    int x;int y;

    x = 5;y = x;

    return 0;}

    Assignment statements

    return statement

  • 8/14/2019 02 Writing First C Program

    25/76

    Showing Results

    The above program will run but it does not show anyresults.Everything happens inside the computers mainmemory only.

    How do we make the program display the valuesstored in the memory cells?

    By asking a special function to do it i.e. calling or

    activating a function to display the values, just like youcall your friend to do something for you.The name of the function is printf.

  • 8/14/2019 02 Writing First C Program

    26/76

    Showing Results

    How do we tell function printf what to display?By passing it two pieces of information:the value to displaythe format to display the value

    Example:

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

    The name offunction to call

    Parentheses surrounding theinformation to pass to the function

  • 8/14/2019 02 Writing First C Program

    27/76

    Showing Results

    How do we tell function printf what to display?By passing it two pieces of information:the value to displaythe format to display the value

    Example:

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

    Indicates that we wantto display the value of y.

  • 8/14/2019 02 Writing First C Program

    28/76

    Showing ResultsHow do we tell function printf what to display?

    By passing it two pieces of information.the value to displaythe format to display the value

    Example:

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

    This is a string i.e. a sequence of characters. A string is always surrounded by double quotes ().

  • 8/14/2019 02 Writing First C Program

    29/76

    Showing ResultsHow do we tell function printf what to display?

    By passing it two pieces of information:the value to displaythe format to display the value

    Example:

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

    Here the string is called a format string .It tells the printf function how we want todisplay the value.

  • 8/14/2019 02 Writing First C Program

    30/76

    Showing ResultsHow do we tell function printf what to display?

    By passing it two pieces of information:the value to displaythe format to display the value

    Example:

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

    We are saying we want the output to be Y= followed by thevalue of y. The %d is a placeholder that marks the positionto display an integer type value.

  • 8/14/2019 02 Writing First C Program

    31/76

    Program Now

    int main(void){

    int x;int y;

    x = 5;y = x;printf( "Y=%d" , y);

    return 0;}

  • 8/14/2019 02 Writing First C Program

    32/76

    Showing ResultsThe printf function is one of many functions availablein standard libraries .

    A library is a collection of functions.

    Each library has a standard header file whose nameends with .h which contains information about thefunctions.

    The information for the printf function is in a filecalled stdio.h .

  • 8/14/2019 02 Writing First C Program

    33/76

    Showing ResultsFor the program to use printf function, thisinformation must be inserted into the program.

    To do this , we add this line in our program:

    #include

    This is called a preprocessor directive .

  • 8/14/2019 02 Writing First C Program

    34/76

    Showing Results A preprocessor or precompiler processes the text ofa program before it is compiled.

    The #include directive notifies the

    preprocessor that some names used in the program(such as printf) are defined in the file stdio.h.

    std standardio input/output

    h header file

  • 8/14/2019 02 Writing First C Program

    35/76

    Complete Program

    #include int main(void){

    int x;int y;

    x = 5;y = x;printf( "Y=%d" , y);

    return 0;

    }

    5xMemory Cells

    5y

    Y=5

    Computer Screen

  • 8/14/2019 02 Writing First C Program

    36/76

    Showing ResultsHow do we make the program display the following?

    Y is 5

    5 is the value in y.

    printf( "Y is %d ", y);

    printf( "%d is the value in y. ", y);

  • 8/14/2019 02 Writing First C Program

    37/76

    What is the output of this program?

    #include int main(void){

    int a, b;

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

    return 0;

    }

  • 8/14/2019 02 Writing First C Program

    38/76

    Showing Results

    How can we get the two values displayed on twoseparate lines?

    Use a special character sequence ( \n) which represents anewline escape sequence to display the output on a

    new line.This makes the cursor (position) on the screen movedown to a new line.

    Computer Screen

    a=475 b=475

  • 8/14/2019 02 Writing First C Program

    39/76

    Showing ResultsExample:

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

    a=475b=475-

    The cursor is nowon the third line

    Computer Screen

  • 8/14/2019 02 Writing First C Program

    40/76

    Showing ResultsWhat is the output of the following?

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

  • 8/14/2019 02 Writing First C Program

    41/76

    Showing ResultsWhat is the output of the following? Same as above

    Can we do this?

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

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

  • 8/14/2019 02 Writing First C Program

    42/76

    Showing ResultsWhat is the output of the following? Same as above

    Can we do this? Yes

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

    Placeholderfor a

    Placeholderfor b

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

  • 8/14/2019 02 Writing First C Program

    43/76

  • 8/14/2019 02 Writing First C Program

    44/76

    VariablesPrograms store data in memory cells.

    A memory cell has a name, a data type, a value,and an address.

    In programming, these memory cells are calledvariables because the value in the memory cell canchange.

  • 8/14/2019 02 Writing First C Program

    45/76

    Variable Declaration and Assignment

    int n;Variable declaration tellscompiler the name and datatype of the variable (i.e.memory cell).

    n = 72; Assigns value 72 to variable n(i.e. memory cell with name n).

  • 8/14/2019 02 Writing First C Program

    46/76

    Variable Declaration and Assignment

    int n;

    n = 72;

    Only variables are allowed on theleft-hand side of an assignmentstatement.

  • 8/14/2019 02 Writing First C Program

    47/76

    Getting Input DataHow do we make a program input the data from theprogram user in order to do computation?

    By asking a special function to do it.The name of the function is scanf.

    After we get the data, where do we store it?In a variable

  • 8/14/2019 02 Writing First C Program

    48/76

    Getting Input DataHow do we tell function scanf where to store thedata?

    By passing it two pieces of information:the address of variable we want to store the data

    the type of the input dataExample:

    scanf( "%d", &num);

    Indicates we want to storethe data in variable num.

  • 8/14/2019 02 Writing First C Program

    49/76

    Getting Input DataHow do we tell function scanf where to store thedata?

    By passing it two pieces of information:the address of variable we want to store the data

    the type of the input dataExample:

    scanf( "%d", &num);

    & (ampersand character) is the address-of operator that gives the address of the variable

  • 8/14/2019 02 Writing First C Program

    50/76

    Getting Input DataHow do we tell function scanf where to store thedata?

    By passing it two pieces of information:the address of variable we want to store the data

    the type of the input dataExample:

    scanf( "%d", &num);

    The format string indicates thatthe data type is integer.

  • 8/14/2019 02 Writing First C Program

    51/76

    Getting Input DataJust like for the function printf, the stdio.h fileprovides additional information about the functionscanf .

    We need to add the preprocessor directive in ourprogram:#include

  • 8/14/2019 02 Writing First C Program

    52/76

    Complete Program

    #include int main(void){

    int num;

    scanf( "%d", &num);

    return 0;}

  • 8/14/2019 02 Writing First C Program

    53/76

    Getting Input Data

    Better to let the user know that the program isexpecting some data.How do we do this?

    We display a prompt using printf function

    _Cursor appears andprogram waits for theuser to enter a number.

    Computer Screen

  • 8/14/2019 02 Writing First C Program

    54/76

    Complete Program

    #include int main(void){

    int num;

    printf( "Enter a number: ");scanf( "%d", &num);

    return 0;}

    Only formatstring inparentheses

    Displaysa prompt.

  • 8/14/2019 02 Writing First C Program

    55/76

    Getting Input Data

    Enter a number: _

    Prompt displayedby printf function

    Computer Screen Cursor appears andprogram waits for theuser to enter a number

  • 8/14/2019 02 Writing First C Program

    56/76

    Getting Input Data

    Enter a number: 91Computer Screen User types a number

    and presses Enterkey.

    The input data isstored as an integer inthe variable num.

    91num

  • 8/14/2019 02 Writing First C Program

    57/76

    Getting Input Data

    Enter a number: 91Computer Screen User types a number

    and presses Enterkey.

    The input data isstored as an integer inthe variable num.

    91num

    Note: the underline is usedto show that this is data

    entered by the user.

  • 8/14/2019 02 Writing First C Program

    58/76

    What does this program do?

    #include int main(void){

    int num, numPower2;

    printf( "Enter n: ");scanf( "%d", &num);numPower2 = num * num;printf( "n x n = %d\n ", numPower2);

    return 0;}

  • 8/14/2019 02 Writing First C Program

    59/76

    What does this program do?

    Enter n: 5n x n = 25

    Computer Screen5 num

    25 numPower2

  • 8/14/2019 02 Writing First C Program

    60/76

    What does this program do?#include int main(void){

    int a, b, sum;printf( "Enter a: ");scanf( "%d", &a);printf( "Enter b: ");scanf( "%d", &b);

    sum = a + b;printf( "a + b = %d\n ", sum);return 0;

    }

  • 8/14/2019 02 Writing First C Program

    61/76

    What does this program do?

    Enter a: 531Enter b: 24

    a + b = 555

    Computer Screen 531 a

    555 sum

    24 b

  • 8/14/2019 02 Writing First C Program

    62/76

    What does this program do?

    #include int main(void){

    int a, b, sum;

    printf( "Enter a and b: ");scanf( "%d%d", &a, &b);

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

    return 0;}

  • 8/14/2019 02 Writing First C Program

    63/76

    What does this program do?

    Enter a and b: 531 24

    531a ?sum24b

  • 8/14/2019 02 Writing First C Program

    64/76

    What does this program do?

    Enter a and b: 531 24531 + 24 = 555

    531a 555sum24b

  • 8/14/2019 02 Writing First C Program

    65/76

    What does this program do?

    #include int main(void){

    int a, b;

    a = 3;b = 5;printf( "a= %d, b=%d\n ", a, b);a = b;

    b = a;printf( "a= %d, b=%d\n ", a, b);return 0;

    }

  • 8/14/2019 02 Writing First C Program

    66/76

    What does this program do?

    3a ?b

    a = b;b = a;

    a = 3;b = 5;

  • 8/14/2019 02 Writing First C Program

    67/76

    What does this program do?

    3a 5b

    a = b;b = a;

    a = 3;b = 5;

  • 8/14/2019 02 Writing First C Program

    68/76

    What does this program do?

    5a 5b

    a = b;b = a;

    a = 3;b = 5;

  • 8/14/2019 02 Writing First C Program

    69/76

    What does this program do?

    5a 5b

    a = b;b = a;

    a = 3;b = 5;

  • 8/14/2019 02 Writing First C Program

    70/76

    What does this program do?

    #include int main(void){

    int a, b, temp;

    a = 3;b = 5;printf( "a= %d, b=%d\n ", a, b);

  • 8/14/2019 02 Writing First C Program

    71/76

    What does this program do?

    temp = a;a = b;b = temp;

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

    return 0;}

  • 8/14/2019 02 Writing First C Program

    72/76

    What does this program do?

    3a ?b ?tempa = 3;b = 5;

    temp = a;a = b;b = temp;

  • 8/14/2019 02 Writing First C Program

    73/76

    What does this program do?

    3a 5b ?tempa = 3;b = 5;

    temp = a;a = b;b = temp;

  • 8/14/2019 02 Writing First C Program

    74/76

    What does this program do?

    3a 5b 3tempa = 3;b = 5;

    temp = a;a = b;b = temp;

  • 8/14/2019 02 Writing First C Program

    75/76

    What does this program do?

    5a 5b 3tempa = 3;b = 5;

    temp = a;a = b;b = temp;

  • 8/14/2019 02 Writing First C Program

    76/76

    What does this program do?

    5a 3b 3tempa = 3;b = 5;

    temp = a;a = b;b = temp;