1 / 28

EG280 - CS for Engineers Chapter 2, Introduction to C Part I

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. Chapter 2 Quiz Topics. Page 45 Practice! 1 – 21 Page 46 Practice! 1 – 6

Download Presentation

EG280 - CS for Engineers Chapter 2, Introduction to C Part I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EG280 - CS for EngineersChapter 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. Program Structure - General Form preprocessing directives (i.e. #include <stdio.h> int main(void) { declarations statements } 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, { } 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 return0; statement 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; } 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 anidentifier. • 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, …) EG280 CS for Engineers

  8. 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: 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. EG280 CS for Engineers

  9. C Data Types - Integer • Type NameLength (bytes)/ Range1 • short 2 / -32768 to +32767 • int 2 / -32768 to +32767 • long 4 /-2,147,483,648to+ 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. EG280 CS for Engineers

  10. C Data Types - Character • Type NameLength (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. EG280 CS for Engineers

  11. C Data Types – Floating Point • Type NameLength (bytes)/ Range1 • 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. 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 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) EG280 CS for Engineers

  14. 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 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 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) EG280 CS for Engineers

  17. Priority of Operators • Parentheses Inner most first • Unary operators Right to left (+ -) • Binary operators Left to right (* / %) • Binary operators Left to right (+ -) 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; 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; EG280 CS for Engineers

  20. 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 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 EG280 CS for Engineers

  23. EG280 CS for Engineers

  24. Practice! 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. 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); 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. 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. EG280 CS for Engineers

  27. TrigonometricFunctions 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 = [-, ]. 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. EG280 CS for Engineers

More Related