Docsity

C Programming Lecture 12: Understanding Variables and File I/O in C, Slides of Electrical Engineering

Toggle sidebar
bg1
C-Programming, Part 1 02/26/2008
Lecture 12 1
Cโ€Programming๎˜ƒ
Part๎˜ƒI:๎˜ƒbasics๎˜ƒ
prep.๎˜ƒfor๎˜ƒLab๎˜ƒ8๎˜ƒ
Why๎˜ƒC?๎˜ƒ
โ€ขSee๎˜ƒhttp://www.tiobe.com/tpci.htm๎˜ƒ
02/07
rank 02/06
rank movement Language share ๏„ in last
year
1 1 = Java 18.978% -3.45%
2 2 = C 16.104% -2.23%
3 3 = C++ 10.768% -0.53%
4 5 ๏‚ญ PHP 8.847% -0.07%
5 4 ๏‚ฏ (Visual) Basic 8.369% -1.03%
6 6 = Perl 6.073% -0.63%
7 8 ๏‚ญ Python 3.566% +0.90%
8 7 ๏‚ฏ C# 3.189% -0.78%
9 10 ๏‚ญ JavaScript 2.982% +1.47%
10 20 ๏‚ญ 10 Ruby 2.528% +2.12%
C๎˜ƒHow๎˜ƒit๎˜ƒStacks๎˜ƒUp๎˜ƒ
โ€ขAs๎˜ƒU๎˜ƒcan๎˜ƒC,๎˜ƒthe๎˜ƒC๎˜ƒlanguage๎˜ƒ(and๎˜ƒits๎˜ƒextensions/derivatives)๎˜ƒ
dominates๎˜ƒthe๎˜ƒsoftware๎˜ƒcommunity๎˜ƒ๎˜ƒ
โ€“Java๎˜ƒalso๎˜ƒa๎˜ƒstrong๎˜ƒshowing๎˜ƒ
โ€“Python๎˜ƒworth๎˜ƒa๎˜ƒpeek๎˜ƒ
โ€ขAdvantages๎˜ƒof๎˜ƒC:๎˜ƒ
โ€“compiled๎˜ƒcode๎˜ƒruns๎˜ƒFAST ๎˜ƒ
โ€“allows๎˜ƒlowโ€level๎˜ƒdevice๎˜ƒcontrol๎˜ƒ
โ€“a๎˜ƒfoundation๎˜ƒof๎˜ƒthe๎˜ƒprogramming๎˜ƒworld๎˜ƒ
โ€ขDisadvantages๎˜ƒof๎˜ƒC:๎˜ƒ
โ€“strings๎˜ƒare๎˜ƒa๎˜ƒpain๎˜ƒin๎˜ƒthe๎˜ƒ@$$๎˜ƒ
โ€“awkward๎˜ƒconventions๎˜ƒ(pointers๎˜ƒcan๎˜ƒbe๎˜ƒdifficult๎˜ƒto๎˜ƒlearn)๎˜ƒ
โ€“requires๎˜ƒa๎˜ƒcompiler๎˜ƒ
What๎˜ƒwe๎˜ƒwill๎˜ƒand๎˜ƒwonโ€™t๎˜ƒdo๎˜ƒ
โ€ขWe๎˜ƒwill๎˜ƒlearn:๎˜ƒ
โ€“to๎˜ƒwrite๎˜ƒsimple๎˜ƒprograms๎˜ƒ
โ€“basic๎˜ƒinterface๎˜ƒ
โ€“control๎˜ƒflow,๎˜ƒmath,๎˜ƒprinting๎˜ƒ
โ€“data๎˜ƒtypes๎˜ƒ
โ€“enough๎˜ƒto๎˜ƒbe๎˜ƒdangerous๎˜ƒ
โ€ขWe๎˜ƒwonโ€™t๎˜ƒlearn:๎˜ƒ
โ€“advanced๎˜ƒpointer๎˜ƒoperations๎˜ƒ
โ€“large๎˜ƒprojects๎˜ƒ(linking๎˜ƒseparate๎˜ƒprograms)๎˜ƒ
โ€“distinctions๎˜ƒbetween๎˜ƒpublic,๎˜ƒprivate,๎˜ƒexternal๎˜ƒvariable s๎˜ƒ
โ€“enough๎˜ƒto๎˜ƒbe๎˜ƒreally๎˜ƒdangerous๎˜ƒ
๎˜ƒ
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download C Programming Lecture 12: Understanding Variables and File I/O in C and more Slides Electrical Engineering in PDF only on Docsity!

C

โ€Programming

