Home » Syntax References

Constant declaration syntax in C/C++ language

Learn: How to declare a constant in C/C++ programming language? Here you will find constant declaration syntax, explanation and example.

A constant is also the name of memory blocks but we cannot change the value of the constants at anytime (runtime).

Note: Declaration with storage class is discussed in variable declaration syntax in C/C++, we can also use storage class with the constants.

How to declare a constant in C/C++?

const keyword is used to declare a constant in C/C++ language, here is the syntax of constant declaration:

const data_type constant_name = value;

Here,
const is a keyword, which specifies that, constant_name is a constant and we cannot change its value.
data_type is the type of data.
constant_name is the name of constant.
value is the value of constant (while declaring a constant value must be provided).

Example:

//constant integer declaration
const int MAX_ROWS = 100;

//constant float declaration
const float PI = 3.14f;

//constant string (characters array) declaration
const char FLLE_NAME [] = "hello.txt";

Consider the program:

#include <stdio.h>

int main()
{
	//constant integer declaration
	const int MAX_ROWS = 100;
	//constant float declaration
	const float PI = 3.14f;
	//constant string (characters array) declaration
	const char FLLE_NAME [] = "hello.txt";
	
	printf("Value of MAX_ROWS: %d\n",MAX_ROWS);
	printf("Value of PI: %f\n",PI);
	printf("Value of FILE_NAME: %s\n",FLLE_NAME);

	return 0;
}

Output

Value of MAX_ROWS: 100
Value of PI: 3.140000 
Value of FILE_NAME: hello.txt

Must read:

  1. variable declaration value in C language
  2. Basic data types in C language




Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.



Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.