Ticker

6/recent/ticker-posts

Constants in C Programming - Variables

Constants 

Constant is a any value that cannot be changed during program execution. In C, any number, single character, or character string is known as a constant. A constant is an entity that doesn’t change whereas a variable is an entity that may change. For example, the number 50 represents a constant integer value. The character string "Programming in C is fun.\n" is an example of a constant character string. 

C constants can be divided into two major categories: 

  1. Primary Constants 
  2. Secondary Constants

These constants are further categorized as

C constants

Numeric constant 

Character constant 

String constant

Numeric constant: 

Numeric constant consists of digits. It required minimum size of 2 bytes and max 4 bytes. It may be positive or negative but by default sign is always positive. No comma or space is allowed within the numeric constant and it must have at least 1 digit. The allowable range for integer constants is -32768 to 32767. Truly speaking the range of an Integer constant depends upon the compiler. For a 16-bit compiler like Turbo C or Turbo C++ the range is –32768 to 32767. For a 32-bit compiler the range would be even greater. Mean by a 16-bit or a 32- bit compiler, what range of an Integer constant has to do with the type of compiler. 

It is categorized a integer constant and real constant. An integer constants are whole number which have no decimal point. Types of integer constants are: 

Decimal constant: 0-------9(base 10) 

Octal constant: 0-------7(base 8) 

Hexa decimal constant: 0----9, A------F(base 16) 

In decimal constant first digit should not be zero unlike octal constant first digit must be zero(as 076, 0127) and in hexadecimal constant first two digit should be 0x/ 0X (such as 0x24, 0x87A). By default type of integer constant is integer but if the value of integer constant is exceeds range then value represented by integer type is taken to be unsigned integer or long integer. It can also be explicitly mention integer and unsigned integer type by suffix l/L and u/U. 

Real constant

Real constant is also called floating point constant. To construct real constant we must follow the rule of  

  • Real constant must have at least one digit. 
  • It must have a decimal point. 
  • It could be either positive or negative. 
  • Default sign is positive. 
  • No commas or blanks are allowed within a real constant. Ex.: +325.34 426.0 -32.76 

To express small/large real constant exponent(scientific) form is used where number is written in mantissa and exponent form separated by e/E. Exponent can be positive or negative integer but mantissa can be real/integer type, for example 3.6*105=3.6e+5. By default type of floating point constant is double, it can also be explicitly defined it by suffix of f/F.

Character constant

Character constant represented as a single character enclosed within a single quote. These can be single digit, single special symbol or white spaces such as ‘9’,’c’,’$’, ‘ ’ etc. Every character constant has a unique integer like value in machine’s character code as if machine using ASCII (American standard code for information interchange). Some numeric value associated with each upper and lower case alphabets and decimal integers are as:

A------------ Z ASCII value (65-90) 

a-------------z ASCII value (97-122) 

0-------------9 ASCII value (48-59) 

;                     ASCII value (59) 

String constant 

Set of characters are called string and when sequence of characters are enclosed within a double quote (it may be combination of all kind of symbols) is a string constant. String constant has zero, one or more than one character and at the end of the string null character(\0) is automatically placed by compiler. Some examples are “,sarathina” , “908”, “3”,” ”, “A” etc. In C although same characters are enclosed within single and double quotes it represents different meaning such as “A” and ‘A’ are different because first one is string attached with null character at the end but second one is character constant with its corresponding ASCII value is 65. 

Symbolic constant 

Symbolic constant is a name that substitute for a sequence of characters and, characters may be numeric, character or string constant. These constant are generally defined at the beginning of the program as 

#define name value , here name generally written in upper case for example 

    #define MAX 10 

    #define CH ‘b’ 

    #define NAME “sony”

Variables

Variable is a data name which is used to store some data value or symbolic names for storing program computations and results. The value of the variable can be change during the execution. The rule for naming the variables is same as the naming identifier. Before used in the program it must be declared. Declaration of variables specify its name, data types and range of the value that variables can store depends upon its data types. 

Syntax: 

     int a; 

     char c; 

     float f; 

Variable initialization 

When we assign any initial value to variable during the declaration, is called initialization of variables. When variable is declared but contain undefined value then it is called garbage value. The variable is initialized with the assignment operator such as 

Data type variable name = constant; 

Example: int a=20; Or int a; a=20; 

ALSO SEARCH:

"constants in c programming"

"constants in c programming examples"

"constants in c programming language"

"types of constants in c programming"

"symbolic constants in c programming"

"define constants in c programming"

"character constants in c programming"

"how to declare constants in c programming"

"string constants in c programming"

"use of constants in c programming"

"example of constants in c programming"

"function of constants in c programming"

"definition of constants in c programming language"

"constants and variables in c programming"