Download presentation

Presentation is loading. Please wait.

Published byHenry Hill Modified over 4 years ago

1
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

2
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)

3
Spring 2001EG280 CS for Engineers3 Program Structure - General Form preprocessing directives (i.e. #include int main(void) { declarations statements }

4
Spring 2001EG280 CS for Engineers4 Program Structure Comments begin with /* and end */ Preprocessor directives give instructions to the compiler (i.e., #include Every C program contains one function named main The body of the main function is enclosed by braces, { }

5
Spring 2001EG280 CS for Engineers5 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

6
Spring 2001EG280 CS for Engineers6 First Program /**********************************************/ /* Program chapter1: compute the sum two numbers */ /* */ #include 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; }

7
Spring 2001EG280 CS for Engineers7 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 identifier must begin with an alphabetic character or underscore may 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, …)

8
Spring 2001EG280 CS for Engineers8 C Data Types C 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: 1aA/ (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.

9
Spring 2001EG280 CS for Engineers9 C Data Types - Integer Type NameLength (bytes) / Range 1 short 2/ -32768 to +32767 int2/ -32768 to +32767 long4/ -2,147,483,648 to + 2,147,483,647 unsigned short 2/ 0 to +65535 unsigned int2/ 0 to +65535 unsigned long4/ 0 to + 4,294,967,295 1 The actual length and range is system dependent and may vary from environment to environment. However, these are fairly typical values for many implementations.

10
Spring 2001EG280 CS for Engineers10 C Data Types - Character Type NameLength (bytes) / Range char 1/ -127 to +128 unsigned char1/ 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.

11
Spring 2001EG280 CS for Engineers11 C Data Types – Floating Point Type NameLength (bytes) / Range 1 float 4/ 3.4E 38 (7 digits of precision) double 8/ 1.7 308 (15 digits of precision) long double 10/ 1.2E 4932 (19 digits ) 1 Ranges are approximate since there is not an exact conversion from binary to decimal floating point.

12
Spring 2001EG280 CS for Engineers12 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

13
Spring 2001EG280 CS for Engineers13 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)

14
Spring 2001EG280 CS for Engineers14 Assignment Statements - continued Example 3 int x, y, z; x=y=0;x 0 and y 0 z=2; z 2 Example 4 y=z;y 2

15
Spring 2001EG280 CS for Engineers15 Arithmetic Operators Addition+ Subtraction- Multiplication* Division/ Modulus% –Modulus returns remainder of division between two integers –Example: 5%2 returns a value of 1

16
Spring 2001EG280 CS for Engineers16 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)

17
Spring 2001EG280 CS for Engineers17 Priority of Operators 1ParenthesesInner most first 2Unary operatorsRight to left (+ -) 3Binary operatorsLeft to right (* / %) 4Binary operatorsLeft to right (+ -)

18
Spring 2001EG280 CS for Engineers18 Increment and Decrement Operators Increment Operator ++ post incrementz = x++; pre incrementz = ++x; Decrement Operator -- post decrementz = x--; pre decrementz = --x;

19
Spring 2001EG280 CS for Engineers19 Abbreviated Assignment Operator operatorexampleequivalent 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;

20
Spring 2001EG280 CS for Engineers20

21
Spring 2001EG280 CS for Engineers21 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

22
Spring 2001EG280 CS for Engineers22 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); XIt is very important to use a specifier that is appropriate for the data type of the variable

23
Spring 2001EG280 CS for Engineers23

24
Spring 2001EG280 CS for Engineers24 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.

25
Spring 2001EG280 CS for Engineers25 Math Functions fabs(x)Absolute value of x. sqrt(x)Square root of x, where x>=0. pow(x,y)Exponentiation, x y. 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.

26
Spring 2001EG280 CS for Engineers26 Math Functions – continued exp(x)Computes the value of e x. log(x)Returns ln x, the natural logarithm of x to the base e. Errors occur if x 0. log10(x)Returns log 10 x, logarithm of x to the base 10. Errors occur if x 0.

27
Spring 2001EG280 CS for Engineers27 Trigonometric Functions sin(x)Computes the sine of x, where x is in radians. cos(x)Computes the cosine of x, where x is in radians tan(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 = [- , ].

28
Spring 2001EG280 CS for Engineers28 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.

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google