INTRODUCTION
A constant is fixed entity. It does not change its value during the entire program execution. Constants can be classified as:
1. Integer Constant
An integer constant is an integer-valued number. It consists of a sequence of digits. Integer constants can be expressed in three ways. They are:
A Decimal Integer: A decimal integer constant can consist of any combination of digits which is taken from the set of 0 to 9 digits.
Example: 0 1 234 5654 etc.
An Octal Integer: An octal integer constant can consist of any combination of digits which is taken from the set of 0 to 7 digits. However, the first digit must be 0, in order to identify the constant as an octal number.
Example: 0 01 077
Hexadecimal Integer: A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from 0 to 9 and also through f (uppercase or lowercase).
Example: 0x 0x1 0x7ABC 0xabc
Unsigned and Long Constants: Unsigned integer constants exceed the magnitude of ordinary integer constants by approximately a factor of 2 and they should not be negative. Long integer constants need more memory. The letter appending L to the end of the constant can create long integer constant.
Example: 5000u decimal unsigned
1234567L decimal long
01234L octal long
0xffful hexadecimal unsigned long
2. Floating-Point Constants
A floating point constant is a base 10 number (decimal number) that contains either a decimal point or an exponent or both.
Example: 0.00 1.00 12.56 876.567
2E-8 0.006e-3 1.66e+8 .121e12
3. Character Constants
A character constant is a single character enclosed in single quotes (single quotation mark).
Example: ‘a’ ‘A’ ‘?’
4. Enumerated Constants
An enumeration is a user defined type with values ranging over a finite set of identifiers called enumeration constants. For example,
Defines color as a new type having three values red, blue and green. Each of these is an enumeration constant. Let us examine how it works.
color c; declares c to be of type color
c = blue;
printf (“As an int, C has the value %d\n”,c);
Will print,
As an int, c has the value 1
Similarly, we can define enum type of data as follows:
enum daysofweek { sun, Mon,Tue, Wed, Thu, Fri, Sat} ;
5. String Literals (String Constants): A string literal is a sequence of characters enclosed in double quotes. The characters may consist of letters, numbers, escape sequences and spaces.
Example:
“C is hard for learning” string literal
“ C” is hard for learning” string literal
A symbolic constant is a name that substitutes for a sequence of characters. The characters may represent a numeric constant, a character constant or a string constant. Hence, a symbolic constant allows a name to appear in place of a numeric constant, a character constant or a string.
Symbolic constants are generally defined at the beginning of the program. The symbolic constant can appear after this, in place of numeric constants, character constants,etc.
#define name text
Example:
#define PI 3.1416
#define TRUE 1
#define friend “Bhimsen”
The const qualifier defines the constant such that the compiler uses its value and it cannot be modified anywhere in the program. This can be achieved either by using #define preprocessor directive or using the const qualifier.
Example:
#include <stdio.h> void main () { const float tax_rate=0.05; float sal, tax; printf (“Salary: “); scanf (“%f”, &sal); tax = sal*tax_rate; printf (“\ntax =%f\n”, tax); } |
Now, let us to examine the same program using #define directive.
#include <stdio.h> #define tax_rate 0.05 void main () { float sal, tax; printf (“Salary: “); scanf (“%f”, &sal); tax = sal*tax_rate; printf (“\ntax =%f\n”, tax); } |
While executing the both programs, you will get the same result.
Every expression evaluates to a value. Every expression results in some value of a certain type that can be assigned to a variable. The data items that operators act upon are called operands. An operator is a symbol that operates on a certain data type. The operator generally remains between the two operands. An expression is a combination of variables, constants, and operators written according to the syntax of the language.
Based on their utility, the operators are classified into different categories in C. Some of the most common operators are listed below.
1. Arithmetic Operator
The arithmetic operators perform arithmetic operations and can be classified into unary and binary arithmetic operations. The arithmetic operators can operate on any built-in data type. A list of arithmetic operators and their meanings are given below:
Operator | Meaning |
+ - * / % | additional or unary plus subtraction or unary minus multiplication division modulo division (returns remainder after division) |
Note: for writing the power, there is no special operator. However, the function pow(x,y) exists in math.h which return x^x.
2. Relational Operator
The relational operators help to compare two similar quantities and depending on their relation, take some decisions. If the condition is true, it evaluates to an integer 1 and zero if the condition is false. The basic types of relational operator are:
Operator | Meaning |
< > <= >= == != | less than greater than less than or equal to greater than or equal to equal to not equal to |
In order to study the relational operators, it is desired to have knowledge of if statements. Here, if it is followed by the condition and the condition is built up from the relational operators in between the operands.
3. Logical Operator
The logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators.
An expression that contains both && and || is called compound expression.
4. Assignment Operator
One of the most common assignment operators is an equal sign. The character or value on the right is assigned to the variable on the left of equal to sign. The other forms of assignment operators are:
5. Increment and Decrement Operators
The increment and decrement operators are very commonly used in C language. The increment operators and decrement operators are extensively used in the loops using structures such as for, while, do, etc. The syntax of the operators is given below.
++<variablename> --<variablename> <variablename>++ <variablename)-- | preincrement predecrement postincrement postdecrement |
The preincrement operator increases the value of the variable by 1 and then the processing does whereas post increment first processes and increase the value of it by 1.
6. Conditional Operator
A conditional operator is very rarely used. This can be carried out with the conditional operator (? : ) An expression that makes use of the conditional operator is called a conditional expression. This can replace the if-else statement. The syntax of conditional operator is:
Expression_1 ? expression_2: expression_3
During evaluating the conditional expression, expression_1 is evaluated at the first step. If expression_1 is true (nonzero), then expression_2 is evaluated and this becomes the value of the conditional expression.
References:
Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 145-153.
Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd
ASK ANY QUESTION ON Constants
You must login to reply
String variables
String variables of c
Mar 03, 2017
0 Replies
Successfully Posted ...
Please Wait...