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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online