data type in c

20
1 DATA TYPE IN C This session Outline Tokens Data Types Statements Types

Upload: thirumalaikumar3

Post on 15-Apr-2017

296 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Data type in c

1

DATA TYPE IN C This session Outline

Tokens

Data Types

Statements Types

Page 2: Data type in c

2

Tokens in C

5 Tokens1. Keywords2. Identifiers3. Constants/Literals4. Operators5. White Spaces/Other symbols

Basic elements for building a program

Page 3: Data type in c

3

Tokens in CKeywords

These are reserved words of the C language. For example int, float, if, else, for, while etc.

IdentifiersAn Identifier is a sequence of letters and digits, but must start with a

letter. Underscore ( _ ) is treated as a letter. Identifiers are case sensitive. Identifiers are used to name variables, functions etc.

Valid: Root, _getchar, __sin, x1, x2, x3, x_1, IfInvalid: 324, short, price$, My Name

Constants/LiteralsConstants like 13, ‘a’, 1.3e-5, “Raj” etc.

Integer constant

Character constant

String constantFloat or Real constant

Page 4: Data type in c

Lectures on Busy Bee Workshop 4

Tokens in C String Literals

A sequence of characters enclosed in double quotes as “…”. For example “13” is a string literal and not number 13. ‘a’ and “a” are different.

OperatorsArithmetic operators like +, -, *, / ,% etc.Logical operators like ||, &&, ! etc. and so on.

White SpacesSpaces, new lines, tabs, comments ( A sequence of characters enclosed

in /* and */ ) etc. These are used to separate the adjacent identifiers, keywords and constants.

Page 5: Data type in c

Lectures on Busy Bee Workshop 5

Tokens in C - Example void main()

{ int a=25; float b=45.0; char c='A'; float sum; sum=a+b+c; printf("Result =%f\n",sum);}

What will be the output?

TokensKeywords?Identifiers?Constants?Operators?Symbols?

Page 6: Data type in c

Lectures on Busy Bee Workshop 6

Basic Data TypesData types are used to indicate the

nature and size of the data.Data Type

Nature Size Range

char Character constant 1 Byte – 8 bits -128 to 127int Integer constant 2 Bytes – 16

bits-32768 to 32767

float Real constant 4 Bytes – 32 bits

3.4 * (10**-38) to 3.4 * (10**+38)

double Real constant 8 Bytes – 64 bits

1.7 * (10**-308) to 1.7 * (10**+308)

Type Modifiers - signed, unsigned, short, and long

A type modifier alters the meaning of the base data type to yield a new type.

Page 7: Data type in c

Lectures on Busy Bee Workshop 7

Data TypesIntegral data types – Numbers without decimal point

char / signed char Stored as 8 bits. Unsigned 0 to 255. unsigned char Signed -128 to 127.

int /signed int Stored as 16 bits. Unsigned 0 to 65535. unsigned int Signed -32768 to 32767.

short int Same as int.

long / long int Stored as 32 bits. Unsigned 0 to 4294967295.signed long /signed long int Signed -2147483648 to 2147483647 unsigned long int

All of the type modifiers can be applied to the base type int.signed and unsigned can also be applied to the base type char.Control String/Format Specifier

– %c, %h, %d, %l, %u, %ul

These are not valid constants: ?25,000 7.1e 4 $200 2.3e-3.4 etc.

Page 8: Data type in c

Lectures on Busy Bee Workshop 8

Data TypesFloating Point Numbers

Floating point numbers are rational numbers. Always signed numbers. i.e Numbers with decimal point.

float• Typically stored in 4 bytes(32 bits) • Range - 3.4 * (10**-38) to 3.4 * (10**+38)

Double• Typically stored in 8 bytes(64 bits)• Range - 1.7 * (10**-308) to 1.7 * (10**+308).

Long double• Typically stored in 10 bytes (80 bits)• Range - 3.4 * (10**-4932) to 1.1 * (10**+4932)

Control String/Format Specifier– %f, %e, %lf

Page 9: Data type in c

Lectures on Busy Bee Workshop 9

Data TypesCharacter and string constants‘c’ , a single character in single quotes are stored as char.

Some special character are represented as two characters in single quotes.‘\n’ = newline, ‘\t’= tab, ‘\\’ = backlash, ‘\”’ = double quotes.Char constants also can be written in terms of their ASCII code.‘\060’ = ‘0’ (Decimal code is 48).

A sequence of characters enclosed in double quotes is called a string constant or string literal. For example“Charu”“A”“3/9”“x = 5”

Page 10: Data type in c

Lectures on Busy Bee Workshop 10

