Day 8 : Various tyes of Constant Varaible Declaration in c lang.

Various tyes of Constant Varaible


Assignments for solve 


1. /* WAP to input temperature in terms of Fahrenheit and convert it to degree Celsius */

2. /* Ass: WAP to input temperature in Degrees Celsius and convert it to Fahrenheit */

3. /*  WAP to input the Radius of a circle and Calculate its Area */

4. /* Ass: WAP to input the Base and Height of a triangle. Calculate and Display its Area */

5. /* WAP to input distance between two cities in kilometers and convert to meters, centimeters, 

   inches, and in feet */

6. /* Ass: WAP to input the Storage Capacity of the computer system in terms of gigabytes and convert it to

   Megabytes, kilobytes, bytes, and in bits 

   

    1 gb = 1024 mb

    1 mb =       1024 kb

    1 kb = 1024 byte

    1 byte = 8 bits

   */


Constant Varaible Declaration


C Library provides the following keywords to define a value as constant

i.e. value once initialized can not be modified.


   In the C programming language, the const keyword is used to declare a constant variable.

   A constant variable is a variable whose value cannot be modified once assigned.

  It often defines values that should be kept the same throughout the program execution.

1. using the "const" Keyword

const <Datatype> <Var_Name> = <value>;

e.g.

const float pi = 3.14f;

- const keyword requires type qualifier (Datatype)

2. using "#define"

In C, #define is a preprocessor directive to define constants or macros. It allows you to define a name for a value or a piece of code that will be replaced by the preprocessor before the actual compilation takes place.


#define <Var_Name> <Value>

e.g.

#define pi 3.14

- #define not require any type qualifier (Datatype)

3. using the "enum" keyword

If there are more constant definitions then the set of constant

values are called enumeration. and enumeration can be created using 

a keyword enum.

enum <Enum_Name>

{

<var>=<value>, <var_name>=<value>

};

e.g.

enum Month

{

jan=31,feb=28,march=31,.... n

}

   

                  


Post a Comment

Previous Post Next Post