C programming cop 3223 lecture 1
Advertisement
This presentation is the property of its rightful owner.
1 / 42

C Programming COP 3223 lecture 1 PowerPoint PPT Presentation

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

Download Presentation

C Programming COP 3223 lecture 1

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


C programming cop 3223 lecture 1

C ProgrammingCOP 3223lecture 1

Raja Iqbal


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

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;


Assessment

Assessment

  • 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


Books

Books

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.


Caution

Caution

  • 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


Advice

Advice

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

    }


Output

Output

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


Variables

Variables

  • 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

123

a123

_fred

num people

num_employees

num-staff

Are they the same?

VALUE

Value

value

VaLuE

_Value_

Are the following variable names valid?


Basic types

Whole number types

char

short intorshort

int

long int or long

Floating point types

float

double

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

% formats

%d for integer

%efor float in scientific notation

%ffor float in decimals

%gchooses %e or %f format for you

%cfor ASCII character

%lefor a double in scientific notation

%.2ffor float to 2 places of decimals


Other symbols

Other symbols

\% or %%for literal % sign

\"for literal "

\nfor new line

\tfor 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

    fflush(stdin);


Assignment

Assignment

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


Lecture set no 1

#include <stdio.h>

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

}


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


Precedence

Precedence

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


Casting

Casting

  • You can change one type to another thus:

    int x;

    float y;

    :

    :

    x = (int) y;

    Note that this truncates and does not round


Pitfalls

Pitfalls

  • 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


  • Login