Part

I:

basics

prep.

for

Lab

Why

C?

-^

See

http://www.tiobe.com/tpci.htm

02/07rank

02/06rank

movement

Language

share

๏„^ in lastyear

=^

Java

=^

C^

=^

C++

๏‚ญ^

PHP

๏‚ฏ^

(Visual) Basic

=^

Perl

๏‚ญ^

Python

๏‚ฏ^

C#

๏‚ญ^

JavaScript

๏‚ญ^10

Ruby

C

How

it

Stacks

Up

โ€ข^

As

U

can

C,

the

C

language

(and

its

extensions/derivatives)

dominates

the

software

community

-^

Java

also

a^

strong

showing

-^

Python

worth

a^

peek

โ€ข^

Advantages

of

C:

-^

compiled

code

runs

FAST

-^

allows

low

โ€level

device

control

-^

a^ foundation

of

the

programming

world

โ€ข^

Disadvantages

of

C:

-^

strings

are

a^ pain

in

the

@$$

-^

awkward

conventions

(pointers

can

be

difficult

to

learn)

-^

requires

a^ compiler

What

we

will

and

wonโ€™t

do

โ€ข^

We

will

learn:

โ€“^

to

write

simple

programs

โ€“^

basic

interface

โ€“^

control

flow,

math,

printing

โ€“^

data

types

โ€“^

enough

to

be

dangerous

โ€ข^

We

wonโ€™t

learn:

โ€“^

advanced

pointer

operations

โ€“^

large

projects

(linking

separate

programs)

โ€“^

distinctions

between

public,

private,

external

variables

โ€“^

enough

to

be

really

dangerous

Docsity.com

C

File

Types

โ€ข^

Source

Code

โ€“^

the

stuff

you

type

in:

has

.c

extension

โ€ข^

Compiled

โ€œExecutableโ€

โ€“^

the

ready

โ€to

โ€run

product:

usually

no

extension

in

Unix,

.exe

in

DOS

โ€ข^

Header

Files

โ€“^

contain

definitions

of

useful

functions,

constants:

.h

extension

โ€ข^

Object

Files

โ€“^

a^ pre

โ€linked

compiled

tidbit:

.o

in

Unix,

.obj

in

DOS

โ€“^

only

if

youโ€™re

building

in

pieces

and

linking

later

A

typical

(short)

program

โ€ข^

Notes:^ โ€“

first

include

is^

so we

have

access

to

printf

(standard

I/O)

-^

define

the

main

program

(must

be

called

main

)^ to

take

no

arguments

(thus

void

)^ and

return

an

integer

-^

braces

surround

the

program

-^

print

value

of

integer,

i,

in

formatted

line

-^

return

zero

(common

return

value

for

successful

program)

#include int main(void){

int i=53;printf(โ€œThe illustrious variable, i, is %d\nโ€,i);return 0;

More

on

program

โ€“^

semicolons

end

each

line

within

program

โ€“^

spacing

is

not

required,

but

makes

for

easier

reading

โ€“^

all

variables

must

be

declared

before

they

are

used

โ€“^

could

have

simply

said:

int i;

then

declared

later

that

i=

โ€“^

the

\n

is

a^

newline;

the

%d

formats

as

decimal

integer

#include int main(void){

int i=53;printf(โ€œThe illustrious variable, i, is %d\nโ€,i);return 0;

Alternate

form

โ€“^

semicolons

delimit

separate

statements,

but

this

program,

while

compact,

is

harder

on

the

eyes

โ€“^

this

time,

we

defined

and

assigned

the

variable

in

separate

steps

(more

commonly

done)

โ€“^

we

shortened

the

print

statement

fluff

โ€“^

the

format

is

now

characters

wide,

forcing

leading

zeros

-^ output

will

be:

i

0053

โ€“^

could

compactify

even

more,

if

sadistic

#include int main(void){ int i; i=53; printf(โ€œi = %04d\nโ€,i); return 0; }

Docsity.com

Result

โ€ข^

If

I^

run

simply

prog_name

,^ without

arguments,

I^

get:

โ€“^

usage:

prog_name

double_val

int_val

โ€“^

normally,

these

would

be

given

more

descriptive

names,

like

initial_x_position

and

number_of_trials

โ€ข^

If

I^

run

prog_name

,^ I

get:

โ€“^

Got

double_val

int_val

=^

โ€ข^

Note

that:

โ€“^

we needed a new header file for

exit()

For

Loops

-^

Notes:^ โ€“

declared

more

than

one

integer

on

same

line

(common

practice)

-^

