Overview of C By Dr Awad Khalil Computer Science Engineering Department C Language Elements Comments make a program easier to understand Used to signify a comment on a single line Text text use if comments on multi lines Don t embed comments within comments CSCI 106 Overview of C by Dr Awad Khalil 2 Compiler Directives include Compiler directive Processed at compilation time Instructs compiler on what you want in the program include iostream Adds library files to program Used with Also user defined CSCI 106 Overview of C by Dr Awad Khalil 3 Compiler Directives Stream data type Object that is a stream of characters Defined in iostream Entered on the keyboard cin Displayed on monitor cout CSCI 106 Overview of C by Dr Awad Khalil 4 Declarations Direct compiler on requirements Based on data needs data identifiers Each identifier needed must be declared Comma used to separate identifiers cin and cout are undeclared identifiers Special elements called streams cin input stream cout output stream Included with the iostream not declared CSCI 106 Overview of C by Dr Awad Khalil 5 Executable Statements cout get output cout Enter the fabric size in square meters cin get input cin sizeInSqmeters Assignment sizeInSqyards metersToYards izeInSqmeters CSCI 106 Overview of C by Dr Awad Khalil 6 Reserved Words and Identifiers Reserved words have special meanings Can NOT be used for other purposes const float and void are some examples Identifiers variables Used to store data by the program user defined Valid identifiers letter letter1 letter Invalid identifiers 1letter const hell o Special symbols C has rules for special symbols CSCI 106 Overview of C by Dr Awad Khalil 7 Upper and Lower Case C case sensitive Compiler differentiates upper lower case Identifiers can be either Be careful though cost Cost Blank spaces Use space to make program readable Use care in placing spaces CSCI 106 Overview of C by Dr Awad Khalil 8 User Defined Identifier An Identifier must always begin with a letter or underscore symbol not recommended An identifier must consist of letters digits or underscore only You cannot use a C reserved word as an identifier CSCI 106 Overview of C by Dr Awad Khalil 9 User Defined Identifier Invalid Identifiers 1Letter Float Const Two Four Joe s Two dimensional CSCI 106 Overview of C by Dr Awad Khalil 10 Data Types and Declarations Predefined data types int integers Positive or negative whole numbers 1000 12 199 100000 INT MAX largest int allowed by compiler float real numbers Positive or negative decimal numbers 10 5 1 2 100 02 99 88 bool boolean true false char Characters Represent characters CSCI 106 Overview of C by Dr Awad Khalil 11 Data Type int The basic integer type is int The size of an int depends on the machine and the compiler On pc s it is normally 16 or 32 bits Other integers types short typically uses less bits long typically uses more bits Different types allow programmers to use resources more efficiently Standard arithmetic and relational operations are available for these types CSCI 106 Overview of C by Dr Awad Khalil 12 Data Type float Floating point types represent real numbers Integer part Fractional part The number 108 1517 breaks down into the following parts 108 integer part 1517 fractional part C provides three floating point types float double long double CSCI 106 Overview of C by Dr Awad Khalil 13 Data Type char char characters Individual character value letter or number Character literal enclosed in single quotes A Character type char is related to the integer types Characters are encoded using a scheme where an integer represents a particular character CSCI 106 Overview of C by Dr Awad Khalil 14 Character Encoding Schemes ASCII is the dominant encoding scheme Examples encoded as 32 encoded as 43 A encoded as 65 Z encoded as 90 a encoded as 97 z encoded as 122 CSCI 106 Overview of C by Dr Awad Khalil 15 Character Encoding Schemes UNICODE CSCI 106 Overview of C by Dr Awad Khalil 16 string Class String object data type A literal string constant is a sequence of zero or more characters enclosed in double quotes Are you aware n Individual characters of string are stored in consecutive memory locations The null character 0 is appended to strings so that the compiler knows where in memory strings ends CSCI 106 Overview of C by Dr Awad Khalil 17 string Class String literal A 1234 Enter the distance Additional data types included in library include string Various operations on strings CSCI 106 Overview of C by Dr Awad Khalil 18 Declarations Identifiers should be Examples Short enough to be char response reasonable to type single int minelement word is norm float score Standard abbreviations are fine but only float standard abbreviations temperature Long enough to be int i understandable int n When using multiple char c word identifiers float x capitalize the first letter of each word CSCI 106 Overview of C by Dr Awad Khalil 19 Constant Declarations Types of constants integer float char bool string objects Associate meaningful terms const float PAYRATE 10 25 CSCI 106 Overview of C by Dr Awad Khalil 20 Hello cpp FILE Hello cpp DISPLAYS A USER S NAME include iostream include string using namespace std int main char letter1 letter2 string lastName Enter letters and print message cout Enter 2 initials and last name cin letter1 letter2 lastName cout Hello letter1 letter2 lastName cout We hope you enjoy studying C endl return 0 CSCI 106 Overview of C by Dr Awad Khalil 21 Executable Statements Memory status Before and after Assignments Form result expression sizeInSqyards metersToYards sizeInMeters sum sum item CSCI 106 Overview of C by Dr Awad Khalil 22 Arithmetic Operators Addition Subtraction Multiplication Division Modulus CSCI 106 Overview of C by Dr Awad Khalil 23 Input Output Operations Input include iostream library cin sizeInSqmeters Output include iostream library cout squareArea CSCI 106 Overview of C by Dr Awad Khalil 24 Program Input cin Form cin dataVariable cin age firstInitial Extracted from cin input stream Directs input to variable cin associated with keyboard input stdin Used with int float char bool and strings Leading blanks ignored floats int char bool and strings Char read 1 at a time 1 non blank Case issues int or float will read until space Stings same as int and float CSCI 106 Overview of C by Dr Awad Khalil 25 Program Output cout Form cout dataVariable cout squareArea Output stream cout Output operator insertion operator cout my height in inches is
View Full Document
Unlocking...