Sunday, 24 November 2013

Constant and Literals in c

The constants refer to fixed values that the program may not change during its execution. These values are also called literals.
C Programming
C Programming


Constants can be of any of the  data types like an integer , a floating , a character , a string literal and an enumeration constants as well.

The constants are consider just like regular variables except that their values can't be modified after their definition.

Integer literals:

An integer literal can be of any number system 1.e a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix are :
1.0x or 0X for hexadecimal,
2.0 for octal.
3.and nothing for decimal.

An integer literal can also have a combination of  unsigned(U) and long(L), respectively. The suffix can be  in uppercase or  in lowercase and can be in any order.

 some examples of integer literals are:

2123         /* Legal */
2145u        /* Legal */
0xFeeL      /* Legal */
0718          /* Illegal: 8 is not an octal digit */
032UU      /* Illegal: cannot repeat a suffix */

 The various types of Integer literals are:
85             /* decimal */
0213         /* octal */
0x4b         /* hexadecimal */
30             /* int */
30u           /* unsigned int */
30l            /* long */
30ul          /* unsigned long */

Floating-point literal:

Floating-point literals must contain a decimal point,an integer part, a fractional part, and an exponent part.Floating point literals can be represent either in decimal form or exponential form.

When you  representing using decimal form, you must consider the decimal point, the exponent, or both and while using exponential form, the literal  must have  the integer part, the fractional part, or both. The signed exponent is introduced by e or E.
Example are:

3.14159           /* Legal */
314159E-5L    /* Legal */
510E                /* Illegal: incomplete exponent */
210f                 /* Illegal: no decimal or exponent */
.e55                 /* Illegal: missing integer or fraction */

Character constants:

Character literals are enclosed within single quotes, e.g., 'a' and can be stored in a variable of char type.

A character literal can be a plain character (e.g., 'a'), an escape sequence (e.g., '\n'), or a universal character (e.g., '\u02C0').

Some characters in C  are preceded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t). Following  are the some escape sequence codes:

Escape sequence             Meaning
\\                                   \ character
\'                                   ' character
\"                                   " character
\?                                  ? character
\a                                  Alert or bell
\b                                  Backspace
\f                                   Form feed
\n                                  Newline
\r                                   Carriage return
\t                                   Horizontal tab
\v                                  Vertical tab
\oo                                Octal number of one to two digits
\xhh . . .                         Hexadecimal number of one or more digits
Example using escape sequence:

#include <stdio.h>

int main()
{
   printf("Hey\tWorld\n\n");

   return 0;
}
Out put:

Hey  World

String literals:

String literals or constants are enclosed within a double quotes "".

You can also break a long line into multiple lines using string literals and separating them using whitespaces.

Some examples of string literals are:

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

Defining Constants

The two simple ways to define a constant in are:

1.Using  preprocessor (#define):

2.Using const keyword.

The #define Preprocessor:
 the form to use #define preprocessor to define a constant is:

#define identifier value
example :

#include <stdio.h>

#define LENGTH 10  
#define WIDTH  5
#define NEWLINE '\n'

int main()
{

   int area; 
 
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);

   return 0;
}
Output of the above c program is:

value of area : 50

The const Keyword:
You can use const keyword prefix to declare constants with a specific type :

const type variable = value;
 example:

#include <stdio.h>

int main()
{
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area; 
  
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);

   return 0;
}
Output:

value of area : 50
C - Overview
C - Basic Syntax
C - Data Types
C - Variables
C - Constants
C - Storage Classes
C - Operators
C - Decision Making
C - Loops
C - Functions
C - Scope Rules
C - Arrays
C - Pointers
C - Strings
C - Structures
C - Unions
C - Bit Fields
C - Typedef
C - Input & Output
C - File I/O
C - Preprocessors
C - Header Files
C - Type Casting
C - Error Handling
C - Recursion
C - Variable Arguments
C - Memory Management
C - Command Line Arguments

0 comments:

Post a Comment