SHARE

Variables and Constants in c:

Variables and Constants in c are crucial topics to understand in c language because lot of people here are beginning programming. These Variables and Constants in c are tokens which represent contents r flowing part of program.

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.

Variables

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.

[/crayon]
 

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:

[/crayon]
 

When you compile this code you will get error.

I hope you have got clear idea about variables and constants after reading this post. If you have any query comment below and take part in discussion.

Facebook Comments

comments