Constants, Variables, Keywords, Data Types
C Tokens
(Constants, Variables, Keywords, Data Types):
A constant is a
quantity that doesn’t change. This quantity can be stored at a locations in the
memory of the computer. A variable also can be stored in the memory of the
computer. But the contents of the variable can be changed.
Types of C
Constants:
C constants can
be divided into two major categories:
1.
Primary Constants
2.
Secondary Constants
1.
Primary Constants we learn Integer
Constants, Real Constants, Character
Constants and
2.
Secondary
Constants we learn Array, Pointers, Structures,
Union, Enum
etc.
Rules
for constructing Integer Constants:
a)
An
integer constant must have at least one digit
b)
It
must not have a decimal point.
c)
It
can be either positive or negative.
d)
If
no sign precedes an integer constant it is assumed to be positive.
e)
No
commas or blanks are allowed within an integer constant.
f)
The
allowable range for integer constants is -32768 to 32767.
Ex: 426
+782
-8000
-7605
/*
program for Integer no’s */
#
include<stdio.h>
void
main()
{
int
a, b,c;
clrscr();
c=a+b;
printf(“value
of c is %d”,c);
getch();
}
Rules for constructing
Real Constants:
a)
A real constant must have at least one
digit
b)
It must have a decimal point.
c)
It can be either positive or negative.
d)
If no sign precedes a real constant it
is assumed to be positive.
e)
No commas or blanks are allowed within
an real constant.
f)
The allowable range for real constants is -3.4e38 to 3.4e38.
Ex: +325.34
+782.0
-80.76
-760.578
#include <stdio.h>
int main (void)
{
/* declarations */
int a;
double x;
/* executable statements */
a = 1000;
x = 100.583665;
printf ("%d\n", a);
printf ("%3d\n", a);
printf ("%4d\n", a);
printf ("%5d\n", a);
printf ("\n");
printf ("%lf\n", x);
printf ("%15lf\n", x);
printf ("%15.4lf\n", x);
printf ("%18.2lf\n", x);
printf ("%12.0lf\n", x);
return (0);
}
Rules for
constructing Character Constants:
a)
A character
constant is a single alphabet, a single digit or a single special symbol
enclosed within single inverted commas.
b)
The maximum
length of a character constant can be 1 character.
Ex:
‘A’
‘I’
‘5’
‘=‘
/
* inputting characters
A program that asks the user for 4 characters and displays them on the screen.
A program that asks the user for 4 characters and displays them on the screen.
#include <stdio.h>
int main (void)
{
/* declarations */
char letter1, letter2, letter3, letter4;
printf ("enter a name: ");
scanf ("%c%c%c%c", &letter1, &letter2, &letter3, &letter4);
printf ("you entered: %c%c%c%c", letter1, letter2, letter3, letter4);
printf (" / backwards: %c%c%c%c\n", letter4, letter3, letter2, letter1);
return (0);
}
output:
enter a name: lucky
you entered : lucky
C Keywords:
Keywords are
the words whose meaning has already been explained to the C compiler. The
keywords cannot be used as variable names. There are only 32 keywords
available in C.
auto
|
double
|
if
|
static
|
break
|
else
|
int
|
struct
|
case
|
enum
|
long
|
switch
|
char
|
extern
|
near
|
typedef
|
const
|
float
|
register
|
union
|
continue
|
far
|
return
|
unsigned
|
default
|
for
|
short
|
Void
|
do
|
goto
|
signed
|
While
|
Data types in C
- Fundamental Data Types
- Integer types
- Floating type
- Character type
- Derived Data Types
·
Arrays
·
Pointers
·
Structures
Data types in c refer to an extensive system used for declaring
variables or functions of different types. The type of a variable determines
how much space it occupies in storage and how the bit pattern stored is
interpreted.
The types in C can be classified as follows −
S.N.
|
Types & Description
|
1
|
Basic Types
They are arithmetic types and are further classified into: (a) integer
types and (b) floating-point types.
|
2
|
Enumerated types
They are again arithmetic types and they are used to define variables that
can only assign certain discrete integer values throughout the program.
|
3
|
The type void
The type specifier void indicates that no value is available.
|
4
|
Derived types
They include (a) Pointer types, (b) Array types, (c) Structure types,
(d) Union types and (e) Function types.
|
The array types and structure types are referred collectively as the
aggregate types. The type of a function specifies the type of the function's
return value. We will see the basic types in the following section, where as
other types will be covered in the upcoming chapters.
Integer Types
The following table provides the details of standard integer types with
their storage sizes and value ranges –
Type
|
Storage size
|
Value range
|
char
|
1 byte
|
-128 to 127 or 0 to 255
|
unsigned char
|
1 byte
|
0 to 255
|
signed char
|
1 byte
|
-128 to 127
|
int
|
2 or 4 bytes
|
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
|
unsigned int
|
2 or 4 bytes
|
0 to 65,535 or 0 to 4,294,967,295
|
short
|
2 bytes
|
-32,768 to 32,767
|
unsigned short
|
2 bytes
|
0 to 65,535
|
long
|
4 bytes
|
-2,147,483,648 to 2,147,483,647
|
unsigned long
|
4 bytes
|
0 to 4,294,967,295
|
To get the exact size of a type or a variable
on a particular platform, you can use the sizeof operator. The
expressions sizeof(type) yields the storage size of the object or type
in bytes. Given below is an example to get the size of int type on any machine
−
#include <stdio.h>
#include <limits.h>
int main() {
printf("Storage size for int : %d
\n", sizeof(int));
return 0;
}
When you compile and execute the above
program, it produces the following result on Linux −
Storage size for int : 4
Floating-Point Types
The following table provide the details of standard floating-point types
with storage sizes and value ranges and their precision −
Type
|
Storage size
|
Value range
|
Precision
|
float
|
4 byte
|
1.2E-38 to 3.4E+38
|
6 decimal places
|
double
|
8 byte
|
2.3E-308 to 1.7E+308
|
15 decimal places
|
long double
|
10 byte
|
3.4E-4932 to 1.1E+4932
|
19 decimal places
|
The header file float.h defines macros that
allow you to use these values and other details about the binary representation
of real numbers in your programs. The following example prints the storage
space taken by a float type and its range values −
#include <stdio.h>
#include <float.h>
int main() {
printf("Storage size for float : %d
\n", sizeof(float));
printf("Minimum float positive value:
%E\n", FLT_MIN );
printf("Maximum float positive value:
%E\n", FLT_MAX );
printf("Precision value: %d\n",
FLT_DIG );
return 0;
}
When you compile and execute the above
program, it produces the following result on Linux −
Storage size for float : 4
Minimum float positive
value: 1.175494E-38
Maximum float positive
value: 3.402823E+38
Precision value: 6
The void Type
The void type specifies that no value is
available. It is used in three kinds of situations −
S.N.
|
Types & Description
|
1
|
Function returns as void
There are various functions in C which do not return any value or you
can say they return void. A function with no return value has the return type
as void. For example, void exit (int status);
|
2
|
Function arguments as void
There are various functions in C which do not accept any parameter. A
function with no parameter can accept a void. For example, int rand(void);
|
3
|
Pointers to void
A pointer of type void * represents the address of an object, but not
its type. For example, a memory allocation function void *malloc( size_t
size ); returns a pointer to void which can be casted to any data type.
|
Comments
Post a Comment