Search This Blog

C language elements


C Language elements (Sections 2.1-2.4)

Example Code:

/*
�* Converts distance in miles to kilometers.
�*/
#include <stdio.h>������������� /* printfscanf 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. maindefine� ������ /* lowercase, cannot be used for other purposes, APPENDIX E */

Standard Identifiers

e.g. printfscanf����� /* 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 identifiers1letterdoubletwo*fourjoe�s
Case sensitivity: C compiler interprets RaterateRATE as different identifiers
Read about �Program Style� p.40!

Executable Statements

Calls to library functions

print f("Enter dist in miles> ");
scan f("%lf", &miles);
print f("That equals %f kms.\n", kms);

Each call to print f and scan f begins with a format string in double quotes.

print f(format string);
print f(format string, output list);
scan f(format string, input list);

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

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

%lf in scan f for type double value
%f in print f for type double value
%d in print f and scan f for type int value
%c in print f and scan f 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:
  • The programmers name
  • The date of current version
  • A brief description of what program does

/*
�* Programmer: William Bell, Date completed: Jan 22, 2006
�* Instructor: Slobodan Vucetic
�*
�* Converts number of miles to number of kilo meters
�*/

No comments:

Post a Comment