Title: C Programming COP 3223 lecture 1
1C ProgrammingCOP 3223lecture 1
2About Myself
- Graduate Student at the School of EECS
- Areas of Interest are Image Compression, Signal
Processing, Hardware and ASIC Design and FPGA
Architectures - Work Experience
- Graduate Research Assistant, School of EECS
- Intel Corporation( Summer/Fall intern)
- Fedex Services (Summer Intern)
3Pre-requisites
- It is better to have some programming experience
but I will assume no programming experience. - Some logic Design background is always helpful
4Todays Objectives
- To recognise the purpose and structure of the
course - To develop a good learning strategy
- To build your first program.
- To declare basic C types
- To develop simple programs to do calculations
- To use printf and scanf
5Course Objectives
- To introduce the C programming language
- To provide awareness of the role which C plays in
modern practical software development - To study in detail the application of C in the
integration and maintenance of practical projects
6Course Objectives
- On completion of this syllabus you should be able
to - develop software in C using sound software
engineering principles - understand the advantages and limitations of C
- demonstrate understanding of the role which C
plays in practical software development
7Assessment
- Assignments 40
- Lab Attendance and participation in Class
discussions 10 - Final Exam 25 ( Open Book)
- Surprise Quizzes 25 (Open Book)
8Class meetings
- Thursday 1730- 2000 COMM 101
9Books
- There are millions of books on C Programming. I
will name few that I like. - Robert Lafore,Turbo C Programming for the PC and
Turbo C, Revised Edition, Howard W Sams
Company - Herbert Schildt, C The Complete Reference, second
edition, McGrawHill - Kernighan, B W, Ritchie, D M The C Programming
Language (second edition), Prentice Hall, 1983
10Advice( Do nots)
- Memorise the code without an understanding of
underlying principles. - Get your friends to do the coursework for you.
- Aim just to get a passing grade
- Let the instructor proceed in tutorials when you
have confusions - Start the coursework the night before it is due.
11Caution
- The C we will use is standard C.
- Any standard C system will do, e.g. Turbo C,
Unix. - Experiment with what files you need to bring
home, if you plan to work at home. - Avoid machine-specific features
12Advice
- It is your responsibility to make sure that the
code you are submitting runs correctly on the
olympus machines. - You may use any development environment which you
are comfortable with.
13Using the environment
- For each program that you write create a project
- A project may have one or more files
- ".c" files contain source
- ".h" files we meet later
- ".txt" files are for documentation
- ".cpp" files For this course we will not use
them - For this course we will have only one ".c" file
14The Hello World Program
- Traditional first example program
- include ltstdio.hgt
- void main(void)
-
- printf(Hello, world\n)
-
15Output
The following will be the output of the Hello
World program Hello, world -
16The compilation process
- Preprocessing (anything beginning is resolved)
- Compiling - generates the object code for each
source file - Linking (binding) - links the separate source
files and the standard libraries to make an
executable file.
17Syntax errors
- You have broken the C "rules of grammar"
- The compiler may report several errors for one
mistake - Fix the syntax errors one at a time and recompile
- Don't despair!
- Warnings have to be fixed too.
18Link-time errors
- After compilation, there may be link errors
- Inconsistent function names
- No "main"
- More than one "main"
- The compiler being awkward (save and re-invoke
the studio)
19Run-time (semantic) errors
- The program does not do what you want
- This is nearly always your own error
- Look carefully through the code
- Else use the debugger (we meet this later)
20The compiler is picky
- Picks on any minor error
- Has no common sense
- Programming can be frustrating and yet very
rewarding
21Variables
- To perform calculations we need space to hold
data. This means declaring variables. - All variables must have a valid name
- All variables have a type
- Variables may be initialised
22Variable names
- Variable names are example of identifiers.They
have the following rules - Must begin with a letter or _
- Followed by zero or more letters, numerals or _
- No spaces allowed
- Case sensitive
- Not a reserve word e.g. int, if, while
- Have to be meaningful
23Are the following variable names valid?
- 123
- a123
- _fred
- num people
- num_employees
- num-staff
- Are they the same?
- VALUE
- Value
- value
- VaLuE
- _Value_
24Basic types
- Whole number types
- char
- short int or short
- int
- long int or long
- Floating point types
- float
- double
- long double
25Example declarations
- int counter1
- long num_records 0
- double length 3.7, width 2.7
- Separation with commas
- Initialisation is optional
- If uninitialised, the value could be anything
26Global and local variables
- A variable declared outside a function (e.g.
main) is global. - A variable declared inside a function is local.
- Global variables are like nuclear power
- Some think they should not be used at all.
- Others think that they may be used with great
caution.
27Rules of number
- Addition a2
- Subtraction a-2
- Multiplication a2
- Unary plus 3.7 same as 3.7
- Unary minus -33
28The problem with division
- What is 5 divided by 2?
- Answer A 2 and a remainder of 1
- This is called integer division
- Answer B 2.5
- This is called real division
- In C both are represented by /
29Rule for division
- If both whole numbers, we get integer division.
- To get remainder we use
- If either or both are floating point we get real
division - 5/2 is 2
- 52 is 1
- 5.0/2 or 5/2.0 or 5.0/2.0 is 2.5
30Output of data
- printf always has a format string with zero or
more places for variable to be written. - One extra argument per
- printf("There are d people here.\n",
num_people) - Types must match or else you will get garbage.
31 formats
- d for integer
- e for float in scientific notation
- f for float in decimals
- g chooses e or f format for you
- c for ASCII character
- le for a double in scientific notation
- .2f for float to 2 places of decimals
32Other symbols
- \ or for literal sign
- \" for literal "
- \n for new line
- \t for tab
- \\ for literal backslash
33Input of numbers
- We use a function scanf - part of a larger family
- We must precede each variable by
- We may scan in any amount of data
- scanf("d f", my_int, my_float)
- We use fflush to clear any unmatched input
- fflush(stdin)
34Assignment
- This is a valid C statement
- x 2
- What does it do?
- To copy or store the results of a calculation
- y x 2
- There can be multiple 's on one line
- a 5 b c 2
35Centigrade to Fahrenheit
- 1. Include stdio.h and declare main
- 2. Declare space for variables
- 3. Input the value in centigrade
- 4. Do the calculation
- 5 Output the answer.
36include ltstdio.hgt void main (void) float
cent, fahr printf("Enter temperature in
Centigrade\n") scanf("f",cent) fahr cent
9/5 32 printf("In Fahrenheit this is .2f
degrees\n",fahr)
37Short cut operators
- Why write a a b
- instead write a b
- Works too for -, , /,
- Why write a a 1 or a1
- instead write a or a
- Works too for a-- or --a
- In an expression a done first a done last
38Precedence
- Order in which arithmetic operations are
evaluated - (anything in brackets) highest
- unary ,-
- ,/,
- , -
- , , -, etc. lowest
39Unsigned qualifier
- unsigned means that the ve is doubled but the
-ve range is sacrificed - unsigned int population
- Int 32768 to 32767
- Unsigned int 0 - 65535
40Casting
- You can change one type to another thus
- int x
- float y
-
-
- x (int) y
- Note that this truncates and does not round
41Pitfalls
- There is no check for overflow or underflow -
you just get garbage - 3 ab is (3 a) b
- Uninitialised variables contain garbage
- Assigning a higher type to a lower one gives a
warning - use a cast - Input of garbage gives a garbage value, unless
you initialise
42Making code readable
- Keep one statement per line
- Add comments to answer questions
- Do not comment the obvious
- Add lots of spacing
- Indent code
- Try not to be too clever