1 / 22

IPC144

Introduction to Programming Using C Week 1 – Lesson 2 (Pages 5 to 9 in IPC144 Textbook). IPC144. Agenda. Basic C programming Elements (Simple Example): Reading the question / Planning the solution Coding the program Compiling & Debugging the program Running & Testing the program

cliffe
Download Presentation

IPC144

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Using C Week 1 – Lesson 2 (Pages 5 to 9 in IPC144 Textbook) IPC144

  2. Agenda • Basic C programming Elements (Simple Example): • Reading the question / Planning the solution • Coding the program • Compiling & Debugging the program • Running & Testing the program • Additional C programming Elements: • variables and data types • output: printf (output format specifiers)

  3. Programming Steps • Gather required information (inputs, processing, outputs)‏ • Plan out the program • Write-out (i.e. “code”) the program(In terms of C, this is called the source code)‏ • Compile the source code to create an object file (i.e. executable file). Debug and re-compile if errors. • Run to test if the executable file runs correctly!

  4. C Programming Mechanics • Every C program starts with a section titled main() • The title “main” indicates that it is the main part of the program an is executed first. • The section title main() is followed by a set of braces { and } used to contain the contents of the main program.

  5. C Programming Mechanics Here is an example of the C programming format: main(){ … contents of C program … }

  6. C Programming Mechanics Output • A statement that we will be using to tell the computer to display output is called printf • The “f” after print indicates that you can format the display of the output. • You can use various special characters in the printf statement to display a tab (\t) or newline (\n) • Refer to the IPC144 notes (page 9) for additional special characters

  7. C Programming Mechanics Output • The C programming language (like the English language) has rules (like structure and grammar). • Most C programming statements (like printf) ends with a semi-colon (just like sentences in the English language end with a period!) • Following the printf statement, text that you want to display is surrounded by round brackets, and are contained in double-quotes. For example: printf (“Print this text”);

  8. Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #1 • Using the handout, plan and then code your program

  9. Practice Note: • The printf statement is actually a tiny program that was created for other such as ourselves to use in our program. • To make certain that all you can compile your program using printf on any computer, you should include a special area (library) that contains those type of functions in your program. The name of the library containing input/output statements is called stdio.h • You do this by adding an “Include” directive at the beginning of your program prior to your main() section:eg. #include<stdio.h> No semicolon!

  10. Practice • OK, I wrote the answer on paper, but how do I actually put it on the Computer? • You need to connect to your computer account – Matrix is OK, but Phobos is required to be used for assignments! • Eg. In MS Windows:START->RUN->telnet matrix.senecac.on.ca

  11. Practice • In your account, you need to use a text-editor to create your program (referred to as source-code). • Only use a text-editor such as nled, pico, vi, nano, etc… since they only allow users to enter text only (not bold, formatting, etc). • All C programming source-code files should be saved with the file extension: lower-case ceg myprogram.c or helloworld.c

  12. Practice Compiling your C program • You need to transform your source code into a file that can be run efficiently by your computer. • This “transformed” file is known as a binary file, machine-language file, executable file, or object file. Basically they all mean the same thing…

  13. Practice • To compile your source code file, you issue the command:cc source_code_filename.c • Assuming there are no compile errors, this will create an executable file called a.out • Although you can “custom-name” your executable fileseg. cc source_code_filename.c –o executable_name You may find after a while, that you run out of space on your computer account. It is better to have your compile program overwrite previous versions of a.out program. Source-code files take up less space than executable files, and you can always re-compile the previous source-code programs if desired….

  14. Variables • It would be nice if your computer program could display other things than “hello world”. • Variables allow data to be stored in some space in the computer’s memory. • In order to have data stored in the computer’s memory, a storage space must first be created. This process is called defining a variable, and it is important not only to define the variable, but define the data-type of the variable (eg. number, character, character strings, etc…)

  15. Variables Rules for variable names: • Only use letters, digits, dashes or underscores(eg. customer_age, number1, number2, etc…) • Cannot begin with a number(eg. bad names: 2be, 1stcustomer, 4wheel) • Variable name should not exceed 31 characters

  16. Variables Suggestion: • Don’t be afraid to use multiple words to describe variable name. Instead of using underscores or dashes, you can use the rule of lowercase for first word, but capitalize first letter of following word(s) • For Example: • customerAge • promptUserGuess • generalSalesTaxRate

  17. Variables • We will look at just the integer data type to this lesson, but will look at other data types in the following lessons… • An integer data type is a whole number that may be positive or negative. The integer data type may have a limited range (-32768 to +32767), but we will learn other data types with larger ranges (stay tuned…)

  18. Variables Putting it all-together: • At the beginning of the main() program, you should first declare your variable(s) and state the variable(s) data type (int represents an integer data type and is known as a “declaration statement”)int firstNumber, secondNumber; Notice: variable declarationstatement (int) ends with asemi-colon ; Notice: same data-typevariables can be grouped Together (separated by comma).

  19. Variables Putting it all-together: • There are many ways to have a variable “hold data” in the variable such as assigning a value, or prompting the user to enter a value, even read data from a file! • Today, we will just learn how to assign an integer value to an integer data-type variable by using the equal signeg. int firstNumber, secondNumber; firstNumber = 3; secondNumber = 5; You can also assign thevalue of a variable whiledeclaring the variableeg. int firstNumber = 10;or int number1 = 3, number2 = 20;

  20. Variables Putting it all-together: • The printf statement allows you to not only display formatted text, but to display values or variables (also in a specified format). • If you want to display the variable’s value in the printf statement, you must use a “format specifier” that represents the variable’s data-type. You use the format specifier %d for int data-type variables.(More format specifiers discussed in future classes)… • For example:int number = 23;printf (“The value of variable called number is %d\n”, number); Format specifier (for aninteger data-type) wherevalue will be inserted Variable name(s)(in order) appearat end…

  21. Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #2 • Using the handout, plan and then code your program

  22. Homework • TASK #1 • Come to the lab, and learn how to create, compile, run, and printout a simple C program. This is due in one week from when assigned in the lab. • TASK #2 • Take the answers for questions #1 and #2 in the REALITY CHECK Handout, create a source-code file, then compile, run and verify your program! Not due, but for practice! • TASK #3 • Work on question #3 by yourself to plan, create, compile and run that program. Remember: Practice makes Perfect!!!!!

More Related