2. C-Constants

C-Introduction: Previous                                                                      Next: C-Basic Programs

What is a Constant in C language?

It is an element of a program which also stores some values as a variable stores but the difference between the variable and constant is that the variable`s value can be changed during the program while constant`s value remain fixed that means once a value is assigned in the beginning of the program cannot be changed.
Constant can be called as ‘Literal’.
E.g.: - Suppose we are making a program of calculating the circumference of a circle, the formula is 2∏ r. so the values are for ‘∏’ and ‘r’ i.e. radius.
Now, value of radius can be different according to the requirement, but the value of ∏ can`t be changed in any case. So, constant is needed to be defined for ∏.
E.g.: - const PI=3.14.
const’ keyword is compulsory and the value of a variable is needed to be assigned when we declare a constant variable.
Types of Constant: -
1. String constant
2. Numeric constant
3. Character constant
1. String constant: - It is a collection of alphanumeric character which must always be enclosed in double quotes (“---“). Maximum length can be stored in single string is 255 characters. String is always ended by a NULL symbol i.e. ‘\0’. The length of the string included this NULL character also.
E.g.: - string “Gaurav” has 6 characters but the length of this string is 7(included NULL character also).
Similarly, “Rs 2299.80”, “Hello! My World” etc.
2. Numeric constant: - It stores value in the form of number. Number can be positive as well as negative number. Numeric constant can be Integer, Floating, Octal, Hexadecimal constants.
4 types of Numeric constants are:- 

  • Integer constant - 9, 10, -2000.
  • Floating point constant - 3.8e, 0.8, 56.0.
  • Octal constant         - -125, 010.
  • Hex constant         - 0X14, -0X56.
3. Character constant: - String is a collection of characters while "character" is a single Alphanumeric. They are enclosed in a single quotation mark (‘-‘).
E.g.: - ‘A’, ‘1’, ’#’ etc.
Keywords: 
These are reserved words which are reserved by ‘C’ programming language. These words can`t be declared as a name of variable. It means the keyword ‘break, can`t be use in a program as a variable (int break ;)
32 keywords are available in C Programming:
  • auto     
  • default    
  • float
  • register   
  • struct
  • break
  • do
  • for
  • return   
  • switch    
  • case
  • double   
  • goto
  • short
  • typedef
  • char
  • else
  • if
  • signed
  • union
  • const
  • enum
  • int
  • sizeof     
  • unsigned
  • while
  • continue
  • extern
  • long
  • static
  • void
Identifiers:
These are names given to variables, arrays and functions etc. These are symbolic names and values of all variables are stored as symbolic names in memory. There are some naming conventions for identifiers. That can be called as naming conventions for variable names: -
Only allowable characters are from A to Z, a to z, 0-9.
No special characters are allowed except underscore ( _ ).
Note: First character of a variable can`t be digit.
Variables names are case sensitive. (ABC and abc both different)
E.g.: - num, num1, num_2, etc.
Type Declaration: - 
As name implies it suggest the type to be suitable to declare for a variable. Mainly they are known as Data Types. 

Because they defines the type of data that a variables will store during the program. Once a type of variable`s value is defined in a declaration then it can`t be changed during the execution of a program. Various types of Data Types are real, int, float, double etc.
Generally we have two categories of Data Types in C:
1. Primitive/Predefined/Built-In Data Type
2. Non-Primitive/User-Defined or Derived/Reference Data Type
Data type indicates:
Type of a value a variable may hold & How much space it will occupy in memory (in Bytes).
Syntax is : <data type> <variable>;
E.g.: int num1;
1. Primitive Data Type: -  
These are also known as Predefined or Built-In data type, because they are provided by ‘C’ language itself. Users have to only use them according to their need.
Basically they are of Four types :

  • int
  • char
  • float
  • double
int: - it used to store integer values means values without fractions or decimal.
It occupies 2 bytes in memory,
The format specifier is “%d”, and
The range is -32768 to 32767.
E.g.: - int a;
They are further divided into :
NameFormat SpecifierRange
unsigned int%u0 to 65535
long int%ld-2,147,483,648 to 2,147,483,647
short int %sh -32768 to 32767  
char: - this data type is used to store a single character in a variable.
It occupies 1 byte in memory.
The format specifier is “%c”, and
The range is -128 to 127.
E.g.: - char ‘a’;
float: - fractions values are not stored in int data type if needed. Suppose we want to store 123.55 in variable then           float data type can be used.
It occupies 4 byte in memory.
The format specifier is “%f”, and
The range is 3.4x10^38 to 3.4x10^38.
    E.g.: - float 52156.325485;
By default %f takes 6 places after decimal.
double: - it used to store fraction values in a large number of data.
It occupies 8 byte in memory.
The format specifier is “%Lf”, and
The range is 1.7x10-308 to 1.7x10308.
      E.g.: - double 515487321546.325485;

Note:  C does not support Boolean data type and it supports one more data type i.e. void. It means nothing is specified or nothing is  going to return. The difference between void and other data types is that we can`t use void data type for declaration of a variable.
2. Non-Primitive Data Type: -
It is called User defined or Derived data type also because they are defined according to user`s requirement.
They are Array, Functions, and Pointer etc.

C-Introduction: Previous                                                                              Next: C-Basic Programs