Sie sind auf Seite 1von 13

Data Types in C

Data Transformation

Programs transform data from one form to another

Input data Output data

Stimulus Response

Programming languages store and process data in


various ways depending on the type of the data;
conseuently! all data read! processed! or written "y a
program must have a type

Two distinguishing characteristics of a programming


language are the data types it supports and the
operations on those data types

# Data Type

# data type is

# set of values #$D

# set of operations on those values

# data type is used to

Identify the type of a varia"le when the varia"le is


declared

Identify the type of the return value of a function

Identify the type of a parameter e%pected "y a function



# Data Type &continued'

(hen the compiler encounters a declaration for a


varia"le! it sets up a memory location for it

#n operator used on a varia"le or varia"les is legal


only if

The operator is defined in that programming language


for a varia"le of that type

The varia"le or varia"les involved with the operator are


of the same or compati"le types

Rules for )onstructing Identifiers in )

)apital letters #*+! lowercase letters a*,! digits -*


.! and the underscore character

/irst character must "e a letter or underscore

0sually only the first 12 characters are significant

There can "e no em"edded "lan3s

4eywords cannot "e used as identifiers

Identifiers are case sensitive


Identifiers refer to the names of data types! constants!
varia"les! and functions

Two )lassifications of Data Types

5uilt*in data types

/undamental data types &int, char, double, float,


void, pointer'

Derived data types &array! string! structure'

Programmer*defined data types

Structure

0nion

6numeration

/undamental Data Types

void used to denote the type with no values

int used to denote an integer type

char used to denote a character type

float, double used to denote a floating


point type

int *! float *! char 7 used to denote a


pointer type! which is a memory address type

0ses of /undamental Data Types
int elevationIndicator;
char inputSymbol;
float totalCost;
int main (void)
{
double grossroduct;
int *temperature!aluetr;
grossroduct " #$%&'();
inputSymbol " *a*;
return (+);
, -- .nd main

Derived Data Types

Array a finite seuence &or ta"le' of


varia"les of the same data type

String an array of character varia"les

Structure a collection of related varia"les


of the same and8or different data types9 The
structure is called a record and the varia"les
in the record are called mem"ers or fields

0ses of Derived Data Types
int elevation/able01+2;
char inputSymbols02 " 34ello 5orld3;
struct operationsStruct
{
double heat6eading;
int temperature!alue;
float speed7eter;
char actionCode;
,; -- .nd struct
struct operationsStruct current8perations;

Records &Structures'

# record permits a programmer to handle a group


of varia"les as one varia"le

The fields &mem"ers' of a record can "e any "uilt*


in or programmer*defined data type

# record can have values assigned to and read


from it :ust li3e the "uilt*in varia"le types

# record can also "e passed as an argument to a


function and serve as the return value for a
function

The typedef 4eyword and Records
The typedef 3eyword can "e used to create a synonym for a
data type9 It is most often used to simplify the naming and
use of record types &i9e9! structure types'9
typedef struct
{
double heat6eading;
int temperature!alue;
float speed7eter;
char actionCode;
, operations6ecord/ype;
operations6ecord/ype current8perations;
operations6ecord/ype future8perations;

5asic Operations on Records
current8perations'speed7eter " 1#$'%;
current8perations'temperature!alue " %&;
current8perations'actionCode " *9*;
latest6eading " current8perations'heat6eading;
status:actor " current8perations'speed7eter *
current8perations'temperature!alue;
future8perations " current8perations;
printf(3/emp; <d Code; <c=n3,
future8perations'temperature!alue,
future8perations'actionCode);

Das könnte Ihnen auch gefallen