Search This Blog

Blog Inauguration By : Prof. N. L. Vijaykumar, INPE - Brazil

CSE

Constants

Constants in c

In C programming language, a constant is a fixed value that does not change during program execution. Constants can be used in place of literals in a program to make the program more readable and easier to maintain. 

They are declared using the const keyword or using preprocessor directives such as #define.

Constants can be of various types such as integer constants, floating-point constants, character constants, string constants, enumerated constants, and symbolic constants. Once a constant is defined in a program, its value cannot be changed.


For example, consider the following code snippet:

const int MAX_VALUE = 100;

#define PI 3.14159

int main()

 {

    int radius = 5;

    float area = PI * radius * radius;

    if (area > MAX_VALUE) {

        printf("Area is too large\n");

    }

    return 0;

 }

In this code snippet, MAX_VALUE and PI are constants. MAX_VALUE is an integer constant declared using const keyword and PI is a symbolic constant declared using #define. The value of these constants does not change during program execution. The constant MAX_VALUE is used to compare the area of a circle with a maximum allowed value, and PI is used to calculate the area of a circle.

Integer constants:

Syntax: 

[base]number

Definition: Integer constants are whole numbers without decimal points. They can be represented in decimal, octal, or hexadecimal notation.

Example:

Decimal integer: 42

Octal integer: 053

Hexadecimal integer: 0x2A

Floating-point constants:

Syntax: 

[number][e|E][exponent]

Definition: Floating-point constants are real numbers with a decimal point. They can be represented in standard or exponential notation.

Example:

Standard notation: 3.14

Exponential notation: 1.5e2

Character constants:

Syntax:

'char'

Definition: Character constants are single characters enclosed in single quotes.

Example:

Character: 'a'

Digit: '5'

String constants:

Syntax: 

"string"

Definition: String constants are a sequence of characters enclosed in double quotes.

Example: "Hello, World!"

Enumerated constants:

Syntax: 

enum enum_name { constant1, constant2, ..., constantN };

Definition: Enumerated constants are user-defined constants that represent integer values.

Symbolic constants:

Syntax

#define constant_name value

Definition: Symbolic constants are user-defined constants that represent values that do not change during program execution.


No comments:

Post a Comment