k^ starts

at^

zero,

remains

less

than

(will

stop

at

increments

by

one

each

time

through

loop

โ€ข^

k++

adds

one

to^

variable:

same

as k^

same

as k^

=^

k^

+^

-^

adds

one

to

count

each

time

(see

rule

above)

-^

โ€œmodsโ€

count

by

4 (remainder

of

count

-^

output

is:

1,^

2,^ 3,

1,^

2,^ 3,

1,^

-^

could

(and

often

do)

use

k^

as^

int

value

within

loop

-^

for (;;)

is^

a^ way

to

get

an

indefinite

loop

(Ctrl

โ€C^

to^

quit)

int

k,count; count

for

(k=0;

k

k++)

count

count

printf

(โ€œcount

%d\nโ€,count);

Math

โ€ข^

Notes:^ โ€“

Must

include

math.h

โ€ข^

if^ compiling

on

linux/unix,

use

-lm

flag

to

link

math

โ€“^

note

mixed

assignment

in

variable

declarations

โ€“^

fabs

is

โ€œfloating

absolute

valueโ€,

and

here

keeps

sqrt

from

getting

a

negative

argument

โ€ข^

otherwise

result

could

generate

NaN

(Not

a

Number)

#include

โ€ฆdouble

x,y,z,pi,ampl=3.0,sigma=1.2; pi^

=^

x^

=^

sin(60.0*pi/180.0); y^

=^

sqrt(fabs(2*x

pi));

z^

=^

amplexp(-0.5pow(x/sigma,2.0))

Math

Warnings

โ€ข^

Number

one

mistake

by

C

newbies:

disrespecting

variable

type

โ€“^

i^

is^

an

integer,

so

truncates

to

zero

th

h^

i^

d^

bl

f

int

i,j=2,k=3; double

x,y,z; i^

=^

x^

=^

y^

=^

z^

=^

printf("i

%d;

x

%f;

y

%f;

z

%f;

other

%f\n",i,x,y,z,j/k);

i^

=^

x

y

z

other

Docsity.com

Casting

โ€ข^

when

necessary,

one

may

โ€œcastโ€

a

value

into

a

type

of

your

choice:

โ€“^

(double) j

โ€“^

((double) j)/k

โ€“^

j/((double) k)

โ€“^

(double) (j/k)

(integer

math

already

done)

โ€“^

(int) 6.

โ€ข^

lesson

is

to

take

care

when

mixing

variable

types

Talking

to

the

Parallel

Port

in

Windows

โ€ข^

We

will

use

the

inpout32.dll

package

-^

parallel

port

access

in

linux/unix

is very

straightforward

-^

Windows

98

and

before

was

also

easy

-^

new

Hardware

Abstraction

Layer

(HAL)

gets

in

the

way

-^

this

inpout

package

bridges

the

HAL

-^

see

www.logix4u.net

to

get

the

package

(already

installed

on

MHA

โ€^3574

machines)

-^

http://www.hytherion.com/beattidp/comput/pport.htm

for

test

programs

โ€ข^

Can

also

access

via

LPT

file

handle

-^

discussed

at

end

of

lecture

-^

runs

25

times

slower

than

the

inpout

version

-^ because

you

have

to

open/close

the

port

all

the

time

Sample

code

(stripped

down

to

fit

on

slide)

#include

#include

#include

#define

PPORT_BASE

0xD

usu.

0x

if

built-in

typedef

void

(_stdcall

*oupfuncPtr)(short

portaddr,

short

datum);

oupfuncPtr

oup32fp;

void

Out32(short

portaddr,

short

datum){

(oup32fp)(portaddr,datum);

int

main(void) {

HINSTANCE

hLib;

short

x=0xAA;

value

to

write

(expr.

in

hex)

hLib

LoadLibrary("inpout32.dll"); oup32fp

(oupfuncPtr)

GetProcAddress(hLib,

"Out32");

Out32(PPORT_BASE,x);

the

actual

output

command

FreeLibrary(hLib);return

Looping

to

make

a

waveform

โ€ข^

The

code

above

makes

a

ramp

of

output

values,

then

cuts

down

to

zero

and

starts

again^ โ€“

repeat

until

Ctrl

โ€C

kills

it

โ€ข^

Each

time:

โ€“^

the

outval

is

increased

by

โ€ข^

statement

equivalent

to

outval

outval

โ€“^

then

mod

by

(^256

and

start

over)

short

outval

for

way

to

make

infinite

loop:

^C

kills

outval

outval

Out32(PPORT_BASE,outval); }

Docsity.com