Docstoc

Lecture 05 - C Programming Basics - 09

Document Sample
Lecture 05 - C Programming Basics - 09 Powered By Docstoc
					                                   Engineering H192 - Computer Programming




                 C Programming Basics


                            Lecture 5




Winter Quarter     The Ohio State University                 Lect 5   P. 1
                   Gateway Engineering Education Coalition
                                     Engineering H192 - Computer Programming



                 ENG H192 Course Web Page


• A web page which contains the course syllabus,
  updated lecture notes and other useful
  information may be found at:

                      http://feh.osu.edu

• A copy of the Excel-based flowchart spreadsheet
  is on the Web site

Winter Quarter       The Ohio State University                 Lect 5   P. 2
                     Gateway Engineering Education Coalition
                                   Engineering H192 - Computer Programming



                   C Program Basics

• C vs. C++

      – C is a subset of C++. All of features in C are
        contained in C++
      – C++ adds more libraries with functions for
        object oriented programming
      – C++ also adds more keywords and some
        added features.



Winter Quarter     The Ohio State University                 Lect 5   P. 3
                   Gateway Engineering Education Coalition
                                   Engineering H192 - Computer Programming



                 Keywords in C and C++
Certain words have a special meaning to the C or
C++ compiler. They are called reserved words or
keywords. We should not try to use these words as
names of variables or function names in a program.

The keyword list for C contains 32 words (D&D –
Section 2.6). C++ adds 30 more keywords (D&D –
Section 18.6).




Winter Quarter     The Ohio State University                 Lect 5   P. 4
                   Gateway Engineering Education Coalition
                                      Engineering H192 - Computer Programming



                 Some Keywords in C and C++
   asm              double               new                    switch
   auto             else                 operator               template
   break            enum                 private                this
   case             extern               protected              throw
   catch            float                public                 try
   char             for                  register               typedef
   class            friend               return                 union
   const            goto                 short                  unsigned
   continue         if                   signed                 virtual
   default          inline               sizeof                 void
   delete           int                  static                 volatile
   do               long                 struct                 while


Winter Quarter        The Ohio State University                      Lect 5   P. 5
                      Gateway Engineering Education Coalition
                                    Engineering H192 - Computer Programming



                 Program Structure in C

• EACH complete C program is composed of:

      –   Comment statements
      –   Pre-processor directives
      –   Declaration statements
      –   One or more functions
      –   Executable statements




Winter Quarter      The Ohio State University                 Lect 5   P. 6
                    Gateway Engineering Education Coalition
                                   Engineering H192 - Computer Programming



                 Program Structure in C
• EACH complete C program is composed of:
   – Comment statements
   – Pre-processor directives
   – Comment statements
   – Declaration statements
   – Comment statements
   – One or more functions
   – Comment statements
   – Executable statements
   – Comment statements

Winter Quarter     The Ohio State University                 Lect 5   P. 7
                   Gateway Engineering Education Coalition
                                  Engineering H192 - Computer Programming



                 Comment Statements
• Formal Comments:
                 /* Comment ….. */
   – Used for detailed description of functions or
     operations (for our benefit, not compiler’s).
   – Can take multiple lines in source file.
• Informal Comments (only in C++, not C):
       // Comment ….. Ends at the end of line
   – Used for quick comments like:
      int temp; // temporary variable for storing
                 // the input value

Winter Quarter    The Ohio State University                 Lect 5   P. 8
                  Gateway Engineering Education Coalition
                                    Engineering H192 - Computer Programming



                 Pre-Processor Directives

#include -- header files for library functions
      Example:
      #include <stdio.h>
                               Note Space
#define -- define constants and macros
      Examples:
      #define e 2.7182818
      #define pi 3.14159265359
                      Note Spaces
Winter Quarter      The Ohio State University                 Lect 5   P. 9
                    Gateway Engineering Education Coalition
                                 Engineering H192 - Computer Programming



                      Declarations

• Declarations tell the compiler what variable
  names will be used and what type of data each
  can handle (store).

• Example declarations:
     int a, b, c ;                       NOTE: semicolon
     float r, p, q ;
     double x, y, z ;
     char m, n ;

Winter Quarter   The Ohio State University                 Lect 5   P. 10
                 Gateway Engineering Education Coalition
                                 Engineering H192 - Computer Programming



                        Data Types

• Integer variables:                                       int a, b ;

• Integer variables, like a or b, store only whole
  numbers like 3 or 7, not 3.33 or 7.65, and only up
  to certain maximum values.
• Floating point variables:                                float c, d ;

• Floating point variables, like c or d, store rational
  numbers, like 3.14159, but only a limited number
  of digits of precision.

Winter Quarter   The Ohio State University                        Lect 5   P. 11
                 Gateway Engineering Education Coalition
                                       Engineering H192 - Computer Programming



                 Internal Storage Representation
• Definitions:
   – Binary digit -- or a "bit", is either a 0 or a 1
   – Byte -- usually a collection of 8 bits together
   – Word -- often a collection of 4 bytes together
• On the Unix system we are using:
   – an "int" data type takes up 4 bytes
     (on some systems, an "int" is only 2 bytes)
   – a "float" data type takes up 4 bytes
   – a "double" data type take up 8 bytes
   – a "char" data type takes up 1 byte

Winter Quarter         The Ohio State University                 Lect 5   P. 12
                       Gateway Engineering Education Coalition
                                  Engineering H192 - Computer Programming



       Programs Have One or More Functions

• Even the main program is a function.

    The body of each user-written function is
    enclosed in braces, { } (or curly brackets)

• The syntax of a function is:
    <function type> function_name (arg. list)
       {           /* beginning of function */
       }           /* end of function       */

Winter Quarter    The Ohio State University                 Lect 5   P. 13
                  Gateway Engineering Education Coalition
                                   Engineering H192 - Computer Programming



                 Executable Statements
 • Simple
     Declaring variables
        int temp ;
       char a ;
     Assigning Values
       temp = 5 ; temp is assigned the value of 5
 • Complex, i.e., Calling Functions
              plotxy (x, y) ;
 • Calculations
              x = (5. / 2 + 6) * 7 ;

Winter Quarter     The Ohio State University                 Lect 5   P. 14
                   Gateway Engineering Education Coalition
                                  Engineering H192 - Computer Programming



                 Arithmetic Operators

*       multiply                   +     add
/       divide                     -     subtract
%       remainder, where:
    x = 13 % 5 ;    /* x will be equal to 3 */

• An expression can be used almost anywhere a
  variable of the same type can be used.
  Ex. expressions:       num + 3, a * d - 5, ...


Winter Quarter    The Ohio State University                 Lect 5   P. 15
                  Gateway Engineering Education Coalition
                                     Engineering H192 - Computer Programming



   Arithmetic Operators – Order of Evaluation

1. Parentheses: () – Evaluate from the inside out

2. Multiplication, Division, and Remainder:
   *, /, and %

3. Addition and Subtraction:
   + and -
      NOTE: Multiple occurrences of operations with the same
      precedence evaluate from left to right. (D&D – Section 2.4)


Winter Quarter       The Ohio State University                 Lect 5   P. 16
                     Gateway Engineering Education Coalition
                                 Engineering H192 - Computer Programming



    Arithmetic Operators – Order of Evaluation

For example:

x = 2 * 3 - (4 + 5) + 8 % 7;

x   =    2 * 3 - 9 + 8 % 7;
x   =    6 - 9 + 8 % 7;
x   =    6 - 9 + 1;
x   =    -3 + 1;
x   =    -2;

Winter Quarter   The Ohio State University                 Lect 5   P. 17
                 Gateway Engineering Education Coalition
                                 Engineering H192 - Computer Programming



   Arithmetic Operators – Order of Evaluation

Another example:

x = 6 / 2 + 1 - 3 + 8 * 4;
x = 33;


x = 6 / (2 + 1) - (3 + 8) * 4;
x = -42;



Winter Quarter   The Ohio State University                 Lect 5   P. 18
                 Gateway Engineering Education Coalition
                                   Engineering H192 - Computer Programming



                 Mixed Mode Arithmetic

• When performing arithmetic operations, the
  "mode" will one of:
   – Floating point, if both operands are floating
     point
   – Integer, if both operands are integer
   – Mixed, if one operand in integer and the other
     is floating point -- the result is floating point
• Integer operations produce integer results
  (remember how you first learned to to division?)


Winter Quarter     The Ohio State University                 Lect 5   P. 19
                   Gateway Engineering Education Coalition
                                     Engineering H192 - Computer Programming



                 Assignment Operators
Operator:             Example:                      Meaning:

     =                x=5;                          x=5;
    +=                x += 5 ;                      x=x+5;
    –=                x –= 5 ;                      x=x–5;
    /=                x /= 5 ;                      x=x/5;
    *=                x *= 5 ;                      x=x*5;
    %=                x %= 5;                       x= x%5;



Winter Quarter       The Ohio State University                 Lect 5   P. 20
                     Gateway Engineering Education Coalition
                                      Engineering H192 - Computer Programming



                  Assignment Operators

Example of assignment operators:

  int a = 4, b = 2, c = 36 ;
  a += b ;            /* This adds b to a, a = ?                  */

  c /= a + b ;          /* What is value of c now? */




 Winter Quarter       The Ohio State University                 Lect 5   P. 21
                      Gateway Engineering Education Coalition
                                      Engineering H192 - Computer Programming



                  Assignment Operators

