Menu
×
   ❮     
HOME Computer Fundamentals C Language C++ MS-OFFICE TALLY 11/12 SCIENCE 11/12 COMM BCA BCCA BBA MCA
     ❯   

Computer

▸ Computer Fundamentals▸ What is Computer▸ History of Computer▸ Types of Computer▸ History of C Language▸ History of C Language

Computer Components

▸ Computer components▸ Input Devices▸ Output Devices▸ CPU▸ Hardware

Computer Memory

▸ Computer Memory▸ Register Memory

Computer Virus

▸ Computer virus▸ Computer virus 2

Computer Network

▸ Computer Network details

Number Systems

▸ Number Systems

C Control Statements

▸ C if else▸ C Switch Statement▸ if-else vs switch▸ C Loops▸ C Do- While Loop▸ C While Loop▸ C For Loop▸ C For Loop▸ Nested Loops in C▸ Infinite Loop in C▸ C break▸ C continue▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ Type Casting

C Functions

▸ What is Function▸ Recursion in C▸ Call: Value & Reference▸ Call: Value & Reference▸ Storage Classes▸ Storage Classes

C Array

▸ 1 D - Array▸ Return an Array in C▸ Array To Function ▸ 2 - D Array▸ Return an Array in C

C Pointers

▸ C Pointers▸ C Double Pointer (Pointer to Pointer)▸ C Double Pointer (Pointer to Pointer)

C Tutorial

▸ Data Types in C▸ Keywords in C▸ C Identifiers▸ Escape Sequence in C▸ ASCII value in C▸ Constants in C▸ Static in C▸ Programming Errors in C▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Conditional Operator in C▸ Bitwise Operator in C▸ Bitwise Operator in C▸ 2s complement in C▸ What is C Language▸ History of C Language▸ Features of C Language▸ How to install C▸ First C Program▸ Compilation process in c▸ Printf Scanft▸ Variables in C ▸ C Operators▸ Comments in C▸ C Format Specifier▸ Literals in C▸ Tokens in C▸ C Boolean▸ Programming Errors in C

Constants in C ❮ Edit Details

In programming languages like C, constants are essential because they give you a mechanism to store unchanging values that hold true throughout the course of the program. These numbers may be used for several things, such as creating mathematical constants or giving variables set values. We will discuss the idea of constants in C, their syntax, how to declare and use them and give illustrated examples along with their anticipated results in this blog article. By the end of this article, you'll have a firm grasp of constants and their importance in C programming.

constant in C is a value that doesn't change as the program runs. Integers, floating-point numbers, characters, and strings are just a few of the several types of constants that may be employed. When a constant has a value, it cannot be changed, unlike variables. They may be utilized in various operations and computations and serve as the program's representation of fixed values.

Advantages of C Constants:

There are several advantages of C Constants. Some main advantages of C Constants are as follows:

  1. Programmers may use constants to provide names that have meaning to fixed numbers, which makes the code simpler to comprehend and update.
  2. Constants assist in avoiding the usage of magic numbers, which are hard-coded values, in the code. Instead, constants offer named representations of such values, enhancing the code's readability.
  3. Constants are reusable throughout the program, allowing for constant values in various locations and lowering the possibility of errors brought on by typos or inconsistent values.
  4. Calculations or processes inside the program can be optimized by using certain constants, such as mathematical or physical constants.
  5. A constant is a value or variable that can't be changed in the program, for example: 10, 20, 'a', 3.4, "c programming", etc.

There are different types of constants in C programming.

List of Constants in C

Constant Example
Decimal Constant 10, 20, 450 etc.
Real or Floating-point Constant 10.3, 20.2, 450.6 etc.
Octal Constant 021, 033, 046 etc.
Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.
Character Constant 'a', 'b', 'x' etc.
String Constant "c", "c program", "c in javatpoint" etc.

2 ways to define constant in C

There are two ways to define constant in C programming.

  1. const keyword
  2. #define preprocessor

1) C const keyword

The const keyword is used to define constant in C programming.

  1. const float PI=3.14;  

Now, the value of PI variable can't be changed.

  1. #include<stdio.h>    
  2. int main(){    
  3.     const float PI=3.14;    
  4.     printf("The value of PI is: %f",PI);    
  5.     return 0;  
  6. }     

Output:

The value of PI is: 3.140000

If you try to change the the value of PI, it will render compile time error.

  1. #include<stdio.h>    
  2. int main(){    
  3. const float PI=3.14;     
  4. PI=4.5;    
  5. printf("The value of PI is: %f",PI);    
  6.     return 0;  
  7. }     

Output:

Compile Time Error: Cannot modify a const object

2) C #define preprocessor

The #define preprocessor is also used to define constant. We will learn about #define preprocessor directive.

Types of constant:

There are different types of Constants in C. Some of them are as follows:

Decimal Constant

A whole number represented in base 10 is known as a decimal constant. It has digits that range from 0 to 9. Declaring a decimal constant has a simple syntax that just requires the value to be written.

Example:

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. int decimal = 42;  
  5. printf("The decimal constant is: %d\n", decimal);  
  6. return 0;  
  7. }  

Output:

The decimal constant is: 42

Real or Floating-Point Constant:

fractional component or exponentiation of a number is represented by a real or floating-point constant. It can be expressed with a decimal point, the letter "E", or the symbol "e" in exponential or decimal notation.

Example:

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. float real = 3.14;  
  5. printf("The real constant is: %f\n", real);  
  6. return 0;  
  7. }  

Output:

The real constant is: 3.140000

Octal Constant:

base 8 value is represented by an octal constant. It is prefixed with a '0' (zero) to show that it is an octal constant and has digits ranging from 0 to 7.

Example:

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. int octal = 052; // Octal representation of decimal 42  
  5. printf("The octal constant is: %o\n", octal);  
  6. return 0;  
  7. }  

Output:

The octal constant is: 52

Hexadecimal Constant:

base-16 value is represented by a hexadecimal constant. It uses letters A to F (or a to f) and numbers 0 to 9 to represent values from 10 to 15. It is prefixed with '0x' or '0X' to identify it as a hexadecimal constant.

Example:

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. int hexadecimal = 0x2A; // Hexadecimal representation of decimal 42  
  5. printf("The hexadecimal constant is: %x\n", hexadecimal);  
  6. return 0;  
  7. }  

Output:

The hexadecimal constant is: 2a

Character Constant

character constant represents a single character that is enclosed in single quotes.

Example:

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. char character = 'A';  
  5. printf("The character constant is: %c\n", character);  
  6. return 0;  
  7. }  

Output:

The character constant is: A

String Constant:

series of characters wrapped in double quotes is represented by a string constant. It is a character array that ends with the null character \0.

Example:

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. char string[] = "Hello, World!";  
  5. printf("The string constant is: %s\n", string);  
  6. return 0;  
  7. }  

Output:

The string constant is: Hello, World!

Rules for constructing constants:

The creation of constants must follow specified guidelines. These guidelines specify the format that constants of various kinds must follow in order for the compiler to accept them as legitimate. The guidelines for creating constants in C are as follows:

Integer Constants:

  1. The digits of an integer constant can range from 0 to 9.
  2. decimal point should not be present in an integer constant.
  3. Positive or negative integer constants are also possible.
  4. You can add a suffix to a constant's name to define its type. 'U' or 'u' stands for unsigned, 'L' or 'l' for long, or 'LL' or 'll' for long long.

Floating-Point Constants:

  1. decimal point, an exponent, or digits can all be found in floating-point constants.
  2. Either exponential notation or decimal notation can be used to write them.
  3. 'E' or 'e' can be used to denote an exponent.
  4. You can add a suffix to a constant's name to define its type. For instance, "F" or "f" stands for float and "L" or "l" for long double.

Octagonal Constants

  1. Base 8 is used for writing octal constants.
  2. They are made up of the numerals 0 through 7.
  3. '0' (zero) should come before any octal constants.

Hexadecimal Constants:

  1. Constants in hexadecimal are expressed in base 16.
  2. To represent numbers from 10 to 15, they are made up of numerals 0 to 9 and letters A to F (or a to f).
  3. Prefixing a hexadecimal constant with '0x' or '0X' is appropriate.

Character Constants:

  1. Individual characters are represented as character constants when they are in single quotes.
  2. single letter or an escape sequence, such as "n" for newline or "t" for tab, can be used as these characters.

String Constants:

  1. A series of characters surrounded in double quotes is represented by string constants.
  2. They are essentially character arrays that are closed with the null character "0".

Conclusion:

As a result of their representation of fixed values that don't change during the course of the program, constants are crucial in C programming. By following the rules for making constants, programmers may create reliable and practical representations of data in their programs.

By giving descriptive names to fixed values, minimizing the usage of "magic numbers", and encouraging self-documenting code, constants improve the readability of code. By offering a central area to edit constant values, facilitating simple modifications, and lowering the possibility of mistakes, they also aid in the maintainability of the code.

As constants may be used and referenced several times throughout the program, using them in C programs also increases code reuse. When utilizing the same value again, it assures consistency and lessens the possibility of adding errors or mistakes.

Constants can also improve computations and processes, particularly when physical or mathematical constants are involved. Programmers can create code that is more effective and optimized by utilizing constants rather than hard-coding variables.