Chapter 2 Slides - Engineering Problem Solving with C...

Info icon This preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style Chapter 2 11 Engineering Problem Solving with C Chapter 2 Simple C Programs – cont’d
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 2 22 Program Structure – Example: Sum of two floating point numbers /******************************************************************/ /* 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; } /***************************************************************************/ Similar to program described earlier where integers were added
Image of page 2
Chapter 2 33 Program Structure - General Form preprocessing directives int main(void) { declarations statements } Could also write int main()   to indicate that no values are being passed to the function “main”
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 2 44 Program Structure As discussed earlier: 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, { }
Image of page 4
Chapter 2 55 Program Structure – cont’d 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 (causes program to returns a value of zero to the operating system which will then indicate a successful termination of the program execution)
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 2 66 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
Image of page 6
Chapter 2 77 Constants and Variables A constant is a specific value of something (a number, a string of characters, a symbol, an array, etc.) Variables A variable is a memory location in a computer that is assigned a name or an identifier The variable name (identifier) is used to reference the memory location In addition to a name, every variable has a type, a size and a value Whenever a new value is placed into a variable it replaces (and destroys) the previous value Reading variables from memory does not change them
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 2 88 Constants and Variables variable is the memory location variable name or identifier value of the variable is in the memory location type of variable is given in the declaration statement initial values may be given in declaration Before a variable is assigned a value, it is not known what may reside in the memory location Note: Miracle C requires decimal points when declaring floating point variables
Image of page 8
Chapter 2 99 Rules for selecting a valid identifier Must begin with an alphabetic character or underscore (cannot begin variable name with a digit)
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern