c programming cop 3223 lecture 1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
C Programming COP 3223 lecture 1 PowerPoint Presentation
Download Presentation
C Programming COP 3223 lecture 1

Loading in 2 Seconds...

play fullscreen
1 / 42

C Programming COP 3223 lecture 1 - PowerPoint PPT Presentation

  • Uploaded on

C Programming COP 3223 lecture 1. Raja Iqbal. About 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

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 'C Programming COP 3223 lecture 1' - albert

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
about myself
About 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)
pre requisites
  • It is better to have some programming experience but I will assume no programming experience.
  • Some logic Design background is always helpful
today s objectives
Today’s 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
course objectives
Course 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
course objectives1
Course 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;
  • Assignments 40%
  • Lab Attendance and participation in Class discussions 10%
  • Final Exam 25% ( Open Book)
  • Surprise Quizzes 25% (Open Book)
class meetings
Class meetings
  • Thursday 1730- 2000 COMM 101

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
advice do nots
Advice( 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.
  • 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
  • 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.
using the environment
Using 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
the hello world program
The Hello World Program
  • Traditional first example program

#include <stdio.h>

void main(void)


printf(“Hello, world\n”);



The following will be the output of the Hello World program

Hello, world


the compilation process
The 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.
syntax errors
Syntax 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.
link time errors
Link-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)
run time semantic errors
Run-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)
the compiler is picky
The compiler is picky
  • Picks on any minor error
  • Has no common sense
  • Programming can be frustrating and yet very rewarding
  • 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
variable names
Variable 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
are the following variable names valid



num people



Are they the same?






Are the following variable names valid?
basic types
Whole number types


short intorshort


long int or long

Floating point types



long double

Basic types
example declarations
Example 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;
global and local variables
Global 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.
rules of number
Rules of number
  • Addition a+2;
  • Subtraction a-2;
  • Multiplication a*2;
  • Unary plus +3.7 same as 3.7;
  • Unary minus -33;
the problem with division
The 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 /
rule for division
Rule 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
    • 5%2 is 1
    • 5.0/2 or 5/2.0 or 5.0/2.0 is 2.5
output of data
Output 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.
% 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

other symbols
Other symbols

\% or %% for literal % sign

\" for literal "

\n for new line

\t for tab

\\ for literal backslash

input of numbers
Input 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


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

centigrade to fahrenheit
Centigrade 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.


#include <stdio.h>

void main (void)


float cent, fahr;

printf("Enter temperature in Centigrade\n");


fahr = cent * 9/5 + 32;

printf("In Fahrenheit this is %.2f degrees\n",fahr);


short cut operators
Short cut operators
  • Why write a = a + b;

instead write a+= b;

  • Works too for -=, *=, /=, %=
  • Why write a = a + 1; or a+=1;

instead write a++ or ++a

  • Works too for a-- or --a
  • In an expression ++a done first a++ done last

Order in which arithmetic operations are evaluated

  • (anything in brackets) highest
  • unary +,-
  • *,/,%
  • +, -
  • =, +=, -=, *= etc. lowest
unsigned qualifier
Unsigned 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
  • You can change one type to another thus:

int x;

float y;



x = (int) y;

Note that this truncates and does not round

  • There is no check for overflow or underflow - you just get garbage
  • 3 * a+b 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
making code readable
Making 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