IPC144 Introduction to Programming Using C Week 1 – Lesson 2

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Agenda What is Computer Programming? The Programming Process
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 1.2 Introduction to C++ Programming
CSCE 206 Structured Programming in C
Whatcha doin'? Aims: To start using Python. To understand loops.
Chapter 6 JavaScript: Introduction to Scripting
Chapter 1: Introduction to computers and C++ Programming
Chapter 1.2 Introduction to C++ Programming
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to Scripting
Introduction to C Topics Compilation Using the gcc Compiler
Getting Started with C.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Intro to PHP & Variables
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Week 9 – Lesson 1 Arrays – Character Strings
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 3 – Introduction to C# Programming
Chapter # 2 Part 2 Programs And data
Introduction to C Topics Compilation Using the gcc Compiler
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Chapter 2 - Introduction to C Programming
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
JavaScript: Introduction to Scripting
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
Chapter 2 part #1 C++ Program Structure
Getting Started With Coding
Presentation transcript:

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

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.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 “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() { … contents of C program … }

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

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”);

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

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

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

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

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 firstNumber, secondNumber; firstNumber = 3; secondNumber = 5; You can also assign the value of a variable while declaring the variable eg. int firstNumber = 10; or int number1 = 3, number2 = 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 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 handout, Question #2 Using the handout, plan and then code your program

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