Storing data in Memory CS1A Review P1 Once you give a place in memory a specific name you can refer to that location by using that name the identifier symbolic referencing Identifiers Programming Basics a descriptive name that maps to a location in the computers memory Identifiers should have meaningful names We have identifiers so we can Retrieve data Reuse data Modify data 2 of 21 Topic 1 Review of CS1A 2 types of Identifiers When to use them Variables contains data values that may change during program execution When you need to store input When you need to store a value or the result of an expression The amount of memory to be reserved is determined at compile time Value is determined at runtime Easier change in the future Increases readability of expressions The amount of memory to be reserved is determined at compile time Value is determined at compile time The value must be declared 3 of 21 The compiler needs to know two things 1 The type of data that needs to be stores How the contents of memory need to be viewed C is strongly typed You have to be specific about what you are storing 2 How much memory is needed to store your data In C the data type provides both pieces of information Topic 1 Review of CS1A Topic 1 Review of CS1A Syntax Declaring Identifiers 5 of 21 Constants When you have a numerical literal Named Constants contains data values that can t be changed during program execution Topic 1 Review of CS1A Variables Description 4 Size Data Values Examples int integer 4 bytes or integers whole s 3 4 235 2 147 483 648 to 2 147 483 647 12152 23 char character 1 byte Characters enclosed in single quotes a z d f float floating point number 4 bytes Pos or neg decimal numbers including fractional part up to 7 digits 32 2 23 32 0 0 123 2332 short short integer 2 bytes Pos or neg integers whole numbers 32 768 to 32767 Same as int Same as int Same as int 4 bytes long long integer double double precision float 8 bytes Floats up to 15 digits Similar to float larger s bool Boolean 1 byte One of 2 values True or false true false sometimes 8 Using unsigned before an integer stores only positive values including 0 doubles the value of positive integers you can store eg unsigned short can store 0 65535 6 of 21 1 Identifiers Naming Rules Identifiers Naming Conventions Stylistic rules what we care about Required rules ALWAYS Use meaningful names Identifiers can only have Variables Letters Numbers Underscore For 1 word Keep it lowercase For 2 or more words Begin with a lower case letter and only the first letter of each successive word will be capitalized must begin with a letter Can t have spaces Can t have special characters Constants All words in caps Use underscore to separate words eg TAX RATE Remember they are case sensitive PROGRAMMER Can t use KEYWORDS KEYWORD a word that has some sort of predefined meaning in the context of a programming language Topic 1 Review of CS1A 7 of 21 Declaring an Identifier Syntax char variableName Syntax type variableName Examples A simple character definition contains one single character value int sum The semi colon tells the compiler that you are finished with the statement char singleChar Can contain values such as a C x X Constants Syntax const type CONSTANT NAME value Examples Note the Single Quotes You must provide the value const int DAYS IN WEEK 7 const float SALES TAX RATE 0 075 Topic 1 Review of CS1A 9 of 21 If you have more than 1 character we need to store we use c strings Topic 1 Review of CS1A Strings are special Data Table C Strings An array of characters The last character is called a null terminator 0 Tells the compiler when the string is ended We need to specify how many characters we want Syntax char variableName size For Example char lastName 15 char lastName 3 Topic 1 Review of CS1A 8 of 21 Strings are special Variables float average char response Topic 1 Review of CS1A allocates space for 15 chars 14 the null terminator allocates space for 3 chars 2 the null terminator 11 of 21 10 of 21 Describes what 2 things with comments What the variable represents How the value is obtained int ageOne int ageTwo char answer char userName 20 float averageAge INPUT first age from user INPUT second age from user INPUT holds Y or N response from user INPUT name of program user CALC OUT avg of two input ages Note This is the only time you ll have comments in line with the code Topic 1 Review of CS1A 12 of 21 2 Assignment Statements Syntax variableName expression Assigns the value of the expression on the right to the variable on the left Example ageOne 15 ageTwo 23 averageAge ageOne ageTwo 2 0 answer y Topic 1 Review of CS1A 13 of 21 3
View Full Document
Unlocking...