Home > Backend Development > C++ > body text

What are the different types of constants in C language?

王林
Release: 2023-09-06 15:33:22
forward
1596 people have browsed it

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;
Copy after login

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;
}
Copy after login

Output

The area of triangle :400.000000
Copy after login

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//
}
Copy after login

Output

a=10
p=6422036
a=12
p=6422036
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!