Download presentation
Presentation is loading. Please wait.
Published byNigel Campbell Modified over 9 years ago
1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
(Pages 5 to 9 in IPC144 Textbook)
2
Agenda Basic Programming Steps (Simple Example):
Read and understand the question Develop an algorithm Code the program in C Compile the program Run, Test and Debug the program C programming Elements: main() function variables and data types output: printf (output format specifiers)
3
Programming Steps Gather required information (inputs, processing, outputs) Determine the algorithm – sketch out, in English, how a human can solve the problem Write (ie “code”) the program (called the source code) Compile the source code. Fix any syntax errors and recompile to create an executable file. Run executable file with test input(s) and check for correct output(s). If necessary, debug by finding logical errors (bugs) and correct source code.
4
C Programming Basics Every C program must contain a function called main() The name “main” indicates that it is the main part of the program and is executed first. main() is followed by the set of braces { and } which contains the contents of the main function.
5
C Program Example Here is the format of a basic C program:
#include <stdio.h> main() { … statements of C program … } Most C statements end with a ; just as English sentences end with a period.
6
C Programming - printf A statement used to display formatted output on the screen is called printf (the “f” indicates that you can format the the output). You can use special characters in a printf statement to format output such as a tab (\t) or newline (\n). Refer to the IPC144 notes (page 9) for additional special characters, eg: \” \\ Text that you want to display is surrounded by parentheses (round brackets) and contained in double-quotes. For example: printf (“Hello!”);
7
C Programming - printf Note: The printf statement is actually a function that was created to make it easy for programmers to display output. To make certain that you can compile your program using printf on any computer, you should include a library that contains standard input and output function information called stdio.h This is done by adding a directive at the beginning of the program before main() as follows: #include <stdio.h> Note: No semicolon!
8
Practice From what we have learned so far, let’s try the first hello.c program
9
Practice How can you actually put your program into the computer?
You need to connect to Seneca’s matrix computer First, read “Getting Started” on the IPC144 website Eg. In MS Windows: START -> RUN -> telnet matrix.senecac.on.ca After entering your userid and password a Unix prompt will be displayed. You then enter Unix commands Eg: >who which displays all users currently using matrix
10
Practice First you need to use a text editor to create your program (referred to as source code). Use a text editor such as nled, pico, vi, nano, etc… to enter plain text (no bold, formatting, etc). Eg: nled hello.c All C programming source code files should be saved with the file extension: lower-case .c eg assignment2.c or hello.c
11
Practice Compiling a C program:
The C program must be translated into an executable file, also known as a binary file, machine-language file, or object file. To compile your source code file named hello.c type in the cc command (to mean compile a C program): >cc hello.c If there are syntax errors then details including as line number will be displayed If there are no syntax errors, then an executable file called a.out is created To run the program enter a.out at the prompt Eg: >a.out
12
Practice You can also provide a name for the executable file by using the –o option with the cc command: eg: cc hello.c –o hello The executable file is now contained in the file named hello, so to run it enter: hello and you should see something like this on the screen: Hello, my name is Fred
13
Variables It would be nice if a computer program could do something more interesting than display your name! Variables allow data to be stored in some location in the computer’s memory. To have data stored in the computer’s memory, a storage area must first be created. This is called declaring a variable. It is important to indicate the variable’s name and also its data type (eg: integer, decimal, character, etc…)
14
Variables Rules for variable names:
Only use letters, digits, or underscores (eg: customer_age, number1, gstRate, etc…) Cannot begin with a digit (eg, invalid names: 2be, 1stcustomer, 4wheel) Cannot use keywords such as int, for, if because they have special meanings
15
Variables Use descriptive names for variables. You can combine multiple words to form a variable name. You could use underscores to separate the words but the convention is to use lowercase for the first word and then capitalize the first letter of each of the following word(s) For example: customerAge promptUserGuess generalSalesTaxRate
16
Integer Data Type First we will study the integer data type in this lesson, and will study other data types in later lessons. An integer data type is a whole number that may be positive or negative. On the matrix computer the integer data type has a limited range: approx. 2109 ( -231 to 231-1, or to ) because the number, including its sign, is stored in 32 bits
17
Variables At the beginning of the main() program, you should first declare your variables and indicate their data type. int is used to represent an integer data type. Here is a declaration of two integer variables int length, width; Note: same data type variables can be listed using commas Note: the declaration must end with a semi-colon ;
18
Variables 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! A value can be assigned to a variable by using the assignment operator which is represented by the equals sign (however don’t say “equals” when you see the “=“ sign – say “is assigned” instead!) eg. int length, width; length = 3; width = 5; Note: The value of a variable can can also be assigned (“initialized”) when declaring the variable eg. int length=3, width=5;
19
Variables The printf statement is used to display values of 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. For example: int area = 23; printf(“Value of area is %d\n”, area); Variable name(s) (in order) appear at end of printf Format specifier for an integer data-type where value of area will be inserted
20
Practice From what we have learned so far, you should be able to start understanding Program #2, rectangle.c, which calculates the area and perimeter of a rectangle
21
Practice!! TASK #1 TASK #2 TASK #3
Attend the lab, and learn how to login to matrix then create, compile, run, and print out a simple C program. TASK #2 Take the answers for questions #1 and #2 in the Exercise 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 is the key to success!!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.