Docsity

ECE 264 Advanced C Programming, Lecture notes of Programming Languages

Toggle sidebar
bg1
ECE 264 Advanced C Programming
2009/01/14
Contents
1 What is computer programming? Characteristics of programs? 2
2 What is C? 2
3 ECE264 3
4 Type System 3
5 Course Logistics 4
6 Instructor and Teaching Assistant 4
7 Grading 5
8 Exercises and Assignments 5
9 Blackboard 5
10 Guest Speaker on Career in Software Development 5
11 Additional Projects outside ECE 264 5
12 First Example 6
13 Compile and Execute C Program 7
14 Second Example 8
ECE264 Purdue University 1Yung-Hsiang Lu
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download ECE 264 Advanced C Programming and more Lecture notes Programming Languages in PDF only on Docsity!

ECE 264 Advanced C Programming

  • 2009/01/
  • 1 What is computer programming? Characteristics of programs? Contents
  • 2 What is C?
  • 3 ECE264
  • 4 Type System
  • 5 Course Logistics
  • 6 Instructor and Teaching Assistant
  • 7 Grading
  • 8 Exercises and Assignments
  • 9 Blackboard
  • 10 Guest Speaker on Career in Software Development
  • 11 Additional Projects outside ECE
  • 12 First Example
  • 13 Compile and Execute C Program
  • 14 Second Example

1 What is computer programming? Characteristics of pro-

grams?

Computer programs are everywhere in our daily lives. Think of Google, Facebook, Ama- zon... cellular phone, text messaging ... What are typical functionalities of a program?

  • Iterations: When you search a book, the database goes through, i.e. iterates , many books to check whether any book matches your keyword (or keywords).
  • Comparison / Condition: This “checking” compares your keyword and the key- words associated with the books.
  • Collection of Information: The list of books matching your search is created and displayed.

The program also has to handle networking and formatting an HTML page that is shown on your browser. In fact, there are probably several programs, one for receiving your request, one for searching the database, and one for creating and formatting the output.

2 What is C?

C was developed in early 1970s in Bell Lab for programming the UNIX operating system. At that time, OS was written primarily in Assembly. From the beginning, high perfor- mance is a major goal in C. C is one of the mostly widely used programming languages now. You can find C in high performance supercomputers as well as tiny embedded sys- tems. In fact, there are many variations of C. Some applications extend the features of C and some others restrict C. C is different from some other types of languages, especially script languages. In C, you need to declare a variable before using it. This is different from MATLAB and script programming. You have learned MATLAB in CS 159 and you will learn script programming in ECE 364.

A brief history of programming languages is available at

http://oreilly.com/news/graphics/prog lang poster.pdf.

You need three steps to execute a C program: compile , link , and execute. A compiler trans- lates a C source program (a text file we can read) to an object file (.o). Linker takes several object files and creates an executable. Finally, the program is executed. C checks types at compilation to prevent run-time type errors. We will learn more about this concept later.

Assume that you will write and rewrite (and rewrite and rewrite) the same program many times (because you will ). Do no assume that your program is perfect after you finish typing. Assume you are going to change the program many times. Make your program clear and easy to read (variable names, indentation ...)

5 Course Logistics

Course Web Page:

https://engineering.purdue.edu/OOSD/S2009/S2009.html.

Introduction of the instructor and the teaching assistant.

Lectures notes will be posted on-line 24 hours before the lectures. Print the note if you want a hard copy.

6 Instructor and Teaching Assistant

Instructor’s Office Hours:

  • Monday, 130-230PM, MSEE 190
  • Tuesday, 5-6PM, MSEE 190
  • Wednesday, 230-330PM, MSEE 190
  • Friday, 230-330PM, MSEE 222 (not MSEE 190)

TA’s Office Hours:

  • Wednesday, 4-5, MSEE 190

Additional hours will be added later. The time may change based on the schedules of most students.

7 Grading

How to become a good programmer? Read and write many programs.

8 Exercises and Assignments

9 Blackboard

Email, Discussion , Submission, Scores ...

http://www.itap.purdue.edu/tlt/blackboard/.

You are strongly encouraged to interact with the instructor, the teaching assistant, and your classmates. To make communication efficient, please save time by avoiding redun-

dancy. Do not send course-related questions by email. The instructor will not answer

these questions by email. Post your questions in Blackboard-Discussion. Please check Blackboard before asking any questions. Sometimes your questions have been asked and answered. Exams and assignment due dates are already posted (therefore, don’t ask). Use email for personal issues only (such as making an appointment or arranging an ear- lier exam).

Please check Blackboard regularly for announcements.

10 Guest Speaker on Career in Software Development

being arranged ... likely March or April

11 Additional Projects outside ECE 264

Game development. Programming PDAs. Algorithm Design. ...

convention. For example, the input arguments of main are called argc and argv. Can you change them? Yes. You can call them dog and bird and the program does exactly the same thing. C has a collection of words that are reserved for special meanings. These are called keywords (page 77 in ABOC). As long as you do not use the keywords, the com- piler does not care whether you call it argc or dog. However, you have to be careful when you violate the convention. Your program will be more difficult to read, possibly by yourself later. Rules and convention can be good. Consider this example

Everyone knows exactly what you mean when you put a note at a particular location. These are the rules. The rules make communication between musicians easier. Com- posers also follow certain convention when they write music. For example, a symphony usually has four movements. Do these rules restrict the creativity of musicians? I don’t think so. Can you say Mozart not creative? When you write a program, you have to fol- low certain rules and convention. Your creativity is built upon these rules and convention so that everyone knows exactly what you mean.

13 Compile and Execute C Program

How do we compile, link, and execute the program? Let’s call the program ex1.c.

gcc -Wall ex1.c -o ex

This program has only one file so we can compile and link in one step using gcc. It is a C compiler available for many types of computers. It is open-source and free of charge. The flag -Wall means to turn on all warning messages. You should use this because warnings usually indicate errors. The file name ex1.c is provided and the output is ex1. If you do not give -o and the output name, the default output is a.out. To execute this program, simple type

./ex

here ./ means the current directory (also called folder ). This is important because it is pos- sible to have another program called ex1 somewhere else on your computer. You want to execute the program at this directory. A common error among beginning programmers is to call their program as “test”. It turns out “test” is also a program provided by UNIX. If you do not use ./test, you will call the wrong one (usually in /usr/bin/test).

If we execute the program by typing

./ex1 ece 264 spring 2009

the output is

./ex ece 264 spring 2009

The program prints the arguments one-by-one, as promised, starting from the program’s name ./ex1.

14 Second Example

This example shows how to create a function and how to call it.

/* second.c */ #include <stdio.h> #include <stdlib.h> int add(int a, int b) { return (a + b); }

int main(int argc, char (^) * argv[]) { int val1; int val2; if (argc < 3) {