Origins of C Ken Thompson Developed Unix Used Assembly and B to program Unix B Derived from BCPL Intro to Programming Denis Ritchie 1970s Developed C to program Unix C was derived from B Topic 9 Part 1 C was not as strictly typed as other languages Allowed it to be more flexible Easier to read and write than Assembly But more error prone lacked automated checks Chapter 4 Chapter 3 Programming Basics Origins of C 2 Definitions Bjarne Stroustrup 1980s Bell Labs developed C 2 Computer Programming The process of implementing algorithms using a language the computer can understand Based on C If you can understand C you can understand C Computer Program An implementation of an algorithm A step by step set of instructions performed by the computer Vise versa is not necessarily true C allows for object oriented programming OOP More modern style of programming High level languages English or people friendly languages Also has stronger type checking and standards Easier Easier Easier Easier to to to to code reuse modify debug Chapter 3 Programming Basics Compiler Interprets High level languages into machine language 3 What is a Programming Language Topic 1 Introduction Review of CS1A 4 Storing data in Memory Programming Language Consists of a set of special words symbols and rules used to construct a program 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 Syntax rules that dictate how valid instructions are written a descriptive name that maps to a location in the computers memory Variables are one type of identifier Semantics rules that dictate the meaning attached to the instructions Chapter 3 Programming Basics We have identifiers so we can Retrieve data Reuse data Modify data 5 Topic 1 Introduction Review of CS1A 6 1 Identifiers Identifiers Naming Rules 2 types of Identifiers Variables contains data values that may change during program execution Identifiers can only have Letters Numbers Underscore Can be retrieved used Modified Required rules must begin with a letter Can t have spaces Can t have special characters Constants contains data values that can t be changed during program execution The value must be declared Can be retrieved Can NOT be modified Remember they are case sensitive payRate payrate Payrate PayRate 7 Topic 1 Introduction Review of CS1A Identifiers Naming Conventions How to Declare Identifiers Stylistic rules what we care about ALWAYS Use meaningful names Variables 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 We need to tell the compiler what identifiers to use The compiler needs to know The data type or type How the contents of memory need to be viewed In other words what kind of information will be stored C is strongly typed Constants You have to be specific about what you are storing All words in caps Use underscore to separate words eg 8 Chapter 3 Programming Basics How much memory is needed to store your data TAX RATE PROGRAMMER Can t use KEYWORDS KEYWORD a word that has some sort of predefined meaning in the context of a programming language 9 Chapter 3 Programming Basics Syntax Description Size Data Values integer 4 bytes Pos or neg integers whole numbers low 9 digits 3 4 235 1215232 23 432 char character 1 byte Characters enclosed in quotes a z d f float floating point number 4 bytes Pos or neg decimal numbers including fractional part up to 7 digits 32 234 23 32 0 0 1 25 123 2353 long long integer 4 bytes 8 on some Same as int systems double double 8 bytes precision float Floats up to 15 digits Same as float but larger s bool Boolean One of 2 values True or false True False 1 byte Chapter 3 Programming Basics Declaring an Identifier Examples int 10 Chapter 3 Programming Basics Variables Syntax type variableName Examples int sum float average char response The semi colon tells the compiler that you are finished with the statement Constants Same as int Syntax const type CONSTANT NAME value Examples const int DAYS IN WEEK 7 const float SALES TAX RATE 0 075 11 Chapter 3 Programming Basics 12 2 Strings are special C String Examples If you have more than 1 character we need to store we use c strings char lastName 15 can hold 15 characters 14 the null terminator C Strings An array of characters The last character is called a null terminator 0 char lastName 3 can hold 3 characters 2 the null terminator Tells the compiler where the end of the string is We need to specify how many characters we want Why is this a bad idea Syntax char variableName size 13 Chapter 3 Programming Basics Understanding a C program No not a C statement include iostream using namespace std preprocessor directives for I O Breaking it down include iostream Tells the compiler to use predefined Standard C functions variables classes Pre processor Directive Don t put void Write it just like this int main Remember this ends a program statement char name 30 int age Tells the pre processor that we want to use i o functions so we include them in our code We need iostream to use cin cout statements All C programs must start with this It tells the compiler where to start cout Enter your name These cin name define a code block cout How old are you You must start with cin age and Note the indent end with 14 Chapter 3 Programming Basics Declares a c string variable Declares an integer variable using namespace std prompts for an input Tells the compiler that we want to use all the standard C functions Functions are small code segments that we use to build our program Reads the input into name 2nd prompts for the input Reads the input into age 4 cout endl name is cout age years old endl Tells the OS that the program terminated properly return 0 15 Don t worry too much about the details here yet just be sure to include them Chapter 3 Programming Basics 16 Chapter 3 Programming Basics Main Declaring Identifiers int main body of function i e program statements return 0 Program execution begins with this function All C programs must have this function MUST BE an int char name 30 int age Datatype or Type The book has an error MAIN CANNOT BE VOID Must start with and end with Must have return 0 as last statement This returns the value 0 to the system so it knows the program completed properly End of C statement Variable Name These are variable declarations Remember we must reserve memory locations to store data
View Full Document
Unlocking...