IPC 144 Introduction to Programming Using C Week

  • Slides: 22
Download presentation
IPC 144 Introduction to Programming Using C Week 1 – Lesson 2 (Pages 5

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

Agenda Basic C programming Elements (Simple Example): Reading the question / Planning the solution

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)

Programming Steps Gather required information (inputs, processing, outputs) Plan out the program Write-out (i.

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!

C Programming Mechanics Every C program starts with a section titled main() The title

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.

C Programming Mechanics Here is an example of the C programming format: main() {

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

C Programming Mechanics Output A statement that we will be using to tell the

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 IPC 144 notes (page 9) for additional special characters

C Programming Mechanics Output The C programming language (like the English language) has rules

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 doublequotes. For example: printf (“Print this text”);

Practice From what we have learned as of now, let’s try the REALITY CHECK

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

Practice Note: The printf statement is actually a tiny program that was created for

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!

Practice OK, I wrote the answer on paper, but how do I actually put

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

Practice In your account, you need to use a text-editor to create your program

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 c eg myprogram. c or helloworld. c

Practice Compiling your C program You need to transform your source code into a

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…

Practice To compile your source code file, you issue the command: cc source_code_filename. c

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 files eg. 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….

Variables It would be nice if your computer program could display other things than

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 strings, etc…)

Variables Rules for variable names: Only use letters, digits, dashes or underscores (eg. customer_age,

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

Variables Suggestion: Don’t be afraid to use multiple words to describe variable name. Instead

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: customer. Age prompt. User. Guess general. Sales. Tax. Rate

Variables We will look at just the integer data type to this lesson, but

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…)

Variables Putting it all-together: At the beginning of the main() program, you should first

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 first. Number, second. Number; Notice: same data-type variables can be grouped Together (separated by comma). Notice: variable declaration statement (int) ends with a semi-colon ;

Variables Putting it all-together: There are many ways to have a variable “hold data”

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 sign eg. int first. Number, second. Number; first. Number = 3; second. Number = 5; You can also assign the value of a variable while declaring the variable eg. int first. Number = 10; or int number 1 = 3, number 2 = 20;

Variables Putting it all-together: The printf statement allows you to not only display formatted

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 datatype. 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 %dn”, number); Format specifier (for an integer data-type) where value will be inserted Variable name(s) (in order) appear at end…

Practice From what we have learned as of now, let’s try the REALITY CHECK

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

Homework TASK #1 TASK #2 Come to the lab, and learn how to create,

Homework TASK #1 TASK #2 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. 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!!!!!