{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cpts121-2-1

# cpts121-2-1 - CptS 121 Fall 09 Lecture 2-1 HK Chapter 2...

This preview shows pages 1–7. Sign up to view the full content.

1 CptS 121 L2-1 – 8/31/09 Prof. Chris Hundhausen CptS 121 Fall ‘09 Lecture 2-1 HK Chapter 2: Variables, Data Types, and Statements Lecture Outline I. C Language Elements II. III. Executable Statements IV. C Program Structure

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 CptS 121 L2-1 – 8/31/09 Prof. Chris Hundhausen Introducing C… Developed in 1972 by Dennis Ritchie at Originally designed as language in which to program UNIX operating system Originally geared toward operating systems programming Has evolved into a general-purpose programming language Has a reputation for being on the low end of the "high-level" programming language spectrum
3 CptS 121 L2-1 – 8/31/09 Prof. Chris Hundhausen C Language Elements Let's take a look at the program presented in the last lecture: /* * Computes the average of three quiz scores */ #include <stdio.h> /* printf, scanf defs */ #define NUM_SCORES 3 int main(void) { int score1, score2, score3; double average; /* Get the scores */ printf("Enter quiz score 1: "); scanf("%d", &score1); printf("Enter quiz score 2: "); scanf("%d", &score2); printf("Enter quiz score 3: "); scanf("%d", &score3); /* Compute average */ average = ((double) (score1 + score2 + score3))/ ((double)NUM_SCORES); /* Display result */ printf("The average of those three scores is %f.", average); return(0); } Comment Preprocessor directive Function header Variable Punctuation Library function Constant

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 CptS 121 L2-1 – 8/31/09 Prof. Chris Hundhausen Preprocessor Directives Begin with "#" Tell the preprocessor to modify the program before compilation ANSI (American National Standards Institute) C defines several standard libraries To use a library, you must include its header file in your code using #include #define can be used to instruct preprocess to replace each occurrence of a textual constants (e.g., NUM_SCORES ) with a value (e.g., 3) Convention: constants are in all caps
5 CptS 121 L2-1 – 8/31/09 Prof. Chris Hundhausen Function main All C programs must define a main function It is the place where execution begins Contains a series of executable statements separated by punctuation to help compiler interpret the statements

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 CptS 121 L2-1 – 8/31/09 Prof. Chris Hundhausen Reserved Words and User-Defined Identifiers Reserved words Always in lowercase Have special meaning (e.g., int , double ) User-Defined Identifiers Name memory cells used for computations ("variables") Name our own custom algorithms (functions) Must consist only of letters, numbers, and underscores Must not begin with digits Must not conflict with reserved words Should not redefine standard C library identifiers
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 25

cpts121-2-1 - CptS 121 Fall 09 Lecture 2-1 HK Chapter 2...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online