c programming data types

c programming data types

A data type is essential to identify the storage representation and the type of operations that can be performed on that data. In general, data type associated with variable indicates,

  • Type of value stored in the variable.
  • Amount of memory (size) allocated for the data variable.
  • Type of operations that can be performed on the variable.

‘C’ supports four basic data type which can be grouped under two groups of data types.

  1. integral data type
  2. Floating point data type

Integral Data Type

An integral Data type is used to store whole numbers and characters. It can be further classification as,

  • Integer data type
  • Character data type
Integer data type
  • This data type is used to store the whole numbers. The integers do not contain the decimal points. They take the binary form for storage. The size of the integer depended on the word length of a machine i.e., it can be either 16 or 32 bits. On a 16-bit machine. The range of integer value is -32768 to +32767.
  • ‘C’ provides control over the range of integer value and the storage space occupied by these values through the data type ‘short int’, ‘int’, ‘long int’ in both ‘signed’ and unsigned’ form.

Signed integer: signed integers are those integers which use 15 bits for storing the magnitude of a number and 1 bit to store its sign. The left-most bit i.e., a 16th bit used the sign, it is 1 for negative number and ‘0’ for the positive number.

Unsigned integer: unsigned integer use all 16 bit i.e., (15+1) bits to store the magnitude i.e., these are the whole numbers that always hold positive values and the sign bit also contains the value.

Character Data Type
  • The data type indicates that value stored in the associated variable is a character. Each character has an equivalent ASCII value.
  • These characters are internally stored as integers.
  • ‘char’ data type occupies one byte of memory for the storage of character.
  • The qualifiers signed and unsigned can be applied on ‘char’ data type.
TypeSizeRangeFormat Specifier
char (or) signed char1-128 to +127%c
unsigned char10 to 255%c

Floating-point Datatype

  • A floating point data type is used to store real numbers. The keyword ‘float’ is used to declare a variable holding a floating-point number with 6 digits of precision occupies 4 bytes for storage.
  • For greater precision we can use the ‘double’ data type for further precision, we have long double.

Data Type qualifiers

  • A qualifier is an adjective describing the data type, they are used in combination with the basic data types.
  • This qualifier along with data types. These qualifiers along with data types can be used to have control over storage space and range of numbers.
  • ‘C’ language provides a set of qualifiers as given below,
    1. short
    2. long
    3. signed
    4. unsigned
  • Short: this qualifier can be used only with the data type int.
    Syntax: short int variable;
  • ‘short int’ is used for small ranger values (-128 to +127) and it occupies 50% of storage space (1 byte) as compared to regular int (2 bytes).
  • long: these qualifiers is used along with the data types ‘int’ and ‘double’. This qualifier is used to increase the range of values, thus consuming more storage space.
    Syntax: long data-type variable;
  • The qualifier ‘long’, when associated with ‘int’ data type, takes a range of integer values and occupies 32 bits (4 bytes) for storage.
  • The qualifier ‘long’, when associated with ‘double’ data type, takes a range of floating point values and occupies 80 bits (10 bytes) for storage.
  • signed the qualifier can be used with ‘int’ and ‘char’ data types. This qualifier affects the internal representation of the data value.
  • When signed qualifier is used with the basic data type the starting bit of storage gets reserved for the sign.
  • unsigned this qualifier can be applied with ‘int’ and ‘char’ data type. This qualifier when used, the total bits get reserved for the magnitude, thus the range of values gets increased.

Constants 

A quantity or number that does not vary during the execution of the program is known as constants. These constants are classified into two types

  1. Numeric constants
  2. Character constants
Numeric constants

Numeric constants can be classified as, integer constants and real constants

Integer constants The integer constants are confined to the following features.

  • It contains a minimum of one digit.
  • It does not contain a decimal point.

Blank spaces and commas are not allowed within the integer constant. An integer constant can be either positive or negative value.
Ex: 400, +789, -100, etc.

The range of integer constants depending on the machine. ‘C’ provides a range of -32768 to 32767 integer constants for 16-bit word length machine and -1247483648 to 2147483647 for 32 bit.

A long integer unsigned integer and unsigned long integer constant can be represented by appending the l or L, u or U or UL respectively.
Ex: long integer constants: 98491560l, 98491560L

unsigned integer constants 55596U, 55596u

unsigned long integer constants 955713345I, 955713345L

An integer constant can be represented in any one of the three numbers system i.e. decimal, octal, hexadecimal.
Decimal integers these constant consists of numbers from 0 to 9 preceded by an optional sign.Ex: +129, -95,145

An octal integer these constant consists of any digits from 0 to 7 with leading zero (‘0’).
Ex: 037, 067,050

Hexa-decimal these integer constants consist of a combination of digits from 0 to 9 and alphabets, i.e.a-f or A-F. The alphabets a to f represent numbers 10 to 15 respectively. Hexadecimal numbers are preceded by 0x or 0X.
Ex: 0xA5, 0XA5, 0xa6c

Real Constants

Real constants are the quantities which contain fractional part i.e., decimal point. Real Constants are also known as the floating-point constant. Real constants can be represented in two ways.

  • Decimal (fractional) form
  • Exponential (scientific) form

Decimal form

  • The real constants represented in decimal form are confined to the following rules.
  • The constant must contain a whole number followed by a decimal point and fraction part.
  • It can be either +ve or –ve.
  • blank spaces and commas are not within the real constant
  • The digits before and after the decimal point can be omitted
    Ex: 110.1,+305.25,-400.50,-.10.

Exponential form

The exponential form is used to represent the real constants both either too large or too small value.
Ex: 8500000000 can be represented as 8.5e9. 0.000000786 can be represented as 7.86e-7.

  • The real constant represented in exponential form consists of two-part, mantissa (i.e. part appearing before the letter e or e) and exponent (i.e. part appearing after e or e).
  • A real constant represented in exponential form is confined to the following rule
  • The letter e or e must separate the mantissa and exponential part.
  • Mantissa part could be either a real number or an integer.
  • Mantissa part could be either +ve or –ve (default is +ve)
  • The exponential part must contain at least one digit, which could be either a +ve or –ve integer.
  • The real constant can be represented as a float (i.e. single –precision) and long double (i.e. extend double –precision) by appending the terminals ‘f’ or ‘f’ and ‘l’ or ‘l’ respectively (Note: default is double-precision).
Character Constants

The character constants can be divided into two types,

  • single character constants
  • string constants

Character constants A single character constant is a single digit, alphabet or special symbol, enclosed within single quotes.
Ex: 'a', '1', '+'

  • A character constant represents numeric value. (i.e., ASCII value of the character). A character constant is, therefore, arithmetic operations.
  • Maximum of one character is taken within a character constant.

String constants A string constant contains a group of character (i.e., alphabets, digits, special symbols, and blank spaces)
Ex: "welcome", "+,-" "10+2".

Type conversion

  • Sometimes the programmer needs the result in certain data type. Example, a division of 5 with 2 should return a float value.
  • Practically compiler always returns integer values because both the arguments are of an integer data type. In those cases, type conversion is required.

Ex:

printf (“\ntwo integers (5&2) : %d”, 5/2);   2
printf (“\none int and one float (5.5&2) : %f”, 5.5/2);     2.75
printf (“\ntwo integers (5&2) : %f”,(float) 5/2);      2.5

whatsapp

error: Content is protected !!