You are on page 1of 7

Chapter 3

Data and Data types:


i.
ii.
iii.
iv.

Integral types
Floating-point types
Character types
String types
Variables and variable declaration:
i.
ii.

Literals Vs Variable
Declaration for; constants, numeric types, chars and string types.

Data is the plural of datum, a single piece of information


Data type can be defined as the type of data of variable or constant store."When we use a variable in a
program then we have to mention the type of data. This can be handled using data type in C
C has 5 basic types of data, with specific sub-types as follows:
1. Integral types
Integral types are used for whole numbers without fractions, such as counting the number of
repetitions of a loop or the number of students in a class.
"int" is the most commonly used integral type, and will always match the natural word length of
the computer.
"long int" and "short int", ( also known as just "long" and "short" ) are variations of int that may
contain more or fewer bits than an ordinary int. Longs may be able to handle larger values than
regular ints, at the expense of more storage space required. Shorts may save on storage space, at the
expense of being able to handle only smaller integers.
The "char" type is technically an 8-bit int. Chars are normally used for storing character codes, but
can hold any integer within their range.
Integral types are normally signed numbers, but they may also be designated as "unsigned".
Unsigned integers cannot store any negative numbers, but their range of positive numbers is
double that of the equivalent signed versions. ( The total number of bits used is the same. Signed
numbers use roughly half their possible bit combinations for representing negative numbers, and
the other half for positive numbers, whereas unsigned numbers use all possible bit combinations for
non-negative values. ) For example, signed 16-bit integers can range from -32768 to + 32767, while
the unsigned version can range from 0 to 65,535.
2. Floating-point types
Floating-point data types represent numbers that may have fractional components, such as
temperatures or grade averages.
"float" is the most basic floating point type, and generally follows the IEEE standard for floating
point numbers.

Floating point numbers use some of their bits to store an exponent, which leaves fewer bits to store
the actual digits of the number. This gives them a much broader range of possible values than
integer types ( for the same number of bits ), but less precision, since fewer bits are available to
represent the digits of the numbers.
The data type "double" uses twice as many bits as the ordinary float, which gives it both a broader
range and more precision. double is the most commonly used floating point type for scientific and
engineering computations.
"long double" uses twice as many bits as doubles, and is used when very large ranges or very
precise values are needed.
3. Character types ( for single characters )
The "char" data type is basically an 8-bit integer, and can be used to store small numbers. ( signed
or unsigned. )
Most commonly the char type is used to store ASCII codes representing character data. For
example, the ASCII code for the capital letter 'A' is 65. Note that the character '9' is not the same as
the integer value 9. In this case the ASCII code for the character '9' is 57. The numerical value 9 in
the ASCII code set happens to represent a horizontal tab character.
4. Character strings
C can represent constant strings using double quotes, such as "Hello World".
The C language uses arrays of single characters to store character strings as variables.
5. Boolean data
Boolean values are used for logic and decision making. They may have the values of "true" or
"false". Boolean data may also be represented and/or printed using the values of 0 for false and 1
for true. Boolean data will be discussed more fully in the section on logic and decision making.
VARIABLES
A variable is a named storage location, where data may be stored and later changed.
An identifier is a more general term for a named location, which may contain either data or code.
o

Identifiers must begin with a letter or an underscore, preferable letters for user programs.

The remaining characters must be either alphanumeric or underscores.

Identifiers may be of any length, but only the first 31 characters are examined in most
implementations.

Identifiers are case sensitive, so "NUMBER", "number", and "Number" are three different
identifiers.

By convention ordinary variables begin with a lower case letter, globals with a Single
Capital, and constants in ALL CAPS.

Multi-word variables may use either underscores or "camel case", such as


"new_value" or "newValue".

Integers are usually assigned variable names beginning with the letters I, J, K, L, M, or N,
and floating point variables are usually assigned names beginning with other letters.
Identifiers may not be the same as reserved words.

All variables must be declared before they can be used.


In C, all variables must be declared before the first executable statement of the program.

Variables may be given an initial value at the time they are declared. This is called "initialization", or
"initializing the variables".
o

Initialization may be performed using either parentheses or equals signs.

Example: double x1( 0.0 ), x2 = 0.0;

UNINITIALIZED VARIABLES ARE DANGEROUS, AND SHOULD BE CONSIDERED


TO HOLD RANDOM VALUES.

Variables may be declared "const", meaning that their values cannot be changed.
o

const variables MUST be initialized at the time they are declared.

By convention, const variables are named using ALL CAPS.

Examples:
const double PI = 3.14159;
const int MAXROWS = 100;

A local variable is a variable which is either a variable declared within the function or is an argument
passed to a function.
A global variable is a variable which is accessible in multiple scopes. It is important to note that global
variables are only accessible after they have been declared.
#include <iostream.h>
double w; -------------global variable
int main() {
int i; -------------------local variable
return 0;
}
Literals and constants are significantly different things in C. It can be said that the term literal in C stands
for unnamed object that occupies memory (literals are typically lvalues), while the term constant stands for
(possibly named) value that does not necessarily occupy memory (constants are rvalues).

Difference between the definition and declaration of a variable in c?


Definition defines the memory area ( allocates the memory ) for the variable and the declaration tells about
the signature of the variable ( type and size to be considered). definition occures once through the
program( memory is allocated once ), but the declaration can occur many times.
OR For a variable, the definition is the statement that actually allocates memory. For example, the statement:

Line 1: #include<stdio.h>
Line 2:
Line 3: void main()
Line 4: {
Line 5: int x; //declaration
Line 6:
Line 7: x=10; //definition
Line 8: }
Type Conversions

There are certain cases in which data will get automatically converted from one type to another:
o

When data is being stored in a variable, if the data being stored does not match the type of the
variable.
The data being stored will be converted to match the type of the storage variable.

When an operation is being performed on data of two different types.


The "smaller" data type will be converted to match the "larger" type.
For example, when an int is added to a double, the computer uses a double version of the int and the result is
a double.
o

When data is passed to or returned from functions.

Data may also be expressly converted, using the typecast operator


o

The following example converts the value of total to a double precision value before
performing the division:
average = ( double ) total / nStudents;

Note that total itself is unaffected by this conversion.

ENUMERATED TYPES

Enumerated ( enum ) data types are basically ints, except that they are restricted to a limited set of
values, and those values are referred to by name not by number.

The use of enums where applicable helps make code more readable and also limits the possibilities
for bad values, thereby reducing bugs and making the code more maintainable and overall better.

The enum keyword is used to define a new data type, having a new data type name and list of
acceptable named values.

Once the new enum type has been declared, variables can be declared of the new type, and assigned
the named values.

For example:
enum SizeType { small, medium, large }; // Declares a new data type, "SizeType"
SizeType item; // Declares a variable of type "SizeType"
// ( Some code left out here. )
if( num < 25 )
item = small; // Use as an int, using the named values instead of numbers
cout << "\nThe item is ";
switch( item ) {
case small:
// Named values are valid integers
cout << "tiny\n";
break;

Named values can be assigned specific numbers. Those not assigned will get successive values. So in
the following example, minor2 will have the value 2 and major2 will have the value 101:
enum errorType { none = 0, minor1 = 1, minor2, major1 = 100, major2, fatal1 = 1000 };

Enumerated type variables can also be initialized.. For example:


errorType errorCode = none;
sizeType bookSize = large;

It is sometimes a good idea to include values such as "invalid", "undefined" or "none" among the list
of enumerated values.

Some compilers may allow using enum variables with ordinary integers, ( e.g. using numbers instead
of names ), but it is poor practice.

Printing enumerated variables prints the assigned integer value.

One should not attempt to do any math using enumerated variables.

CONSTANTS

Constants are specific numbers ( or characters or strings ) that have a specific value which cannot be
changed.

Floating-point constants are normally indicated by the presence of a decimal point, and are normally
doubles.

Single characters are enclosed with single quote marks, such as 'A'

Character strings are enclosed with double quote marks, such as "Please enter the first number > "

Special characters, as either single characters or within a string:


o \n - New line character.
o \t - Horizontal tab.
o \g - Bell

C has different data types for different types of data and can be broadly classified as :

1.

Primary data types

2.

Secondary data types

Format specifier
The Format Specifier tells printf() where to put a value in a string and what format to use in printing the
value.
Here are some of the format specifiers:
Keyword

Format Specifier

Size

char

%c

1 Byte

unsigned char

<-- -- >

8 Bytes

int

%d

2 Bytes

long int

%ld

4 Bytes

unsigned int

%u

2 Bytes

float

%f

4 Bytes

double

%lf

8 Bytes

long double

%Lf

12-16 Bytes

Lets try format specifiers in our code:

#include<stdio.h>
#include<conio.h> //without this, you cannot use getch()
void main(void)
{
printf("My name is %s","Muzammil");
printf("I got %c grade in Intermediate",'A');
printf("I am %d years old",19);
getch();
}
QUALIFIER : When qualifier is applied to the data type then it changes its size or its size.
Size qualifiers : short, long
Sign qualifiers : signed, unsigned
TYPEDEF : It is used to create new data type. But it is commonly used to change existing data type with
another name.
Syntax:
typedef [data_type] synonym;
OR
typedef [data_type] new_data_type;
Example:
typedef int integer;
integer rno;

You might also like