Constants -A constant is an identifier whose value cannot be changed throughout the execution of a program whereas the variable value keeps on changing. In C there are four basic types of constants. They are:
Numeric Constant:
1. Integer constants
2. Floating point constants
String or Character Constants:
1. Single character string constant / Character constants
2. String of characters constant / String constants
Numeric constants:
1. Integer Constant – An integer constant is signed or unsigned whole number.
Ex: - 34, -48, +45 etc.
Ex: - 34, -48, +45 etc.
2. Real or Floating Point Constant – An signed or unsigned number with fractional part is called real or floating point constant.
Ex: - 0.45, -5.64, 0.45e2 etc.
where e = exponential.
Ex: - 0.45, -5.64, 0.45e2 etc.
where e = exponential.
Rules to form Integer and Floating Point Constants
• No comma or blank space is allowed in a constant.
• It can be preceded by – (minus) sign if desired.
• The value should lie within a minimum and maximum permissible range decided by the word size of the computer.
String or Character constants:
1. Single character string constant / Character constants – Any letter or character enclosed in single apostrophe is called Single character string constant / Character constant.
Ex: - ‘y’ ‘$’ ‘T’ ‘3’
Ex: - ‘y’ ‘$’ ‘T’ ‘3’
2. String of characters constant / String constants – Any string of characters consisting of letters, digits and symbols enclosed in double quotes is called String of characters constant / String constants.
Ex:- “Total Amount” “Jail-420” “Average=”
Ex:- “Total Amount” “Jail-420” “Average=”
Symbolic constants - Symbolic Constant is a name that substitutes for a sequence of characters or a numeric constant, a character constant or a string constant. When program is compiled each occurrence of a symbolic constant is replaced by its corresponding character sequence. The syntax is as follows:
#define name text
where name implies symbolic name in caps. text implies value or the text.
For example:
#define printf print
#define MAX 100
#define TRUE 1
#define FALSE 0
#define SIZE 10
The # character is used for preprocessor commands. A preprocessor is a system program, which comes into action prior to Compiler, and it replaces the replacement text by the actual text. This will allow correct use of the statement printf.
Advantages of using Symbolic Constants are:
• They can be used to assign names to values
• Replacement of value has to be done at one place and wherever the name appears in the text it gets the value by execution of the preprocessor. This saves time. if the Symbolic Constant appears 20 times in the program; it needs to be changed at one place only.
Variables: - A variable is an identifier or a name which is used to refer a value and this value varies or changes during the program execution. A variable is written with a combination of letters, numbers and special character _ (underscore) with the first letter being an alphabet. Maximum of 31 letters can be used to write a variable.
Ex: - c, fact, h34, total_amount etc
All variables have three essential attributes:
• the name
• the value
• the memory, where the value is stored.
For example, in the following C program a, b, c, d is the variables but variable eis not declared and is used before declaration. After compiling the source code and look what gives?
main ( )
{
int a, b, c;
char d;
a = 3;
b = 5;
c = a + b;
d = ‘a’;
e=d;
……….
……….
}
After compiling the code, this will generate the message that variable e not defined.
Note the following points while writing a variable in C language:-
• Upper and Lower case alphabets are taken differently, so the variable SUM and sum are referring to different value.
• No special characters other than underscore ( _ ) are permitted.
• Some C compliers will not accept more than 8 characters.
• All variables used in a C program are declared with appropriate data types before the variable is assigned any value.
• Reserved words cannot be used as variables.
Variable Declaration- Before any data can be stored in the memory, we must assign a name to these locations of memory. For this we make declarations. Declaration associates a group of identifiers with a specific data type. All of them need to be declared before they appear in program statements, else accessing the variables results in junk values or a diagnostic error. The syntax for declaring variables is as follows:
data- type variable-name(s);
For example,
int a;
short int a, b;
int c, d;
long c, f;
float r1, r2;
Initializing variables- When variables are declared initial, values can be assigned to them in two ways:
1. Within a Type declaration
The value is assigned at the declaration time.
For example,
int a = 10;
float b = 0.4 e –5;
char c = ‘a’;
2. Using Assignment statement
The values are assigned just after the declarations are made.
For example,
a = 10;
b = 0.4 e –5;
c = ‘a’;
Basic Data Type – There are four basic data types in C language. They are given below along with number of bytes occupied by them in the computer memory (RAM).
Data Type | Bytes Occupied in RAM |
char | 1 byte |
int | 2 bytes |
float | 4 bytes |
double | 8 bytes |
char – It refers to character. It can hold one letter/symbol. Char in C language is associated with integers to refer a letter/symbol as per ASCII which has assigned integer value for all letters/symbols used in programming.
int – It refers integer. It can hold a signed or unsigned or unsigned whole number within the specified range.
float – It refers floating point or real number. It can hold a real number like 3.245453 or 5.87e3 with six decimal digits in decimal or exponential form. A float number using 6 decimal digits is called a single precision number.
double – It also refers to floating point or real number. It can hold a real number in double precision. A double precision number uses 12 decimal digits like 4.657835876582 or 3.67823574825e14.
Additional Data Types– There are some additional data type which is used in advance programming.
Data Type | Byte Occupied in RAM |
unsigned char | 1 byte |
short int | 1 byte |
unsigned short int | 1 byte |
unsigned int | 2 bytes |
long int | 4 bytes |
unsigned long int | 4 bytes |
long double | 10 bytes |
Note – unsigned is used when positive values are expected for a variable like age of a person.
Consider the following examples to declare variables in C program: -
• unsigned short int marks, age;
• unsigned int total, years;
• int m, amount;
• float x, y, z;
• double cd, pi;
• char sex, sname [20], address [50];
Like it ? Share it.



No comments:
Post a Comment