Professional Documents
Culture Documents
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.
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.
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.
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.
Variables may be given an initial value at the time they are declared. This is called "initialization", or
"initializing the variables".
o
Variables may be declared "const", meaning that their values cannot be changed.
o
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).
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.
The following example converts the value of total to a double precision value before
performing the division:
average = ( double ) total / nStudents;
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 };
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.
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 > "
C has different data types for different types of data and can be broadly classified as :
1.
2.
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
#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;