SlideServe
Menu
Préférences
© SlideServe · v2026

Engineering Problem Solving with C Fundamental Concepts

denzel avatar
28 diapositives · 464 Views · Oct 09, 2014

Engineering Problem Solving with C Fundamental Concepts. Chapter 2 Simple C Programs. Program Structure. Program Structure - General Form. preprocessing directives int main(void) { declarations statements }. Program Structure.

Engineering Problem Solving with C Fundamental Concepts - slide 1
01 / 28Engineering Problem Solving with C Fundamental Concepts
1 / 17

Télécharger la présentation

Engineering Problem Solving with C Fundamental Concepts
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs Etter/Ingber

  2. Program Structure Etter/Ingber

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

  4. 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, { } Etter/Ingber

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

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

  7. Constants and Variables Etter/Ingber

  8. 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 Etter/Ingber

  9. C Data Types • Integers • short • int • long • Floating-Point Values • float • double • longdouble • Characters • char Etter/Ingber

  10. 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 Etter/Ingber

  11. Assignment Statements Etter/Ingber

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

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

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

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

  16. Priority of Operators • Parentheses Inner most first • Unary operators Right to left (+ -) • Binary operators Left to right (* / %) • Binary operators Left to right (+ -) Etter/Ingber

  17. Increment and Decrement Operators • Increment Operator ++ • post increment x++; • pre increment ++x; • Decrement Operator - - • post decrement x- -; • pre decrement - -x; Etter/Ingber

  18. 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; Etter/Ingber

  19. Etter/Ingber

  20. Standard Input and Output Etter/Ingber

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

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

  23. Etter/Ingber

  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); Etter/Ingber

  25. Library Functions Etter/Ingber

  26. 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. • 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. Etter/Ingber

  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 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 [-, ]. Etter/Ingber

  28. 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. Etter/Ingber