eg280 - cs for engineers chapter 2, introduction to c part i topics: program structure constants and...

28
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: •Program structure •Constants and variables •Assignment Statements •Standard input and output •Mathematical and character functions

Upload: henry-hill

Post on 11-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

EG280 - CS for Engineers

Chapter 2, Introduction to C Part I

Topics:

• Program structure

• Constants and variables

• Assignment Statements

• Standard input and output

• Mathematical and character functions

Page 2: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Chapter 2 Quiz Topics• Page 45 Practice! 1 – 21• Page 46 Practice! 1 – 6• Page 50 Practice! 1 – 9• Page 54 Practice! 1 – 4• Page 56 Practice! 1 – 6• Page 60 Practice! 1 – 4 (beware answer in back of

text for #1)

Page 3: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 3

Program Structure - General Form

preprocessing directives (i.e. #include <stdio.h>

int main(void)

{

declarations

statements

}

Page 4: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 4

Program Structure

•Comments begin with /* and end */

•Preprocessor directives give instructions to the compiler (i.e., #include <stdio.h>

•Every C program contains one function named main

•The body of the main function is enclosed by braces, { }

Page 5: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 5

Program Structure - continued

• The main function contains two types of commands: declarations and statements

• Declarations and statements are required to end with a semicolon (;)

• Preprocessor directives do not end with a semicolon

• To exit the program, use a return 0; statement

Page 6: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 6

First Program

/**********************************************//* Program chapter1: compute the sum two numbers *//* */#include <stdio.h>int main(void) { /* Declare and initialize variables. */ double number1 = 473.91, number2 = 45.7, sum; /* Calculate sum. */ sum = number1 + number2;

/* Print the sum. */ printf(“The sum is %5.2f \n”, sum);

/* Exit program. */ return 0;}

Page 7: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 7

Constants and Variables•A constant is a specific value that does not change during execution.

•A variable is a memory location that is assigned a name or an identifier.

•An identifier is used to reference a memory location.

•Rules for selecting a valid identifiermust begin with an alphabetic character or underscoremay contain only letters, digits and underscore (no

special characters such as * + - / ( ), and others )case sensitive (pi and Pi and PI are unique names)cannot use keywords as identifiers (main, if, int, …)

Page 8: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 8

C Data TypesC supports three basic types of data:

integer – used to represent integer values

examples: 10 -128 1256

character – used to represent coded (i.e. ASCII) characters: 1 a A /

(I ‘’ my wife; I ‘’ my kids; I ‘’ my dog.)

floating point – used to represent scientific values examples: 3.1415927 -0.907E-12

In addition, special modifiers can be applied to restrict the range of values or to increase the precision.

For example, unsigned integers have positive (or zero) value only, and double precision floating point values can represent very small or very large scientific values.

Page 9: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 9

C Data Types - Integer

Type Name Length (bytes) / Range1

short 2 / -32768 to +32767

int 2 / -32768 to +32767long 4 / -2,147,483,648 to +

2,147,483,647

unsigned short 2 / 0 to +65535

unsigned int 2 / 0 to +65535

unsigned long 4 / 0 to + 4,294,967,295

1The actual length and range is system dependent and may vary from environment to environment. However, these are fairly typical values for many implementations.

Page 10: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 10

C Data Types - Character

Type Name Length (bytes) / Range

char 1 / -127 to +128

unsigned char 1 / 0 to +255

char data is intended to be used to hold character (ASCII) codes. However, char variables can be manipulated (added, subtracted, multiplied, …) just like int data.

Page 11: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 11

C Data Types – Floating Point

Type Name Length (bytes) / Range1

float 4 / 3.4E38 (7 digits of precision)

double 8 / 1.7308 (15 digits of precision)

long double 10/ 1.2E4932 (19 digits )

1 Ranges are approximate since there is not an exact conversion from binary to decimal floating point.

Page 12: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 12

Symbolic Constants

• Defined with a preprocessor directive

• Compiler replaces each occurrence of the directive identifier with the constant value in all statements that follow the directive

• Example

#define PI 3.141593

Page 13: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 13

Assignment Statements

• Used to assign a value to a variable; General Form:

identifier = expression;

• Example 1: double sum = 0;(sum 0)

• Example 2: int x;

x=5; (x 5)• Example 3:

char ch;ch = ‘a’; (ch 0x61 = 0b01100001)

Page 14: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 14

Assignment Statements - continued

• Example 3int x, y, z;x=y=0; x 0 and y 0 z=2; z 2

• Example 4y=z; y 2

Page 15: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 15

Arithmetic Operators

• Addition +

• Subtraction -

• Multiplication *

• Division /

• Modulus %– Modulus returns remainder of division

between two integers– Example: 5%2 returns a value of 1

Page 16: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 16

Integer Division

• Division between two integers results in an integer.

• The result is truncated, not rounded• Example:

5/3 is equal to 1 (5./3 = 5/3. = 1.6667)

3/6 is equal to 0 (3./6 = 3/6. = 0.5)

Page 17: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 17

Priority of Operators

1 Parentheses Inner most first

2 Unary operators Right to left(+ -)

3 Binary operators Left to right(* / %)

4 Binary operators Left to right(+ -)

Page 18: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 18

Increment and Decrement Operators

• Increment Operator ++

post increment z = x++;

pre increment z = ++x;• Decrement Operator --

post decrement z = x--;

pre decrement z = --x;

Page 19: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 19

Abbreviated Assignment Operator

operator example equivalent statement

+= x += 2; x = x+2;

-= x -= 2; x = x-2;

*= x *= y; x = x*y;

/= x /= y; x = x/y;

%= x %= y; x = x%y;

Page 20: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 20

Precedence of Arithmetic and Assignment Operators

Precedence Operator Associativity 1 Parentheses: () Innermost first

2 Unary operators

+ - ++ -- (type)

Right to left

3 Binary operators * / %

Left ot right

4 Binary operators + -

Left ot right

5 Assignment operators = += -= *= /= %=

Right to left

Page 21: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 21

Standard Output• printf() Function

–prints information to the screen (standard output device)

–requires two arguments

• control string

• conversion specifier

• Example

double angle = 45.5;

printf(“Angle = %.2f degrees \n”, angle);

Output: Angle = 45.50 degrees

Page 22: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 22

Standard Input• scanf() Function

– inputs values from the keyboard– required arguments

• control string• memory locations that correspond to the

specifiers in the control string• Example:

double distance;char unit_length;scanf("%1f %c", &distance, &unit_length);

It is very important to use a specifier that is appropriate for the data type of the variable

Page 23: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 23

TABLE 2.7 Conversion Specifiers for Input Statements

Variable Type Specifier Integer Values

int %i, %d short %hi, %hd long int %li, %ld unsigned int %u unsigned short %hu unsigned long %lu

Floating-Point Values float %f, %e, %E, %g, %G double %lf, %le, %lE, %lg,

%lG long double %Lf, %Le, %LE, %Lg,

%LG Character Values

char %c

Page 24: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 24

Practice!

printf("Sum = %5i; Average = %7.1f \n", sum, average);

printf("Sum = %4i \n Average = %8.4f \n", sum, average);

printf("Sum and Average \n\n %d %.1f \n", sum, average);

printf("Character is %c; Sum is %c \n", ch, sum);

printf("Character is %i; Sum is %i \n", ch, sum);

Assume that the integer variable sum contains the value 65, the double variable average contains the value 12.368 and that the char variable ch contains the value 'b'. Show the output line (or lines) generated by the following statements.

Page 25: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 25

Math Functions

fabs(x) Absolute value of x.

sqrt(x) Square root of x, where x>=0.

pow(x,y) Exponentiation, xy. Errors occur if

x=0 and y<=0, or if x<0 and y is not an integer.

ceil(x) Rounds x to the nearest integer toward (infinity).

Example, ceil(2.01) is equal to 3.

floor(x) Rounds x to the nearest integer toward - (negative infinity).

Example, floor(2.01) is equal to 2.

Page 26: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 26

Math Functions – continued

exp(x) Computes the value of ex.

log(x) Returns ln x, the natural logarithm of x to the base e. Errors occur if x 0.

log10(x) Returns log10x, logarithm of x to the base 10.

Errors occur if x 0.

Page 27: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 27

Trigonometric Functionssin(x) Computes the sine of x, where x is in radians.cos(x) Computes the cosine of x, where x is in radianstan(x) Computes the tangent of x, where x is in radians.asin(x) Computes the arcsine or inverse sine of x, where x

must be in the range [-1, 1]. Returns an angle in radians; range = [-/2,/2].

acos(x) Computes the arccosine or inverse cosine of x, where x must be in the range [-1, 1]. Returns an angle in radians; range = [0, ].

atan(x) Computes the arctangent or inverse tangent of x. The Returns an angle in radians in the range [-/2,/2].

atan2(y,x) Computes the arctangent or inverse tangent of the value y/x. Returns an angle in radians; range = [-, ].

Page 28: EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and

Spring 2001 EG280 CS for Engineers 28

Character Functions

toupper(ch) If ch is a lowercase letter, function returns the corresponding uppercase letter; otherwise, it returns ch

isdigit(ch) Returns a nonzero value if ch is a decimal digit; otherwise, it returns value of zero.

islower(ch) Returns a nonzero value if ch is a lowercase letter; otherwise, it returns value of zero.

isupper(ch) Returns a nonzero value if ch is an uppercase letter; otherwise, it returns value of zero.

isalpha(ch) Returns a nonzero value if ch is an uppercase letter or a lowercase letter; otherwise, it returns zero.

isalnum(ch) Returns a nonzero value if ch is an alphabetic character or a numeric digit; otherwise, it returns zero.