C Data Types

Data types are used to store various types of data that is processed by program. Data type attaches with variable to determine the number of bytes to be allocate to variable and valid operations which can be performed on that variable. C supports various data types such as character, integer and floating-point types.

Character Data Type

C stores character type internally as an integer. Each character has 8 bits so we can have 256 different characters values (0-255).
Character set is used to map between an integer value and a character. The most common character set is ASCII.
Let take a look at example of using a variable which hold character 'A'.
01.#include <stdio.h>
02. 
03.void main()
04.{
05.char ch = 'A';
06.printf("%c\n",ch);
07.ch = 65;// using integer representation
08.printf("%c\n",ch);
09.ch = '\x41';   // using hexadecimal representation
10.printf("%c\n",ch);
11.ch = '\101';   // using octal representation
12.printf("%c\n",ch);
13.}
Here is the output
A
A
A
A

Integer Data Type

Integer data types are used to store numbers and characters. Here is the table of integer data type in various forms:
Data TypeMemory AllocationRange
signed char1 byte27 to 271 (128 to 127)
Unsigned char1 byte0 to 281 (0 to 255)
short2 bytes215 to 215 1 (32768 to 32767)
Unsigned short2 bytes0 to 216 1 (0 to 65535)
long int4 bytes231 to 2311 (2,147,483,648 to 2,147,483,647)
int2 or 4 bytes depending on implementationRange for 2 or 4 bytes as given above

Floating-point Data Type

The floating-point data types are used to represent floating-point numbers. Floating-point data types come in three sizes: float (single precision), double (double precision), and long double (extended precision). The exact meaning of single, double, and extended precision is based on implementation defined. Choosing the right precisionfor a problem where the choice matters requires understanding of floating point computation.

Floating-point literal

Floating-point literal is double by default. You can use the suffix f or F to get a floating-point literal 3.14f or 3.14F

Type Conversion

Type conversion occurs when an expression has a mixed data types. At this time, thecompiler will try to convert from lower to higher type, because converting from higher to lower may cause loss of precision and value.C has following rules for type conversion.
  • Integer types are lower than floating-point types.
  • Signed types are lower than unsigned types.
  • Short whole-number types are lower than longer types.
  • The hierarchy of data types is as follows: double, float, long, int, short, char.
So based on those rules, we have general rules for type conversion:
  • Character and short data are promoted to integer.
  • Unsigned char and unsigned short are converted to unsigned integer.
  • If the expression includes unsigned integer and any other data type, the other data type is converted to an unsigned integer and the result will be unsigned integer.
  • If the expression contains long and any other data type, that data type is converted to long and the result will be long.
  • Float is promoted to double.
  • If the expression includes long and unsigned integer data types, the unsigned integer is converted to unsigned long and the result will be unsigned long.
  • If the mixed expression is of the double data type, the other operand is also converted to double and the result will be double.
  • If the mixed expression is of the unsigned long data type, then the other operand is also converted to double and the result will be double.

Conversion Type Casting

When we want to convert the value of a variable from one type to another we can use type casting. Type casting does not change the actual value of variable. It can be done using a cast operator (unary operator).

No comments:

Post a Comment