Wednesday, 13 December 2017

C Constants

c constants

C Constants

C constants are nothing but the one type of variables which values cant be change during the program execution. C constants  treat like variables but key difference is that its value doesn’t change.
Like variables c constants can be of many data types such as int,float,char etc.

C defines  some rules to define constants these are given below-

Rules to for naming c constants:


  1. A constants name can have any letters , digits and underscore .
  2. The first letter of constants must be  a letter .
  3. Generally It is good programming  to type constants name in upper case(capital letters). But however there is no restriction to type in uppercase.
  4. Upper case and lower case treated as different. It is case sencetive.

In c we can define constants in many ways. These are given below

Defining constant using const Keyword:

By using const keyword we can declare constant. Const keyword is used as prefix,

Syntax

[
const data type  constant_name;
]

for example:




#include <stdio.h>
int main() {
   const int  NUMBER1= 20;
   const int  NUMBER2 = 10;
   const char NEW_LINE = '\n';
   int addition;
 
   addition = NUMBER1+NUMBER2;
   printf("addition is : %d", addition);
   printf("%c", NEW_LINE);
   return 0;
}


Output:

Addition is:30

Defining constant using #define

In c it is an also simplest way to define constant using the #define keyword. This type of constant doesn’t need to declare their data type. The are declared after the declaring header files.

Syntax:

[
#define constant_name value

]

For example:

[
#include <stdio.h>

#define NUMBER1 10  
#define NUMBER2  20
#define NEW_LINE '\n'

int main() {
   int addition; 
 
   addition=NUMBER1+NUMBER2;
   printf("Addition is : %d", addition);
   printf("%c", NEW_LINE);

   return 0;
}

]

Output:

Addition is:30


Literals:

Following are the different literals which can be used as constants.

1. Integer literals

Integer literals are the literals which only stores the numerical values without any floating point.integer literals are of three types:

  • decimal literals:  decimal literals means which numbers has base 10. For ex. 22,-9 25 etc.
  • octal literals:   octal literals having base 8. These literals start with 0. For ex. 0236,023,014 etc.
  • hexadecimal  literals:  hexadecimal literals hiving base 16.  These literals start with 0x or 0X.

2. Floating-point literals

A floating point literal is nothing but numeric value that has either a fractional form or an exponent form. For example:

-5.7
0.0000934
-0.32E-5

3. Character literals

A character literal is nothing but  constant which having single quotation around single character. For example: 'M', 'l', 'Y', 's'

Enumeration constants

 This is another most important type of constants in c.enum keyword is used to define enumeration types constants .

 For example:

enum colors {yellow, blue, black, pink};

Here, colors is a variable  of enumeration type and yellow, blue, black, pink are the enumeration constants having value 0, 1, 2 and 3.

               





Share This
Previous Post
Next Post

Admin and author of BestBCANotes. Complted master degree in Computer Science.

0 comments: