Constants
A constant is a also identifier whose value can't be hanged during the program execution. In C
language we can declare constant using const
or #define
macro. Constants are also called as Literals. C Constants is divided into 2 categories.
- Numeric Constants
- Character Constants
Numeric Constants
Numeric constants are again divided into 2 types.
- Integer Constants
- Real constants / Floating point Constants
Integer constants accepts only integer data type values. They won't accept point values. They can be either 'positive or negative' constants are again divided into two categories.
- Infix int const
- Suffix int const
Infix integer constant indiates base of the value in which it has to be read.
Suffix int constAn integer literal(constant) can also have a suffix that is a combiantion of U and L, for unsigned and long, respectively.
Example:- 123 /* By default it is consider has integer */
- 123U, 123u /* It means unsigned integer */
- 123l, 123L /*Long integer constant */
- 123ul /*Unsigned long integer constant */
- 078 /* Illegal: 8 is not octal digit */
- 032UU /* Illegal: cannot repeat a suffix */
- 85 /* Decimal */
- 0213 /* Octal */
- 0x4b /* hexadecimal */
- 30 /* Int */
- 30u /* unsigned int */
- 30l /* long */
- 30ul /* unsigned long */
Real constants also known as floating point constants. These are having fractional part either positive or negative. These real constant again for 2 forms.
- Decimal form: Decimal form it must contain decimal part.
Example:3.423, -8.964, 8, 93 - Exponential form: Generally real or floating point constants are represented by using a format Mantissa
Mantissa: The part appearing before e, Mantissa is a real number expressed in decimal notation.
Exponent:Exponent is an integer number, it is positive or negative - Letter 'e': Letter 'e' separation the mantissa and exponent that can be written in either lowercase (or) uppercase. Example:
0.65 x 104 = 0.65e4
12.5 x 10-2 = 12.5e-2
1.5 x 105 = 1.5e+5
3.18 x 103 = 3.18E3
-1.2 x 10-1 = -1.2E-1
Character constants are divided into 3 types:
- Single Character Constants: A character constants which has been specified with in single quotes. This character maybe Letter(a, b, A, B, etc...) or maybe digit(1 to 9) or maybe symbol(%, #, <, ? etc...)
- String Constants: A group of characters which are enclosed between double quotes are called as string constants.
ExampleHello
or5+10
. - Back slash Character Constant
- There are some characters which have special meaning in C language.
- They should be preceded by backslash symbol to make use of special function of them.
- Giuen below is the list of special characters and their purpose.
S.No | Escape sequence | Character represented |
1. | \a | ALert(bell, alarm) |
2. | \b | Backspace |
3. | \f | Form feed |
4. | \n | new line |
5. | \r | Carriage return |
6. | \t | Horizontal tab |
7. | \v | Vertical tab |
8. | \' | Single Quotation mark |
9. | \" | Double quotation mark |
10. | \? | Question mark |
11. | \\ | Backslash |
12. | \0 | Null character |
Variables
In programming, a variable is a container(storage area) to hold data. To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location.
Example:int playerscore = 95;Here, playerscore is a variable of int type. The int variable is assigned an integer value 95.
Rules for naming a variable:
- A variable name can only have letters (both uppercase and lowercase letters), digits and underscore.
- The first letter of a variable should be either a letter or an underscore. If you start with number it will not work as variable.
- The variable name should not be a keyword
- White space is not allowed.
- There is no rule on how long a variable name(identifier) can be. However, you may run into problems in some compilers if the variable name si longer than 31 characters.
- In C programming, we have to declare the variable before using it in the program.
How to declare a variable
The Variable declaration indicates that the operating system is going to reserve a piece of memory with that variable name.
Variable declaration
The syntax for variable declaration is as follows
type variable_name; (single variable declaration)or
type variable_name, variable_name, variable_name; (Multiple variable declaration)
Syntax
int a,b,c;int age;
char name;
Here a, b, c, age, name are variables. The int, char are the data types
Example program
int main() {
/* variable declaration */
int height, width;
height = 20;
width = 20;
printf("Value of height = %d \n value of width = %d", height, width);
return 0;
}
output
Value of height = 20
Value of width = 20