C Language elements (Sections 2.1-2.4)

 

Example Code:

 

/*

 * Converts distance in miles to kilometers.

 */

#include <stdio.h>              /* printf, scanf definitions */

#define KMS_PER_MILE 1.609      /* conversion constant       */

 

int

main(void)

{

      double miles,  /* input - distance in miles.       */

             kms;    /* output - distance in kilometers  */

 

      /* Get the distance in miles. */

      printf("Enter the distance in miles> ");

      scanf("%lf", &miles);

 

      /* Convert the distance to kilometers. */

      kms = KMS_PER_MILE * miles;

 

      /* Display the distance in kilometers. */

      printf("That equals %f kilometers.\n", kms);

 

      return (0);

}

 

General form of a C program

 

Preprocessor directives

Main function heading

{

Declarations

Executable statements

}

 

Compiler (preprocessor) directives

 

#include <stdio.h> /* look at APPENDIX B */

 

Include the source code for library file stdio.h.

Enables C compiler to recognize printf and scanf from this library.

 

#define KMS_PER_MILE 1.609

 

Substitute 1.609 for the name KMS_PER_MILE wherever it appears.

 

Comments

 

Statements that clarify the program - ignored by compiler but "read" by humans.

 

/* Calculate cost of trip */

 

Function main

 

int main (void)   /* int is related to return(0) is program is successful*/

{

      Function body

}

 

Reserved words

 

e.g. main, define       /* lowercase, cannot be used for other purposes, APPENDIX E */

 

Standard Identifiers

 

e.g. printf, scanf     /* advanced topic: standard identifiers can be redefined by the programmer */

 

User-Defined Identifiers: Variable declarations and data types

 

Variable declaration informs compiler about all variables that will be used by the program. Standard data types are double (123.34), int (34), and char (‘w’)

 

double miles; /* Declares the variable miles that can store a real number (with a fractional part) */

 

Other declarations:

 

int kids, courses; /* Declares the variables kids and courses that can store integer values */

 

char initial; /* Declares the variable initial that can store a single character */

 

Valid identifiers: consists of letters, digits, underscores; could not start with digit, cannot use reserved words

Invalid identifiers: 1letter, double, two*four, joe’s

Case sensitivity: C compiler interprets Rate, rate, RATE as different identifiers

Read about “Program Style” p.40!

 

Executable Statements

 

Calls to library functions

 

printf("Enter dist in miles> ");

scanf("%lf", &miles);

printf("That equals %f kms.\n", kms);

 

Each call to printf and scanf begins with a format string in double quotes.

 

printf(format string);

printf(format string, output list);

scanf(format string, input list);

 

After the format string (following comma) comes an input list (for scanf) or an output list (for printf). The variables named in the input list are preceded by “&” which means “address of” (e.g., &miles).

 

%lf and %f are placeholders - They indicate the data type and position of a value in a format string. Use:

 

%lf in scanf for type double value

%f in printf for type double value

%d in printf and scanf for type int value

%c in printf and scanf for type char value

 

The two-character sequence \n represents the newline character - carriage return, line feed.

 

Assignment statements

 

Variable declaration reserves memory space for program variables. However, initial values of variables are unknown before the program assigns some values to them!!

 

Assignment statements assign values to variables.

 

The value to be assigned is written on the right hand of the assignment operator =. The variable getting the value is on the left hand side.

 

sum = x + y;

 

sum gets the value of x + y. Variables x and y must be "defined" beforehand - they must have data stored in them.

 

kids = kids + 1;

 

kids gets the value of the "current value of kids" + 1. If kids was 5, its new value will be 6

 

hypotenuse = sqrt(side1 * side1 +

                  side2 * side2);

 

Assignment statements can be quite complex!

 

Notice that

x + y = sum;

is invalid. Why?

 

The return statement

 

Return(0) informs the operating system that the program finished and that it executed without error

 

Program style: very important (read p.56-57)

 

Each program should consist of:

 

/*

 * Programmer: William Bell, Date completed: Jan 22, 2006

 * Instructor: Slobodan Vucetic

 *

 * Converts number of miles to number of kilometers

 */

 

Each step of the program should be preceded with a comment explaining what it does