Home >Backend Development >C++ >What are the different types of constants in C language?

What are the different types of constants in C language?

王林
王林forward
2023-09-06 15:33:221675browse

What are the different types of constants in C language?

Constant is a value that cannot be changed during program execution;

In C language, a number or a character or a string is called a constant. It can be of any data type. Constants are also called literals.

There are two types of constants -

Main constants - Integers, floating point numbers and characters are called main constants.

Auxiliary constants - Arrays, structures, pointers, enumerations, etc. are called auxiliary constants.

Syntax

const datatype variable;

Main constant example

Real-time demonstration

#include<stdio.h>
int main(){
   const int height=20;
   const int base=40;
   float area;
   area=0.5 * height*base;
   printf("The area of triangle :%f", area);
   return 0;
}

Output

The area of triangle :400.000000

Auxiliary constant example

Real-time Demo

include<stdio.h>
void main(){
   int a;
   int *p;
   a=10;
   p=&a;
   printf("a=%d</p><p>",a);//10//
   printf("p=%d</p><p>",p);//address value of p//
   *p=12;
   printf("a=%d</p><p>",a);//12//
   printf("p=%d</p><p>",p);//address value of p//
}

Output

a=10
p=6422036
a=12
p=6422036

The above is the detailed content of What are the different types of constants in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete