Variables and Constants in C - PRsBlog
Home >> C Language >> Variables and Constants in C

Variables and Constants in C

Variables:

Variable is name of memory location which contain value of specific type.
Value of variable can be changed during execution of program.

Constants:

Fixed values which do not change during execution of program are called constants . constants in c can be categorized in following types.

Declaring constants:

1.  Decimal Integer constants:

const int i=12;
%d or %i format specifier is used to print decimal integer value with in printf function.

2. Octal integer constants:

const int i=012;

Any constant preceded by 0 is regarded as octal integer constant . To print octal value %o format specifier is used.

3. Hexadecimal constants:

const int i=0x12;

Any constant preceded by 0x or 0X is regarded as Hexadecimal Integer constant. To print Hexadecimal value %x format specifier is used in printf function.

4. Real constants:

const float a=3.14;
We can use %f,%e or %E format specifier within printf function to print real values.

5. Character constants:

const char ch=’d’;

Any constant enclosed in single quotes is character constants. %c format specifier is used to print single character .

6. String constants

const char[5]=”Ravin”;
Constant enclosed in double quotes is called String constant. We can use %s format specifier to print string value.

 

7. Escape Sequence:

Escape sequence is special character constant Which consistof two character and used with output function to print invisible pattern like newline and backspace
List of escape sequence :
‘\n’: Backspace
‘\b’: form feed
‘\f’: New line
‘\r’: carrige return
‘\t’: horizontal tab
‘\v’:vertical tab
‘\”: to print single quotes
‘\\’:to print back slash
‘\0’: null
‘\”‘: To print double quotes
Important :
If you try to reassign a value to constant , you will encounter compile time error saying “Cannot modify a const object”. For example see code below:

const int i=12; printf("%d",i); i=15; printf("%d",i);

About Log Raj Bhatt

A Computer Science Student and Java Developer.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest