03 data types, Lecture notes of C programming

Introduction to C

Typology: Lecture notes

2013/2014

Uploaded on 12/29/2014

wsabren1..
wsabren1.. 🇮🇶

4

(5)

11 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Types
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download 03 data types and more Lecture notes C programming in PDF only on Docsity!

Data Types

data typesdata typesdata typesdata types

Variables in C must have a data type

integers and floating-point numbers

the following are the basic C data types:

char

short int (or just “short”)

int

long int (or just “

long

float

double

long double

why have data types?why have data types? why have data types?why have data types?

not the rule)Some languages don’t (but they’re the exception,

The computer itself may have types

some

numberscomputers handle integers differently from real

Performance

old computers did everything with integers

calculations involving real numbers were slow

Safety

the compiler can catch some errors

bits are fundamentallybits are fundamentallybits are fundamentallybits are fundamentally

typelesstypelesstypelesstypeless

  • • The computer doesn’t store the type, just bits

depending on what type you tell the computer it isthe same data can be interpreted differently

  • • 01000000000000000000000000000000

hex: 0x

Integer: 1073741824

Floating-point: 2.

String: “@”

charcharcharchar –

  • ––

a special integral typea special integral typea special integral typea special integral type

  • • Used to store characters

letters

digits

keyboard symbols

some unprintable characters

  • Remember – in the computer, everything is

numbers

even text

text as numberstext as numbers text as numberstext as numbers

Every character is assigned a numeric code

There are different sets of codes

most common –

ASCII

dinosaur –

EBCDIC

Probably others

We will only consider ASCII in this class

but there’s nothing intrinsic in C that specifies this

intint intint

  • Used when you have no specific reason to pick • The most common integer type

one of the others

use short when space is at a premium

than 32,767use long when your numbers may be greater

it’s the most efficient integer type

doubledouble doubledouble

  • Used when you have no specific reason to pick • The most efficient floating-point type

one of the others

float – when space is at a premium

very big numberslong double – when you need very precise or

what does this do?what does this do? what does this do?what does this do?

  • • This program will print out a temperature conversion table

prints a Fahrenheit temperature and its Celsius equivalent

intervalsfor Fahrenheit temperatures from 0 to 300 at 20 deg.

let’s look more closelylet’s look more closelylet’s look more closelylet’s look more closely

#include

(^) <stdio.h>

int (^) main

(^) (void)

int (^) fahr;

int (^) celsius;

int (^) lower

(^) = (^) 0;

int (^) upper

(^) = (^) 300;

int (^) step

(^) = (^) 20;

fahr (^) = (^) lower;

while

(^) (fahr

(^) <= (^) upper)

celsius

(^) (fahr

(^) – (^) 32) (^) / (^) 9;

printf

(^) (“%d\t%d\n”,

(^) fahr,

celsius);

fahr (^) = (^) fahr

(^) + (^) step;

return}

(^) 0;

  • }
Remember, this stuff goes on all our programs

let’s look more closelylet’s look more closelylet’s look more closelylet’s look more closely

#include

(^) <stdio.h>

int (^) main

(^) (void)

int (^) fahr;

int (^) celsius;

int (^) lower

(^) = (^) 0;

int (^) upper

(^) = (^) 300;

int (^) step

(^) = (^) 20;

fahr (^) = (^) lower;

while

(^) (fahr

(^) <= (^) upper)

celsius

(^) (fahr

(^) – (^) 32) (^) / (^) 9;

printf

(^) (“%d\t%d\n”,

(^) fahr,

(^) celsius);

fahr (^) = (^) fahr

(^) + (^) step;

(^) }

return

(^) 0;

  • }

This is an assignment statement

this sets the value of a variable
in this case, it sets the value of
fahr
to the value of
lower
, which is 0

let’s look more closelylet’s look more closelylet’s look more closelylet’s look more closely

#include

(^) <stdio.h>

int (^) main

(^) (void)

int (^) fahr;

int (^) celsius;

int (^) lower

(^) = (^) 0;

int (^) upper

(^) = (^) 300;

int (^) step

(^) = (^) 20;

fahr (^) = (^) lower;

while

(^) (fahr

(^) <= (^) upper)

celsius

(^) (fahr

(^) – (^) 32) (^) / (^) 9;

printf

(^) (“%d\t%d\n”,

(^) fahr,

(^) celsius);

fahr (^) = (^) fahr

(^) + (^) step;}

return

(^) 0;

  • (^) }

This is a loop

we’ll get to loops in a while

this will repeatedly execute the following 3 statements

as long as

fahr

is less than or equal to

upper

let’s look more closelylet’s look more closelylet’s look more closelylet’s look more closely

#include

(^) <stdio.h>

int (^) main

(^) (void)

int (^) fahr;

int (^) celsius;

int (^) lower

(^) = (^) 0;

int (^) upper

(^) = (^) 300;

int (^) step

(^) = (^) 20;

fahr (^) = (^) lower;

while

(^) (fahr

(^) <= (^) upper)

celsius

(^) (fahr

(^) – (^) 32) (^) / (^) 9;

printf

(^) (“%d\t%d\n”,

(^) fahr,

(^) celsius);

fahr (^) = (^) fahr

(^) + (^) step; }

return

(^) 0;

Here’s another use of printf to print things out

this is more complex than we’ve used before

it prints the values of two variables

let’s look more closelylet’s look more closelylet’s look more closelylet’s look more closely

#include

(^) <stdio.h>

int (^) main

(^) (void)

int (^) fahr;

int (^) celsius;

int (^) lower

(^) = (^) 0;

int (^) upper

(^) = (^) 300;

int (^) step

(^) = (^) 20;

fahr (^) = (^) lower;

while

(^) (fahr

(^) <= (^) upper)

celsius

(^) (fahr

(^) – (^) 32) (^) / (^) 9;

printf

(^) (“%d\t%d\n”,

(^) fahr,

(^) celsius);

fahr (^) = (^) fahr

(^) + (^) step; }

return

(^) 0;

Another assignment

changes the value of

fahr

by adding the

value of

step

to it