Unformatted text preview:

C Programming Basics February 2 2006 Outline Basics of C Programming Review last lecture Basic structure of a C program Larry Caretto Computer Science 106 Comments and statements White space not part of program Variables are case sensitive Collection of functions with execution starting in a function called main Computing in Engineering and Science February 2 2006 Screen output and keyboard input 2 Review Basic C Programs http www csun edu lcaretto comp106 A C program is a collection of functions Structure of computer Programming in higher level language like C Execution starts in the main function Functions may be in more than one file Have comments and code Must include libraries Compile link execute debug Integrated development environment Design code test Initial assignments will have only one function called main Program correctness essential 3 A Note on Syntax 4 Basic C Program Structure When we describe programs we distinguish between items that are written as stated and descriptions of other items that are required or optional Items written as stated will be in a nonproportional font like this one Descriptions of other items will be in bold italic in brackets like this Example Name your name include iostream using namespace std int main your program statements return EXIT SUCCESS May have other include statements 5 6 1 C Programming Basics February 2 2006 Purpose of Basic Structure Purpose of Basic Structure II The include iostream statement invokes the basic input output library The using namespace std statement defines standard names so you do not have to declare them in your code The int main declares the start of the main function and declares it to be of type int more on types next week The opening and closing braces and mark the start and end of the main function EXIT SUCCESS is a symbolic constant defined to have a value of zero The return statement passes a value to the calling function in this case the operating system 7 8 C Statements Program Variables C statements can have multiple lines The end of a C statement is marked with a semicolon White spaces are ignored in C w x y is the same as w x y C statements use program variables that represent computer memory locations Look like mathematical variables but they not Program variables describe operations on memory locations e g Take the value from memory location 121 Add to this the value from location 237 Store the result in location 142 C writes this as w x y 9 10 Programming vs Math Programming vs Math II What the statements on the previous slide mean Consider the following operations y 2 x y y 3 What is x at the end of this sequence In mathematics x would equal y which is 3 In programming we set the value of x to the value of y when y was 2 thus x is 2 11 y 2 assign a value 2 to the variable named y representing a memory location x y find the value of the variable named y and assign that value to x y 3 assign a value 3 to y Once x is set to the value of y 2 it is not changed in this sequence 12 2 C Programming Basics February 2 2006 More on Variables Guidelines for Variables Rules for variable names Use meaningful variable names Can have up to 31 characters actually more but only first 31 are recognized Allowed characters are A Z a z 0 9 and Variable name cannot start with 0 9 Variables names are case sensitive Answer is not the same as answer Variable names refer to memory locations whose values you can change innerRadius outerRadius rInner rOuter rIn rOut are good examples Referring to an inner radius and an outer radius as x and y is not a good idea In lecture we will often use single letter variables for simplicity This is reasonable when we represent common physical abbreviations KE mV2 2 could use variable m for mass V for velocity and KE 13 14 Variables and Data Types Changing Variable Values Variables must be declared as having a certain data type before they are used Variables should be declared as close to the point of their first use as possible Variables are usually assigned a value when they are first declared We will cover data types in detail next week Can change a value by input statements and assignment statements Use as the assignment operator x 2 assigns the value 2 to x x y assigns the value of y to x x x 2 takes the existing value of x adds 2 to it and assigns this to x What is x after x 1 y 2 x x y The value of x is 3 15 Input and Output 16 Output using cout cout string writes the string between the quotation marks to the screen cout x writes the value of the variable x to the screen Can have one or more output operators in a single cout command We use the following commands and operators for input and output Use the command cout for screen output Use the command cin for keyboard input Use the output insertion operator between output items for screen and files Use the input extraction operator between input items from keyboard and files Code cout Name int x 2 cout x cout x x 17 Screen output Name 2 x 2 18 3 C Programming Basics February 2 2006 Input using cin Output spacing Input prompt tells user what to input Enter several variables with a single cin command Separate entries by a space and press enter the Enter key after last entry Code Actions cout Enter x cin x Type 2 3 enter for x 2 3 cout Enter x y and z Type 1 4 3 2 12 7 enter cin x y z to set x 1 4 y 3 2 and z 12 7 cout does not provide any spacing between output or new lines E g x 13 2 y 12 6 cout x y or cout x cout y would print 13 212 6 You can put a string of blanks in your cout commands cout x y would print 13 2 12 6 19 Escape sequences 20 Equivalent Statements cout n radius r ndiameter d n area a cout n radius r ndiameter d n area a cout endl cout radius r endl cout diameter d endl cout area a Special characters to control printing entered in strings n for new line can also use endl t for tab for quotation mark for backslash E g cout x t y n Or cout x t y endl 21 Equivalent Statements II 22 Output Errors What is the output from these statements double r 1 PI 3 14159265358979 double d 2 r double a PI r r cout n radius r ndiameter d n area a radius 1 Default output is six diameter 2 significant figures area 3 14159 with no trailing zeros 23 What is wrong with these statements cout Enter x cin y cout x …


View Full Document

CSUN COMP 106 - Basics of C++ Programming

Download Basics of C++ Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Basics of C++ Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Basics of C++ Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?