• admin@embedclogic.com

Variable and Constant

Variable

Variable in C programming is basically an identifier which is used to fix a block in memory on the basis of its data type.

Here int, char, float are data types and these data types use to fix memory for variables.A variable name can be changed as per our choice.

Variable Declaration

Syntax:

Example :

Variable Initialization

One step ahead from variable declaration if we assign some value at the time of declaration then it is known as variable initialization.

Note:

In most of the storage class except extern, variables are initialized with some default or garbage data. In extern storage class, declared variable must be initialized to get memory.

Initialization is only considered if the value is provided at the time of declaration.

Sometimes initialization is must otherwise declare variable would not be facilitated with a memory during compilation(Example: extern variable, will look in storage class).

Rules to construct a Variable name

All the rules of identifier construction will imply in variable name construction because variable name is nothing but an identifier.

In C language variable name can use all 52 alphabets(upper and lower case), integers and only one symbol _(Underscore).

In C language The first character of the variable name should be always from alphabets or _(Underscore).(See Table example 1, 2, 3,4 ).

In C language variable name should be less than 31 characters.(See Table example 7 ).

In C language variable name must not be in C reserved keyword.(See Table example 8 ).

See the below table for some example –

Constant

As the name suggests Constant is an identifier that can not be changed in the whole program.A variable can also be constant in a program if it is declared with const keyword.

Constants are divided into following categories-

  1. Integer Constant
  2. Floating/real value Constant
  3. Character Constant
  4. String Constant
  5. Escape Sequence Constant

Integer Constant

  1. Integer Constants further divided into 3 categories:
  2. Decimal constants
  3. Octal Constants
  4. Hexadecimal constants

Decimal Constants  

All the numbers made by the combination of digits 0 to 9 is called decimal numbers like 10, 12039, 1253787289 etc.

If the decimal number is long then we can use l or L at the end of the number like 123637L or 12789l.

If the decimal number is unsigned then we can use u or U at the end of the number like 123637U or 12789u.

if the decimal number is long and unsigned both then we can use ul or UL at the end of the number like 123637UL or 12789ul.

Octal Constants

The octal numbers are made by the combination of digit 0 to 7.

Any number which has 0 in the prefix is considered as an octal number.

Example: 015,012,014,017 are not treated as a decimal number but it is considered as an octal constant in C language so be careful.

Hexadecimal Constants

The hexadecimal number made by the combination of following digits.

0,1 ,2 ,3,4,5,6,7,8,9,a,b,c,d,e,F

Example: 0x01, 0x04, 0x1F, 0x0A, 0x0C

Real value Constant

Constants with a decimal point or exponential are real or Floating constants.

Character Constant

Sequence of single character under single quote(‘ ‘) is a character constant.

String Constant

A sequence of characters inside the double quotation is a string constant.

Escape Sequence Constant

There are following escape Sequence Constant.

Escape SequenceCharacter
\tHorizontal Tab
\vVertical tab
\rCarriage return
\\Backslash
\bBackSpace
\fformfeed
\'Single quotation
\?Question mark
\"Double quotation

Subscribe and stay updated with our latest articles.