Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Constants:

In C programming, constants are fixed values that cannot be altered during program execution. They are typically used to represent values that remain constant throughout the program. Constants can be categorized into two types:

  1. Literal Constants: These are fixed values like numbers or characters.

  2. Symbolic Constants: These are identifiers that represent constant values.

Here's how you can define and use constants in C:

Literal Constants:

Integer Constants:

int num = 10; // Integer constant

Floating-point Constants:

float pi = 3.14; // Floating-point constant

Character Constants:

char letter = 'A'; // Character constant
Symbolic Constants:

Symbolic constants are defined using the #define directive or the const keyword.

Using #define Directive:

#define MAX_VALUE 100 // Define a constant named MAX_VALUE with value 100

Using const Keyword:

const float PI = 3.14159; // Define a constant PI with value 3.14159
Example:
#include #define MAX_VALUE 100 // Symbolic constant using #define int main() { const int MIN_VALUE = 0; // Symbolic constant using const printf("Maximum value: %d\n", MAX_VALUE); printf("Minimum value: %d\n", MIN_VALUE); return 0; }

In this example:

Constants are useful for making code more readable, maintainable, and less error-prone. They provide a way to give meaningful names to values used in the program and make it easier to update these values if needed.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java