C – Constants & Variables

After reading this constants and variables topic, you will understand its theory and examples and also you will also able to use them in C programming.

C constants value always fixed and do not change during the execution of the C program. C constants are also refereed as literals.

Constants Types :

The constants types in C language shown below:

1. Integer constant: It is a group of digits either from 0 to 9.

For example:

int a=500;

2. Octal: It is any combination of digits either from 0 to 7 and 0 (zero) at the beginning.

For example:

int a=058;

3. Hexadecimal: It is any combination of digits either from 0 to 9 and letters a,b,c,d,e,f respectively also 0x at the beginning.

For example:

int a=0x8f;

4. Real/float: It is any number containing a fractional part.

For example:

float a=1.54;

5. Single character: A single character enclosed in between pair of single quote marks (‘ ’).

For example:

char a=’k’;

6. String: – It is a group of character enclosed in double quotes (“ “).

For example:

char a[]=”LANGUAGE”;

7. Symbolic constant: It is defined by using #define directive.

For example:

#define PI 3.14;

8. Object constant: It is used to declare variable as constant during the execution of program.

For example:

int const a=5;

9. Backslash character constants: These constants having special meaning and made up of using backslash character (\) at the beginning with a single character. These are

ConstantMeaning
\aaudible alert (bell)
\\backslash
\bbackspace
\rcarriage return
\"double quote
\fform feed
\thorizontal tab
\nnew line
\0null
\?question mark
\'single quote
\vvertical tab

Variables

A variable is a name of storage area for holding data and variable data can vary during the execution of the program.

For example:

The statement in C is given by

int sum;

Indicate that

  • int represent integer data type.
  • sum is variable name.

Rules for defining Variable Name

  1. An alphabet or underscore used in the starting letter of variable name.
  2. A variable name cannot be keywords or standard function name.
  3. A variable name length is compiler dependent.
  4. Only underscore in special characters is allowed.
  5. Variable name is case sensitive i.e. uppercase letters and lower case letters are different.

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

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