March R. Smith - University of St Thomas - Minnesota Today’s Class Recap of a simple programRecap of a simple program A “history” of CA “history” of C The nuts and bolts of compiling programsThe nuts and bolts of compiling programs Overview of Statements and VariablesOverview of Statements and Variables Simple typesSimple types Printing out textPrinting out text
March R. Smith - University of St Thomas - Minnesota C Program Always in a fileAlways in a file Always defines a ‘function’ to performAlways defines a ‘function’ to perform Usually the function is named ‘main’Usually the function is named ‘main’ We “compile” it to make it workWe “compile” it to make it work
March R. Smith - University of St Thomas - Minnesota History of C Interpreting expressions (1940s)Interpreting expressions (1940s) Sort/Merge GeneratorSort/Merge Generator A2 CompilerA2 Compiler FortranFortran AlgolAlgol C
March R. Smith - University of St Thomas - Minnesota “Compiling” - the mechanics “Command Line” compiling“Command Line” compiling Should work the same on PCs and MacsShould work the same on PCs and Macs You have to “compile” programs to run themYou have to “compile” programs to run them $ gcc hello.c $ You simply command the computer to run the programYou simply command the computer to run the program $ hello Hello, world! $
March R. Smith - University of St Thomas - Minnesota Compiling and Running What is really happening?What is really happening? How a program executesHow a program executes Computer ‘Languages’Computer ‘Languages’ InterpetingInterpeting CompilingCompiling Running “real” instructionsRunning “real” instructions What it looks like in RAMWhat it looks like in RAM
March R. Smith - University of St Thomas - Minnesota Sample C Program /* R. Smith First program assignment, QMCS 130 January 29, 2008 */ #include #include int main (void) { printf("Hello, world\n"); return 0; return 0;}
March R. Smith - University of St Thomas - Minnesota Observations Case sensitive: match cases “exactly”Case sensitive: match cases “exactly” Program is worked “in order” from start to endProgram is worked “in order” from start to end –“Sequences” are fundamental to computing Curly braces mark “nested” componentsCurly braces mark “nested” components –“Hierarchies” are fundamental to computing Learn the “patterns” of typical programming tasksLearn the “patterns” of typical programming tasks –Setting up the program –Doing output
March R. Smith - University of St Thomas - Minnesota Statements and Variables Statements we’ll useStatements we’ll use –“Function declarations” like main Always followed by a pair of parentheses,Always followed by a pair of parentheses, and then curly bracesand then curly braces –Other statements are followed by a “;” –Variable declarations –Assignment statements VariablesVariables –Storage cells in your program –Each one is like a spreadsheet cell, only dumber
March R. Smith - University of St Thomas - Minnesota Variable Naming Naming StructureNaming Structure First character: A-Z, a-z, _ or $First character: A-Z, a-z, _ or $ Remaining: same plus digitsRemaining: same plus digits Style rulesStyle rules Descriptive namesDescriptive names Usually start with lowercase letterUsually start with lowercase letter Camelback: use capitals to mark wordsCamelback: use capitals to mark words itemsOrdered, totalPaymentitemsOrdered, totalPayment DO NOT USE $ to start a nameDO NOT USE $ to start a name
March R. Smith - University of St Thomas - Minnesota Syntax is King A&CA&C
March R. Smith - University of St Thomas - Minnesota Assignment Statements “Variable substitution”“Variable substitution” where we fill one thing in with anotherwhere we fill one thing in with another
March R. Smith - University of St Thomas - Minnesota “Types” of variables First use: you say the typeFirst use: you say the type int = “integer”int = “integer” signed, no decimal part, less than 2Gsigned, no decimal part, less than 2G ExamplesExamples int sampleInt = 12; sampleInt = 25;
March R. Smith - University of St Thomas - Minnesota Printing The ‘printf()’ functionThe ‘printf()’ function –Syntax of ‘functions’ –Takes 1 or more ‘arguments’ or ‘parameters’ First argument: “format string”First argument: “format string” –A string of characters to print –Embedded ‘signals’ to say what else to print (“%” character) –Each ‘signal’ corresponds to a subsequent parameter Extra parametersExtra parameters –One per format signal –%i – integers –%c – individual characters –%s – strings of characters
March R. Smith - University of St Thomas - Minnesota Sample 1 – string declarations char *myname = “Rick”;
March R. Smith - University of St Thomas - Minnesota Sample 2 Dear John Smith, We are pleased, John, that you have agreed to represent the Smith family in the upcoming event. All members of the Smith family will be thrilled with the results. Your name, John, will be remembered for generations by other members of the Smith family. Sincerely, The Company.
March R. Smith - University of St Thomas - Minnesota Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.