Example of assignment operators:

  int a = 4, b = 2, c = 36 ;
  a += b ;            /* This adds b to a, a = ? */
  [ Answer: a = a + b, so a = 4 + 2 or a = 6 ]
  c /= a + b ;        /* What is value of c now? */




 Winter Quarter       The Ohio State University                 Lect 5   P. 22
                      Gateway Engineering Education Coalition
                                      Engineering H192 - Computer Programming



                  Assignment Operators

Example of assignment operators:

  int a = 4, b = 2, c = 36 ;
  a += b ;            /* This adds b to a, a = ?  */
  [ Answer: a = a + b, so a = 4 + 2 or a = 6 ]
  c /= a + b ;        /* What is value of c now? */
  [ Answer: c = c / (a + b), and a = 6 now,
    so c = 36 / (6 + 2), so c = 36 / 8 or c = 4 ]


 Winter Quarter       The Ohio State University                 Lect 5   P. 23
                      Gateway Engineering Education Coalition
                                       Engineering H192 - Computer Programming



                 Increment/Decrement Operators

Operator:                 Meaning:                               When?

count++ ;             count = count + 1 ;                        After use
++count ;             count = count + 1 ;                        Before use
count-- ;             count = count - 1 ;                        After use
--count ;             count = count - 1 ;                        Before use




Winter Quarter         The Ohio State University                    Lect 5   P. 24
                       Gateway Engineering Education Coalition
                                       Engineering H192 - Computer Programming



                 Increment/Decrement Operators

Examples of increment and decrement operators:
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now?                           */

c = b-- - ++a ;
/* What are the values of a, b, c now?                           */



Winter Quarter         The Ohio State University                      Lect 5   P. 25
                       Gateway Engineering Education Coalition
                                       Engineering H192 - Computer Programming



                 Increment/Decrement Operators

Examples of increment and decrement operators:
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now? */
              (Answers: a = 5, b = 1, c = 7)
c = b-- - ++a ;
/* What are the values of a, b, c now?      */




Winter Quarter         The Ohio State University                 Lect 5   P. 26
                       Gateway Engineering Education Coalition
                                       Engineering H192 - Computer Programming



                 Increment/Decrement Operators
Examples of increment and decrement operators:
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now? */
              (Answers: a = 5, b = 1, c = 7)
c = b-- - ++a ;
/* What are the values of a, b, c now?      */
             (Answers: a = 6, b = 0, c = -5)



Winter Quarter         The Ohio State University                 Lect 5   P. 27
                       Gateway Engineering Education Coalition
                                  Engineering H192 - Computer Programming



                 Relational Operators
Operator:             Meaning:

    <                 Less Than
    >                 Greater Than
    <=                Less Than or Equal To
    >=                Greater Than or Equal To
    ==                Exactly Equal To
    !=                Not Equal To



Winter Quarter    The Ohio State University                 Lect 5   P. 28
                  Gateway Engineering Education Coalition
                                    Engineering H192 - Computer Programming



                  Relational Operators
• Used for asking questions like:
      Is x bigger than 10?

• In C, the value of 1 stands for true and 0 stands
  for false. But C will recognize any non zero value
  as true.

• NOTE:          "==" is NOT same as "="




Winter Quarter      The Ohio State University                 Lect 5   P. 29
                    Gateway Engineering Education Coalition
                                 Engineering H192 - Computer Programming



                 Logical Operators

! (not)
    Ex: a != b is true if a and b are not equal

 && (and)
     Ex: 5<6 && 7>4 is true, but
          5>6 && 7>4 is not true (i.e., false)
|| (or)
     Ex: 5>6 || 7>4 is true
         5<6 || 7<4 is also true

Winter Quarter   The Ohio State University                 Lect 5   P. 30
                 Gateway Engineering Education Coalition
                                     Engineering H192 - Computer Programming



                 Exponentiation Operations

Exponentiation is not written as x**2 or x^2
    C does not have an exponentiation operator. You
    can use the math function pow (a, b) which
    raises a to the b power. You must put a #include
    <math.h> in your source code and you might
    need the -lm switch in your compile command
    when on some UNIX systems, for example:

    >g++ -o myprog.out myprog.cpp -lm

Note that g++ on Linux does not need the "-lm".
Winter Quarter       The Ohio State University                 Lect 5   P. 31
                     Gateway Engineering Education Coalition
                                    Engineering H192 - Computer Programming

                    Skeleton Program
/**************************************/
/* Name: Brutus Buckeye               */
/* Seat No. 0, Instr: W. Hayes */
/* Program progname                   */
/**************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ( )
{
     statements ;
}
Winter Quarter      The Ohio State University                 Lect 5   P. 32
                    Gateway Engineering Education Coalition

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:8
posted:2/9/2012
language:English
pages:32