Programming in C

 

Summary of today’s lecture

•      What C is NOT

•      Look at a C program, noting differences from a C++ program

•      Input and Output in C

–   What C uses instead of cin and cout

–   Formatting output for numbers and strings

•      Preprocessor directives and macros

•      Command line arguments

 

 

What C is not

•      The following are not supported in C

–    Classes and object-based programming

–    Vectors and string type; you must use pointers

–    Strict type checking;  programs will compile easier;  more runtime errors

–    Reference variables or call by reference; you must use pointers if you want to change arguments

–    Function overloading; a function name can be used only once

–    Operator overloading;

–    Templates

–    Default arguments for functions

–    The // comment

 

Typical C program

•      #include <stdio.h>
#define kmPerMile 1.609
int main(void)
{    double miles, kms;    
/*variables */
      printf(“Enter the distance in miles: “);
      scanf(“%lf”, &miles);
      kms = kmPerMile * miles;
      printf(“That equals %f kilometers.\n”, kms);
      return (0);
}

 

The printf function

•      Note that this is a function with an argument

•      When the printf function is called, the argument consists of

–    a format string (in quotes) and

–    A print list

•      The format string contains placeholders that begin with the symbol % followed by a symbol standing for a type
              
printf(“That equals %f kilometers.\n”, kms);

•      The print list contains the variables that belong to each placeholder

–    The placeholder and print list must match in type

 

Another example of printf

•      char letter1, letter2, letter3;
int age;
  /*  assume values for variables */

•      printf(“hi %c%c%c – your age is %d \n”,
                            letter1,letter2, letter3, age);

 

 

 

The scanf function

•      The argument to scanf also has two parts

–    Format string

–    Input list

                scanf(“%lf”, &miles);

•      The format string is enclosed in quotes, and indicates the type to be read

•      The input list is the address of the variable names where the input should be stored

–    Since it must be the address of the variable names, each variable is preceded by an &

 

Example of scanf

•      Suppose we wanted to read in the three char, then an age, for the last printf output

•      scanf(“%c%c%c%d”,
             &let1, &let2, &let3, &age);

•      To output this infor use the printf
 printf(“hi %c%c%c – your age is %d \n”,
                            let1,let2, let3, age);

 

Placeholders in Format Strings

•      C matches variables with placeholders in left-to-right order

Formatting values of type int

•      To specify the field width of an integer for output

–   Add a number between the % and the d of the placeholder in the printf format string

–   Example
printf(“Results: %3d meters = %4d ft %2d in\n”, meters, feet, inches);

Results: 21 meters=  68ft 11 in

 

Formatting values of type double

•       Both the field width and number of decimal places must be indicated

–    You must be sure to include enough places in the field width to include the decimal point

•      The form of the placeholder is %n.mf where n is the field width and m is the desired number of decimal places

–    If the number of decimal places is smaller than the internal number, it will be rounded

•      Eliminating leading blanks

–    For ints, just omit the field width number  %d

–    For floats, omit the number before the period %.mf

 

Some exercises for you to do

•             Variables double x = 12.335;  
                      int y = 100;

•             Show what will be displayed by the following statements.  Use ▓ for blanks.

1.      printf(“x is %6.2f  y is %4d\n”, x, y);

2.      printf(“y is %d\n”,  y);

3.      printf(“x is %.1f ”, x);

 

Array elements in scanf

   #define MAX  8 
int main(void)
{   double x[MAX];
      int    I;
 printf("Enter %d numbers separated by blanks or
                                                 returns: \n ",  MAX);
             for  (i = 0;  i < MAX;  ++i)
               scanf("%lf", &x[i]);

……other stuff

•      If you put a semicolon after the #define MAX  8 then it becomes part of the string that is substituted for MAX

 

printf and scanf with strings

•      If a string’s actual size is less than the declared char array size, all of C string handling functions ignore the rest of the array.

•      The %s placeholder can take a field width just as for ints and floats

–    If the field width is larger than the string, the string will be right justified

•    i.e. it will be padded with spaces on the left

–    If you want the strings left justified, use negative field widths

–    If the width is less than the string length, the field is expanded to the size of the string with no padding.

 

Examples formatting strings

   printf(“***%8s***%3s***\n”, “Short”, “Strings” );

•      Suppose the following is in a loop to print out president’s names that the user types in.

•      printf(“%-20s\n, president);

•      With the minus sign, the president’s names will be left justified

–   Without the minus sign, they will be right justified

 

Strings and scanf

•      Since strings are really arrays of characters, the array name is the address of the first character in the string

•      Since it is already an address, the & is not needed

•      Example

 

Questions

4. When the scanf function is scanning a string, if there is more input data than will fit in the declared size of the array, it will _____. (choose one)

a. Copy only those characters that will fit and ignore the rest

b. Copies the whole string overflowing the array because scanf has no way of knowing the arrays’s declared size.

c. Scans all the characters but stores only the ones that fit, discarding the rest.

5. When printf is given a string argument to print using a %s placeholder, how does it know how many characters to print?

6. Write the C code to declare a 5 character array, and initialize it to a string of 4 blanks.