Chapter 2 Slides

Chapter 2 Slides - Click to edit Master subtitle style...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter 2 Chapter 2 11 Engineering Problem Solving with C Chapter 2 Simple C Programs – cont’d Chapter 2 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 Chapter 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” Chapter 2 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, { } Chapter 2 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 ; statement (causes program to returns a value of zero to the operating system which will then indicate a successful termination of the program execution) Chapter 2 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 Chapter 2 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 Chapter 2 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...
View Full Document

This note was uploaded on 09/26/2008 for the course ESE 124 taught by Professor Sussman-fort during the Spring '08 term at SUNY Stony Brook.

Page1 / 74

Chapter 2 Slides - Click to edit Master subtitle style...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online