Constants in c

                       
 In this blog 
 constant
 Types of constant
 Integer constant
 floating point constant
 character constant
 string constant
 Declaring constant


Constant 

constants are indentifiers whose value is defined by the programmer at compiler time , its value can't be changed  if you try to change the value of a constant then it will give you error.



Types of constant 




Integer constant

an integer constant is type of constant of int type .

eg :  3412

if you want to make it unsigned place U or u as a suffix , l or L for long , UL or ul for unsigned long 

eg : 31345ul    (is integer of unsigned long)

if you want to make it ocatal or hexadecimal , use prefix 0  or (0x , 0X)  respectively

eg: 07         (is integer of type octal)          0x7   (is integer of type hexadecimal)

by default it is +ve if you want to make it -ve place -ve sign.

eg : -8l       (is  -ve integer of long type)



floating point constant

floating point constant is type of float . A floating point constant consist of a integer part, decimal part and exponent part , it is not necessary that it will contain all these part 

eg 7.54    ,   7 e+2  ,   3.54E-3

by default the floating point constant in +ve until you put  -ve sign in front of it.

eg :  -7.54 is -ve

by default all floating point constant is of type double until you put f or F (float ) and l or L (long)
eg :  7.54 ( dobule )  ,   7.54f (float)   , 7.54L  (long)


character constant 

char constant are single character that are enclose in single quotes (eg :  'a' ) , in memory character are stored in ascii values .



string constant

string constant are set of characters which are enclosed in double quotes (eg : "a"  or "alpha") in string constant compiler automatically adds the null character('\0' ) at the end of string , so size of string "alpha" is 6 not 5 bytes.


Declaring constant

there are 2 methods for declaring constant 

1) using keyword const
2)  using #define (preprocessor directive)

const int a=5 (would initialize variable a with 5 )

#define pi 3.14 (here pi is not a variable it's just simple text)

Diffrence between using const keyword and using #define is  that the 1st method can had varied scope but the 2nd is only global scoped .

Back to top(leave the comment down below if you have a doubt or suggestions ,thnks🙂)

No comments:

Post a Comment

Contact Form

Name

Email *

Message *