Presentation is loading. Please wait.

Presentation is loading. Please wait.

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.

Similar presentations


Presentation on theme: "Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs."— Presentation transcript:

1

2 Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs

3 Etter/Ingber Program Structure

4 Etter/Ingber Program Structure - General Form preprocessing directives int main(void) { declarations statements }

5 Etter/Ingber Program Structure Comments begin with the characters /* and end with the characters */ Preprocessor directives give instructions to the compiler Every C program contains one function named main The body of the main function is enclosed by braces, { }

6 Etter/Ingber 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

7 Etter/Ingber Program Structure - First Program /******************************************************************/ /* Program chapter1 */ /* */ /* This program computes 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; } /***************************************************************************/

8 Etter/Ingber Constants and Variables

9 Etter/Ingber Constants and Variables A constant is a specific value 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) case sensitive can not use keywords as identifiers

10 Etter/Ingber C Data Types Integers short int long Floating-Point Values float double long double Characters char

11 Etter/Ingber Symbolic Constants 4 Defined with a preprocessor directive 4 Compiler replaces each occurrence of the directive identifier with the constant value in all statements that follow the directive 4 Example –#define PI 3.141593

12 Etter/Ingber Assignment Statements

13 Etter/Ingber Assignment Statements 4 Used to assign a value to a variable 4 General Form: identifier = expression; 4 Example 1 double sum = 0;sum 4 Example 2 int x; x=5;x 4 Example 3 char ch; ch = a;a 0 5 a

14 Etter/Ingber Assignment Statements - continued 4 Example 3 int x, y, z; x=y=0; z=2;x y z 4 Example 4 y=z;y 0 0 2 2

15 Etter/Ingber Arithmetic Operators 4 Addition+ 4 Subtraction- 4 Multiplication* 4 Division/ 4 Modulus% –Modulus returns remainder of division between two integers –Example 5%2 returns a value of 1

16 Etter/Ingber Integer Division 4 Division between two integers results in an integer. 4 The result is truncated, not rounded 4 Example: 5/3 is equal to 1 3/6 is equal to 0

17 Etter/Ingber Priority of Operators 1 ParenthesesInner most first 2 Unary operatorsRight to left (+ -) 3 Binary operatorsLeft to right (* / %) 4 Binary operatorsLeft to right (+ -)

18 Etter/Ingber Increment and Decrement Operators 4 Increment Operator ++ post incrementx++; pre increment++x; 4 Decrement Operator - - post decrementx- -; pre decrement- -x;

19 Etter/Ingber 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 Etter/Ingber

21 Standard Input and Output

22 Etter/Ingber Standard Output 4 printf Function –prints information to the screen –requires two arguments control string conversion specifier 4 Example double angle = 45.5; printf(Angle = %.2f degrees \n, angle); Output: Angle = 45.50 degrees

23 Etter/Ingber Standard Input 4 scanf Function –inputs values from the keyboard –required arguments control string memory locations that correspond to the specifiers in the control string 4 Example: double distance; char unit_length; scanf("%1f %c", &distance, &unit_length); X It is very important to use a specifier that is appropriate for the data type of the variable

24 Etter/Ingber

25 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.

26 Etter/Ingber Library Functions

27 Etter/Ingber 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. 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 log10x, logarithm of x to the base 10. Errors occur if x<=0.

28 Etter/Ingber 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 in the 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 in the 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 in the range [-, ].

29 Etter/Ingber Character Functions toupper(ch)If ch is a lowercase letter, this 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 a zero. islower(ch)Returns a nonzero value if ch is a lowercase letter; otherwise, it returns a zero. isupper(ch)Returns a nonzero value if ch is an uppercase letter; otherwise, it returns a zero. isalpha(ch)Returns a nonzero value if ch is an uppercase letter or a lowercase letter; otherwise, it returns a zero. isalnum(ch)Returns a nonzero value if ch is an alphabetic character or a numeric digit; otherwise, it returns a zero.


Download ppt "Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs."

Similar presentations


Ads by Google