TRANS03


C Data Types and Variables Chapter 3


Objectives of this chapter:

 

 

Variables and Constants

What are Data Types ?

C Data Types.

The int Type

 

 

 

 

Declaring an int variable.

eg:

int sum;

int marks, scores, value1;

 

 

 

marks = 75;

Initializing a variable

 

int sum = 0;

int marks=19, score =94;

int value, num = 76;

 

&

in the last line, only num is initialized to 76.

 

int Constants

 

sum=45;

 

sum = 055;

 

Sum=0x2D;

 

Other int types

 

 

unsigned, long and short.

 

 

 

 

 

 

Declaring other integer types

 

Examples

 

long int gross_pay;

long tax;

short int value;

short pants;

unsigned int count;

unsigned players;

unsigned long headcount;

unsigned short votes;

 

Type unsigned int, long int, and unsigned long int constants

Examples:

 

unsigned int levels;

long int score;

unsigned long int high_score;

 

levels = 500U;

score=5000l;

high_score=45000UL;

 

 

 

 

 

&

The types unsigned double, and unsigned long double are NOT valid floating-point types. All floating point types list above stores positive and negative values.

 

Declaring Floating-Point variables

 

 

Example:

 

float average, frequency;

double trouble;

float price=6.54;

long double gnp;

 

Floating-Point Constants

 

 

For example :

 

3.14159 .2 100 .25

 

 

For example:

 

1.2e16 4e12 .8E-5 2.e8

 

 

For example:

 

float number;

number = 4.0 * 2.0;

 

 

 

 

Example :

2.3f 4.5f 9.11e9F 2.1E2f

 

 

Example:

 

54.321 4.32e4L

 

 

The char Type

 

 

 

 

 

The char type is defined as a one-byte (eight bits) units of memory.

 

Declaring type char variables

 

variables of type char are defined in the same manner as other variables:

 

char choice;

char letter1, letter2;

 

signed and unsigned char type

 

 

Characters constants and initialization

 

For example:

 

char my_grade = 65;

char your_grade = ’A’;

 

In the above declarations, my_grade is initialized to the value of 65 which is the ASCII code for the character A, your_grade is initialized to the character constant A.

 

a single letter between single quotation marks is a C character constant.

 

When the compiler encoutners a character constant like ’A’, it is converted to its proper ASCII code value (which is 65).

 

Example:

 

char big; /* declare a char variable */

 

big = ’B’; /* assign letter B into big*/

 

big = B; /* No! B is treated as a variable! */

 

big = "B"; /* no! "B" is a string */

 

 

 

Non-printing Characters

 

 

 

 

 

char beep = 7;

 

 

 

char ctrl_p = \x10;

 

& the character ‘\X010’ is equivalent.

 

Use special symbol sequences called escape sequences..

 

\a

alert

\b

backspace

\f

formfeed

\n

newline

\r

carriage return

\t

horizontal tab

\v

vertical tab

\\

backslash (\)

single quote (‘)

\"

double quote (")

 

 

For example:

 

char beep = ‘\a’;

char netx_line;

 

next_line = ‘\n’;

 

 

 

Defining Program constants

 

 

 

 

 

#define constant_name value

 

example:

#define PI 3.14159

 

 

 

const const_name type = value;

 

example:

 

const int pi = 3.14159;

 

 

 

 

 

 

 

1