introduction to programming using c week 1 lesson 2 pages 5 to 9 in ipc144 textbook n.
Skip this Video
Download Presentation

Loading in 2 Seconds...

play fullscreen
1 / 21

IPC144 - PowerPoint PPT Presentation

  • Uploaded on

Introduction to Programming Using C Week 1 – Lesson 2 (Pages 5 to 9 in IPC144 Textbook). IPC144. 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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'IPC144' - hansel

Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to programming using c week 1 lesson 2 pages 5 to 9 in ipc144 textbook
Introduction to Programming Using C

Week 1 – Lesson 2

(Pages 5 to 9 in IPC144 Textbook)

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

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.
c programming basics
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.
c program example
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.
c programming printf
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!”);
c programming printf1
C Programming - printf


  • 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!



  • From what we have learned so far, let’s try the first hello.c program
  • 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
  • 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

  • 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 .cegassignment2.c or hello.c

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

  • 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:


and you should see something like this on the screen:

Hello, my name is Fred

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

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
  • 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
integer data type
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. 2109 ( -231 to 231-1, or -2147483648to2147483647)
    • because the number, including its sign, is stored in 32 bits
  • 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: the declarationmust end with asemi-colon ;

Note: same data typevariables can be listed using commas

  • 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 variableeg. int length=3, width=5;

  • 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 “formatspecifier” 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) appearat end of printf

Format specifier for aninteger data-type wherevalue of area will be inserted

  • 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
  • TASK #1
    • 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!!