Data TypesCharacter and String data types

char /signed char/unsigned char• Typically stored in 1 byte(8 bits) • Range – signed -128 to 127, unsigned 0 to 255

char name[20] – Character Array • Above example occupy 20 bytes to store maximum of 20

characters of any one name.• char name[5][20] – Store any five name, each name have

maximum of 20 letters each.

Control String/Format Specifier– %c, %s

Page 11: Data type in c

Lectures on Busy Bee Workshop 11

Data Types - SummaryData Types Length Rangeunsigned char 8 bits 0 to 255 char 8 bits -128 to 127 unsigned int 16 bits 0 to 65,535 short int 16 bits -32,768 to 32,767 int 16 bits -32,768 to 32,767 enum 16 bits -32,768 to 32,767 unsigned long 32 bits 0 to 4,294,967,295 long 32 bits -2,147,483,648 to 2,147,483,647 float 32 bits 3.4 * (10**-38) to 3.4 * (10**+38) double 64 bits 1.7 * (10**-308) to 1.7 * (10**+308) long double 80 bits 3.4 * (10**-4932) to 1.1 * (10**+4932)

Page 12: Data type in c

Lectures on Busy Bee Workshop 12

enum & typedef Type Enum

Defines a set of constants of type int. Example: enum modes { LASTMODE = -1, BW40 = 0, C40,

BW80, C80, MONO = 7 }; Here "modes" is the type tag. "LASTMODE",

"BW40", "C40", etc. are the constant names. The value of C40 is 1 (BW40 + 1); BW80 = 2 (C40

+ 1), etc.

typedef Define a new data type using existing types Example :

typedef int Number;

typedef char String[20];

Page 13: Data type in c

Lectures on Busy Bee Workshop 13

VariablesNaming a Variable

Must be a valid identifier.Must not be a keywordNames are case sensitive.Variables are identified by only first 32 characters.Library commonly uses names beginning with _.Naming Styles: Uppercase style and Underscore stylelowerLimit lower_limitincomeTax income_tax

Page 14: Data type in c

Lectures on Busy Bee Workshop 14

DeclarationsDeclaring a Variable

Each variable used must be declared.A form of a declaration statement isdata-type var1, var2,…;

Declaration announces the data type of a variable and allocates appropriate memory location. No initial value (like 0 for integers) should be assumed.

It is possible to assign an initial value to a variable in the declaration itself.data-type var = expression;

Examplesint sum = 0;char newLine = ‘\n’;float epsilon = 1.0e-6;

Page 15: Data type in c

Lectures on Busy Bee Workshop 15

Global and Local VariablesGlobal Variables

These variables are declared outside all functions.

Life time of a global variable is the entire execution period of the program.

Can be accessed by any function defined below the declaration, in a file.

/* Compute Area and Perimeter of a circle */

#include <stdio.h>float pi = 3.14159; /* Global */

main() { float rad; /* Local */ printf( “Enter the radius “ ); scanf(“%f” , &rad);

if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad;

printf( “Area = %f\n” , area ); printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”);

//printf( “Area = %f\n” , area );}

Page 16: Data type in c

Lectures on Busy Bee Workshop 16

Global and Local VariablesLocal Variables

These variables are declared inside some functions.

Life time of a local variable is the entire execution period of the function in which it is defined.

Cannot be accessed by any other function.

In general variables declared inside a block are accessible only in that block.

/* Compute Area and Perimeter of a circle */

#include <stdio.h>float pi = 3.14159; /* Global */

main() { float rad; /* Local */ printf( “Enter the radius “ ); scanf(“%f” , &rad);

if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad;

printf( “Area = %f\n” , area ); printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”);

//printf( “Area = %f\n” , area );}

Page 17: Data type in c

Statement Types

Input Statements – scanf(), getchar(), gets() etcOutput Statements – printf(), putchar(), puts() etcDeclaration Statement – Datatype variablelist;Assignment Statements - variable=expression;/(=,+=,-

+,*=,/=)Control statements – if, if …else, nested if, switch Iteration statements – for, while, do while

Lectures on Busy Bee Workshop 17

Page 18: Data type in c

Try this in lab sessionProgram for print the memory allocation of

all data types. Program for print your address label.Program for add this two number 1,20,000

and 80,500.Write a program to illustrate enum &

typedef.Rewrite Program 2 for statements

illustration (i.e Get any number <5, 1 time, or 2 time.

Lectures on Busy Bee Workshop 18

1. Know the date type size2. Flow control, tokens3. Size limit, control string4. enum & typedef 5. Statements

Page 19: Data type in c
Page 20: Data type in c