an introductio to c programming

Upload: fazal-mcpherson

Post on 04-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 An Introductio to C Programming

    1/9

  • 8/13/2019 An Introductio to C Programming

    2/9

    Lecturer: Aurel Liddell CSI 2101 September, 2013

    2

    mainis a special function. Every C program begins executing at the start of main. The main

    function will usually call other functions to help perform its job, some that you will define and

    others from libraries that are provided.

    On the first line of the hello.c program, #include,is used to tell the compiler to

    include information from the standard input/output library.

    In hello.c, the main function has the statement:

    printf(hello, world\n);

    printfis a library function that prints output. In this case we use it to display to screen the words hello,

    world.

    The last statement in our program is:

    return 0;

    Simply put, this statement indicates that the program should return control to its calling environment. In

    this case it would be the operating system. The return value of zero implies normal termination of a

    program.

    LibraryA collection of predefined declarations of functions and variables. Librariesare stored in headerfiles and saved with a .h extension.

  • 8/13/2019 An Introductio to C Programming

    3/9

    Lecturer: Aurel Liddell CSI 2101 September, 2013

    3

    VARIABLES

    1. Variables

    There are two properties of a variable:

    its data type

    its name

    1.1 Data types

    There are only a few basic data types in C. E.g:

    char A single byte, capable of holding one character

    int An integer value. It stores data in 4 bytesfloat A floating point number e.g 2.4

    1.2Variable Names

    Variables names are made up of letters and digits, the first character must be a

    letter.

    An underscore ( _ ) is considered a letter and can be used in naming a variable.

    There are some keywords that cannot be used as variable names because they are

    reserved for special use by the C language. E.g:

    Table 1. Keywords

    break else new extern

    case finally return void

    catch for switch while

    continue function this with

    default

    if

    throw

    delete in try

    do instanceof typeof

    interCapping/camel Caseis allowed e.g. customerName

    VariableA symbolic name assigned to a particular memory location that stores a particular value.

  • 8/13/2019 An Introductio to C Programming

    4/9

    Lecturer: Aurel Liddell CSI 2101 September, 2013

    4

    Variable names are case sensitive. So a variable customerName is different to

    CustomerName.

    Best practice is to choose variable names that are related to the purpose of the

    variable.

    1.3Declarations

    Before a variable can be used, it must be declared. A declaration specifies the properties of a

    variable, e.g.

    int age;

    char letter;

    float height;

    N.B.

    A semicolon demarks the end of a programming statement.

    Variables of the same type can be declared in one comma - delimited statement:

    int age, idNum, days;

    Initializing Variables:

    A variable may be initialized (i.e. given a starting value) in its declaration. If the name is

    followed by an equal sign and an expression, the expression serves as an initializer, e.g.

    int age = 16;

    char letter = A;/* NB: surround characters with single quotes*/

    float height = 160.5;

  • 8/13/2019 An Introductio to C Programming

    5/9

    Lecturer: Aurel Liddell CSI 2101 September, 2013

    5

    1.4Operators

    1.4.1. The Assignment Operator (=)

    This is used to assign a value to a variable e.g.:

    nValue = 35;

    In this statement, the value 35 is assigned to a variable called nValue or simply put, the value 35

    is being stored in the variable called nValue. NB. It does not mean the variable nValueequals

    35.

    1.4.2. The Arithmetic Operators:

    + For addition- For subtraction

    * For multiplication

    / For division% (Modulus) To return the remainder after

    division

    These are used to perform basic arithmetic functions. E.g. the expression 2+3 adds two

    numbers, but to use the sum, it needs to be assigned to a variable, e.g.:

    total = 2+3;

    Variables can also be used with arithmetic operators to calculate a result:

    int num1,num2,result;

    num1 = 5;

    num2 = 3;

    result = num1 num2; // result holds the value 2

    Operatorssymbols that have a meaning when working with data.

  • 8/13/2019 An Introductio to C Programming

    6/9

    Lecturer: Aurel Liddell CSI 2101 September, 2013

    6

    1.4.3. Relational and Logical Operators:

    Relational Operators> Greater than

    >= Greater than or equal to< Less than

  • 8/13/2019 An Introductio to C Programming

    7/9

    Lecturer: Aurel Liddell CSI 2101 September, 2013

    7

    The ++ and -- operators can be used two ways:

    prefix: ++y or --x (the variable is incre/decre -mented before its value is used)

    postfix: y++ or x--(the variable is incre/decre -mented after its value is used)

    Example:

    int x = 0; int n = 5;

    x = n++; /* assigns 5 to x then increments n by 1 */

    x = ++n; /* increments n by 1 then assigns 5 to x */

    NB: in both cases n becomes 6 after execution.

    Some special operators in C

    Operator Operation Example

    ++ Increment x++ or ++x

    -- Decrement x-- or --x

    += Add and assign x += y

    -= Subtract and assign x -= y

    *= Multiply and assign x *= y

    /= Divide and assign x /= y

    %= Modulus and assign x %= y

  • 8/13/2019 An Introductio to C Programming

    8/9

    Lecturer: Aurel Liddell CSI 2101 September, 2013

    8

    Input and Output

    1. Formatted Output using printf

    The predefined function printf is used to display formatted output. It takes the form:

    printf(format,argument);

    Some of the most commonly used format placeholders:

    Format Description

    %d Print an integer as a signeddecimal number.

    %f Print afloating-point number in normal (fixed-point)notation.

    %s Print acharacter string.

    %c Print a character (char).

    E.g.

    to print a text string:printf(This is my first string.\n);

    to print the value in a variable:printf(Your age is%d\n , age);

    Note that we use \n to include a newline character after the output is printed.

    2. Formatted Input using scanf

    The predefined function scanf is used to read formatted input. It takes the form:scanf(format, variable address);

    Some of the most commonly used format placeholders:

    Format Description

    %d Scan an integer as a signeddecimal number.

    %f Scan afloating-point number in normal (fixed-point)notation.

    %s Scan acharacter string.The scan terminates atwhitespace.Anull character is stored at the

    end of the string, which means that the buffer supplied must be at least one character longerthan the specified input length.

    %c Scan a character (char). Nonull character is added.

    E.g.:

    To read a persons name as input: scanf(%s,&name);

    Note that we use the &(ampersand) operator to get the address of the variable.

    http://en.wikipedia.org/wiki/Decimalhttp://en.wikipedia.org/wiki/Floating-pointhttp://en.wikipedia.org/wiki/Fixed-point_arithmetichttp://en.wikipedia.org/wiki/Character_stringhttp://en.wikipedia.org/wiki/Decimalhttp://en.wikipedia.org/wiki/Floating-pointhttp://en.wikipedia.org/wiki/Fixed-point_arithmetichttp://en.wikipedia.org/wiki/Character_stringhttp://en.wikipedia.org/wiki/Whitespace_(computer_science)http://en.wikipedia.org/wiki/Null_characterhttp://en.wikipedia.org/wiki/Null_characterhttp://en.wikipedia.org/wiki/Null_characterhttp://en.wikipedia.org/wiki/Null_characterhttp://en.wikipedia.org/wiki/Whitespace_(computer_science)http://en.wikipedia.org/wiki/Character_stringhttp://en.wikipedia.org/wiki/Fixed-point_arithmetichttp://en.wikipedia.org/wiki/Floating-pointhttp://en.wikipedia.org/wiki/Decimalhttp://en.wikipedia.org/wiki/Character_stringhttp://en.wikipedia.org/wiki/Fixed-point_arithmetichttp://en.wikipedia.org/wiki/Floating-pointhttp://en.wikipedia.org/wiki/Decimal
  • 8/13/2019 An Introductio to C Programming

    9/9

    Lecturer: Aurel Liddell CSI 2101 September, 2013

    9

    Exercise:

    Convert the Average Algorithm to C Source Code

    Note: When converting an algorithm to code, ensure that all variables are declared before

    they are used.

    Algorithm/ Pseudocode C source code1. Start #include

    main()

    {

    2. Get three numbers, num1, num2, num3 int num1,num2,num3,sum;

    float average;

    num1 = 3;num2 = 10;

    num3 = 2;

    3. Add num1 + num2 + num3, store in sum sum = num1+num2+num3;

    4. Divide sum by 3, storing in average average = sum/3;

    5. Print average printf(Average is %f\n,average);

    6. Stop return 0;

    }

    References:

    http://ug.howtoprogramtutor.com

    Kerningham, B. and Ritchie, D. (1988), The C programming language (2nd

    ed.), Prentice Hall

    Patt, Y. and Patel, S. (2001), Introduction to computing systems: from bits & gates to C &

    beyond. Mc Graw-Hill

    http://ug.howtoprogramtutor.com/http://ug.howtoprogramtutor.com/http://ug.howtoprogramtutor.com/