Unformatted text preview:

Intro to Programming Topic 9 Part 1 Chapter 4 Origins of C Ken Thompson Developed Unix Used Assembly and B to program Unix B Derived from BCPL Denis Ritchie 1970s Developed C to program Unix C was derived from B 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 2 Chapter 3 Programming Basics Origins of C 2 Bjarne Stroustrup 1980s Bell Labs developed C Based on C If you can understand C you can understand C Vise versa is not necessarily true C allows for object oriented programming OOP More modern style of programming Also has stronger type checking and standards Easier Easier Easier Easier Chapter 3 Programming Basics to to to to code reuse modify debug 3 1 Definitions Computer Programming The process of implementing algorithms using a language the computer can understand Computer Program An implementation of an algorithm A step by step set of instructions performed by the computer High level languages English or people friendly languages Compiler Interprets High level languages into machine language Topic 1 Introduction Review of CS1A 4 What is a Programming Language Programming Language Consists of a set of special words symbols and rules used to construct a program Syntax rules that dictate how valid instructions are written Semantics rules that dictate the meaning attached to the instructions Chapter 3 Programming Basics 5 Storing data in Memory 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 a descriptive name that maps to a location in the computers memory Variables are one type of identifier We have identifiers so we can Retrieve data Reuse data Modify data Topic 1 Introduction Review of CS1A 6 2 Identifiers 2 types of Identifiers Variables contains data values that may change during program execution Can be retrieved used Modified Constants contains data values that can t be changed during program execution The value must be declared Can be retrieved Can NOT be modified Topic 1 Introduction Review of CS1A 7 Identifiers Naming Rules Required rules Identifiers can only have Letters Numbers Underscore must begin with a letter Can t have spaces Can t have special characters Remember they are case sensitive payRate payrate Payrate PayRate Chapter 3 Programming Basics 8 Identifiers Naming Conventions 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 Constants All words in caps Use underscore to separate words eg TAX RATE PROGRAMMER Can t use KEYWORDS KEYWORD a word that has some sort of predefined meaning in the context of a programming language Chapter 3 Programming Basics 9 3 How to Declare Identifiers 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 You have to be specific about what you are storing How much memory is needed to store your data 10 Chapter 3 Programming Basics Syntax Description Size Data Values Examples int 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 Same as int 11 Chapter 3 Programming Basics Declaring an Identifier 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 Syntax const type CONSTANT NAME value Examples const int DAYS IN WEEK 7 Visual Studio const float SALES TAX RATE Section 0 075 Help System Chapter 3 Programming Basics Articles Fundamental int integer introductory Types Data Type Ranges Fundamental char character introductory Types Data Type Ranges Fundamental floating point Types Type float introductory float Data number Further Information 12 discussion example discussion example discussion example 4 Strings are special If you have more than 1 character we need to store we use c strings C Strings An array of characters The last character is called a null terminator 0 Tells the compiler where the end of the string is We need to specify how many characters we want Syntax char variableName size 13 Chapter 3 Programming Basics C String Examples char lastName 15 can hold 15 characters 14 the null terminator char lastName 3 can hold 3 characters 2 the null terminator Why is this a bad idea 14 Chapter 3 Programming Basics Understanding a C program No not a C statement include iostream using namespace std preprocessor directives for I O Tells the compiler to use predefined Standard C functions variables classes Don t put void Write it just like this int main Remember this ends a program statement char name 30 int age 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 All C programs must start with this It tells the compiler where to start Declares a c string variable Declares an integer variable prompts for an input Reads the input into name prompts for the 2nd 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 Chapter 3 Programming Basics 5 Breaking it down include iostream Pre processor Directive 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 using namespace std 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 Don t worry too much about the details here yet just be sure to include them Chapter 3 Programming Basics 16 Main 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 The book has an error MAIN CANNOT BE VOID Must


View Full Document

Saddleback CS 1A - Intro to Programming C

Loading Unlocking...
Login

Join to view Intro to Programming C 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 Intro to Programming C 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?