Data Types and Constants in C-Chapter 1 - LEKULE

Breaking

19 Dec 2015

Data Types and Constants in C-Chapter 1

To use any language in communication (to write/to speak), we need to understand it’s grammar first.  In the case of a programming language like C, the scenario is same as in the case of a communication language. We need to understand the grammar of C programming language first. So here begins:-
In this article we explain Data Types and Constants


The basic grammar of C can be explained through:-

  • Data Types
  • Constants
  • Variables and Keywords
  • Operators and Operands
  • Control structures (branching and looping)
  • Functions
  • Arrays
  • Strings
  • Pointers
  • Structures
  • Files

Data Types


There are 4 data types in C language. They are:-
  • int – This data type is used to define an integer number (-….-3,-2,-1,0,1,2,3….). A single integer occupies 2 bytes.
  • char – Used to define characters. A single character occupy 1 byte.
  • float – Used to define floating point numbers (single precision). Occupies 4 bytes.
  • double – Used for double precision floating point numbers(double precision). Occupies 8 bytes.

Note:- Single precision and Double precision basically differs in the number of digits represented after the decimal point. Double precision number will represent more digits after the decimal point than a single precision number. Example:- Single precision – 32.75   and double precision –  32.7543


Data Types can also be classified as shown in the image below – Primitive, Derived and User Defined.

C Data Types- C programming tutorial

  • Primitive data types are the first form – the basic data types (int,char,float,double).
  • Derived data types are a derivative of primitive data types known as arrays, pointer and function.
  • User defined data types are those data types which are defined by the user/programmer himself.

Note: We will learn about Derived and user defined data types in coming chapters.

Data Type Qualifiers


Each of these data type has got qualifiers. The purpose of a qualifier is to manipulate the range of a particular data type or its size. The 4 qualifiers in C are long,short,signed and unsigned.First two long and short are called size qualifiers and the other two signed and unsigned are called sign qualifiers.


Example: int – when declared normally is of 2 bytes. If it is declared as unsigned int – then its range is from 0 to 65535. In other case, if it is declared as signed int – then its range is from (-32767 to 32768). In the case of signed int, one bit (MSB) is used to store the sign of the integer +/-. This basically means the programmer will not be able to display/store a number higher than 65535 using unsigned int. Similarly it is not possible to manipulate a number beyond -32767 or +327678 using signed int.


The qualifiers long and short are used to increase storage size of the data type.
Example: Integer data type int is normally 2 byte. If you declare it as long int – then its size will increase from 2 bytes to 4 bytes. Similarly if you declare it as short int – its size will reduce from 2 bytes to 1 byte. 


The table below describes all data types and the most commonly used qualifier combinations – with its size,range and format specifier. Note: You will learn more about the use of format specifiers in coming chapters.


Keyword
Format Specifier
Size
Date Range
char
%c
1 byte
-128 to +127
int
%d
2 bytes
0 to 255
float
%f
4 bytes
-3.4e38 to +3.4e38
double
%lf
8 bytes
-1.7e38 to +1.7e38
long int
%ld
4 bytes
-231 to +231
unsigned int
%u
2 bytes
0 to 65535
long double
%Lf
16 bytes
-3.4e38 to +3.4e38
Unsigned char
%c
1 byte
0 to 255


Constants

There are 4 types of constants in C.
  • Integer constants
  • Character constants
  • Real/Floating point constants
  • String constants

C programming tutorial



By definition, a constant is a quantity that does not change throughout the execution of a program.

Integer Constants


An integer constant is an integer quantity which contains a sequence of digits.It should not have a decimal point. Blanks and commas are not allowed within an integer constant.An integer constant can be either +ve or -ve. The constant must lie within the range of the declared data type (including qualifiers long,short etc.).

Example of valid integer constants:- 976    8987   5   -25  etc.

Example of invalid integer constants:-  78.43   7-8   89,76  etc.

An integer constant can be either Decimal, Hexa Decimal or Octal. See the table below to understand how these 3 different constants are defined in C.


Integer Type
Prefix
Suffix
Example
Hexa Decimal
Ox
  OxA7B
Octal
O
  O54
Long Hexa Decimal
Ox
I or L
OxA7BL
Unsigned Long Hexa Decimal
Ox
UI or UL
OxA7FUI
Long Octal
O
I or L
O54L
  • A decimal constant can contain any combination of integers from 0 through 9. Ex: 189      0      75       87
  • A hexa decimal constant should begin with 0X or 0x. Ex: 0x65F  or 0X7A
  • An octal constant should begin with digit 0. It can take any digits from 0 through 7.

Note:- There is no binary integer constant in C by default. This means, you cant give a binary number directly to program by writing sth like:- 0b11001011 – which is meaningless and result in an error. How ever, programmer can add a preprocessor (which we will learn later) to accept binary numbers in a program.

Floating Point Constants


They are also known as real constants. A real constant contains a decimal point or an exponent. It can be either +ve or -ve. Commas and blank space are not allowed within a real constant.


Examples:– 254.175,      -16.47      -0.5e-7     +4.1e8

Character Constant

A character constant is a character which is enclosed in single quotes. A character constant is of size 1byte and can contain only 1 character. A character can be an alphabet like a,b,A,C etc or a special character like &,^, $, #,@ etc or a single digit from 0 through 9. It can also be an escape sequence character like space ‘ ‘ or a null character ‘\o’  or  a new line ‘\n’  etc.


Example: ‘A’       ‘a’     ‘ b’      ‘8’     ‘#’  etc.


Each character has a corresponding ASCII value. ASCII value is the numeric code of a particular character and is stored inside the machine’s character set.


Note:- It is good that you read more about ASCII. There are basically two types of ASCII characters known as control characters and printable characters. Control characters are usually used to control a device using the program or to manipulate some logic inside a program. Control characters are not usually printed to an output device. Printable characters are usually printed to a display device or a printer.


String Constants


  1. A string constant is a collection of characters enclosed in double quotations “”
  2. It may contain alphabets, digits, special characters and blank space.



No comments: