data types slides by faixan

18
Programming Fundamentals C- Data Types

Upload: faixaen-akhter

Post on 15-Aug-2015

26 views

Category:

Software


1 download

TRANSCRIPT

Programming Fundamentals

C- Data Types

Outlines

• Data Type• Integer, long and short• Integer, signed and unsigned• Char• Double

Data Type:

• In the C programming language, data types refer to an extensive system used for declaring variables or functions of different types.

• The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.

Integer, long and short:

• We had seen earlier that that the range of an Integer constant dependsupon the compiler. For a 16-bit compiler like Turbo C or TurboC++ the range is –32768 to 32767.

• For a 32-bit compiler the rangewould be –2147483648 to +2147483647.

• Here a 16-bit compiler means that when it compiles a C program it generates machinelanguage code that is targeted towards working on a 16-bit microprocessor like Intel 8086/8088. As against this, a 32-bitcompiler like VC++ generates machine language code that is targeted towards a 32-bit microprocessor like Intel Pentium. Notethat this does not mean that a program

• Note that this does not mean that a program compiled using Turbo Cwould not work on 32-bit processor.

• It would run successfully on 32-bit processor.

16-bit processor: 32-bit processor:

• shorts are at least 2 bytes big.• longs are at least 4 bytes big.• shorts are never bigger than ints.• ints are never bigger than longs.• This figure shows the size of different integers

based upon the OS used:• Its format string is %u.

Example:

• C allows the abbreviation of short int to short and of long int tolong. So the declarations made above can be written as,long i ;long abc ;short j ;short height ;

Integer, signed and unsigned

• A standard C integer data type (‘int’) is signed.

• Sometimes, we know in advance that the value stored in a giveninteger variable will always be positive, when it is being used to only count things, for example. In such a case we can declare thevariable to be unsigned, as in,unsigned int num_students ;

• With such a declaration, the range of permissible integer values(for a 16-bit OS) will shift from the range -32768 to +32767 to therange 0 to 65535. Thus, declaring an integer as unsigned almostdoubles the size of the largest possible value that it can otherwisetake.

• Note that an unsigned integer still occupies two bytes. This is how an unsigned integer can be declared:unsigned int i ;unsigned i ;

• The keyword is unsigned int and 2 bytes required, its range is 0 to 65535 and its format string is %u.

• Its example is:unsigned i ;scanf(“%u”,&i);

Char:

• To assign, or store a character value we use “char” data type.

• In C-language the keyword of Character data type is “char”.

• It requires 1 byte in memory.• Its range is -128 to +127.• Its format string is %c.

Example:

Output:

ASCII Codes:

• ASCII abbreviated from American Standard Code for Information Interchange, also known as US-ASCII, is a character-encoding scheme. Originally based on the English alphabet, it encodes 128 specified characters into 7-bit binary integers.

Double:

• C-language offers a double data typethat occupies 8 bytes in memory and has a range from -1.7e308 to+1.7e308. A variable of type double can be declared as,double a, population ;

• In C-language its keyword is “double”.• It requires 8 bytes in memory.• Its format string is “%lf”.• Its example is double a, population ;Scanf(“%lf%lf”,&a,&population);

Conclusion:

• First of all we discussed about Data Types and then various Data types like int, short, long, signed, unsigned double, char.

• All about the Data Types their format strings.

• Then took a little view of ASSCII codes.

Thank you for concentration…Prepared by: Muhammad Faizan Akhter