Home » C++ programs

Example of declaring and printing different constants in C++

Declaring and printing different constant in C++: Here, we will learn to declare and print the value of different type of constants using C++ program.

Constant

Constants can be declared by using "const" keyboard and value of constants can never be changed during the program’s execution.

Syntax:

const data_type constant_name = value;

Read: constant declaration in C/C++

In this program, we are declaring 4 constants:

  1. String constant (character array constants): MY_NAME and MY_ADDRESS
  2. Integer constant: MY_AGE
  3. Float constant: MY_WEIGHT

Consider the program:



#include <iostream>
using namespace std;

int main()
{
	//declaring constants
	const char		MY_NAME[]="Master Satendra Sharma";
	const char		MY_ADDRESS[]="New Delhi, INDIA";
	const int		MY_AGE =17;
	const float		MY_WEIGHT = 50.25f;
	
	//printing the constants values
	cout<<"Name:   "<<MY_NAME<<endl;
	cout<<"Age:    "<<MY_AGE<<endl;
	cout<<"Weight: "<<MY_WEIGHT<<endl;
	cout<<"Address:"<<MY_ADDRESS<<endl;
	
	return 0;
}

Output

Name:   Master Satendra Sharma
Age:    17
Weight: 50.25  
Address:New Delhi, INDIA

Here, I am trying to change the value, consider the program...



#include <iostream>
#include <string.h>
using namespace std;

int main()
{
	//declaring constants
	const char 	MY_NAME[]="Master Satendra Sharma";
	const char 	MY_ADDRESS[]="New Delhi, INDIA";
	const int 	MY_AGE =17;
	const float MY_WEIGHT = 50.25f;
	
	//changing the value
	MY_AGE = 18; //ERROR
	MY_WEIGHT = 60.0f; //ERROR
	strcpy(MY_NAME,"RAHUL"); //ERROR
	
	
	//printing the constants values
	cout<<"Name:   "<<MY_NAME<<endl;
	cout<<"Age:    "<<MY_AGE<<endl;
	cout<<"Weight: "<<MY_WEIGHT<<endl;
	cout<<"Address:"<<MY_ADDRESS<<endl;
	
	return 0;
}

Output

main.cpp: In function 'int main()':     
main.cpp:14:9: error: assignment of read-only variable 'MY_AGE'       
  MY_AGE = 18; //ERROR   
         ^
main.cpp:15:12: error: assignment of read-only variable 'MY_WEIGHT'   
  MY_WEIGHT = 60.0f; //ERROR            
            ^            
main.cpp:16:24: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]              
  strcpy(MY_NAME,"RAHUL"); //ERROR      
         ^
In file included from main.cpp:2:0:     
/usr/include/string.h:125:14: error:   initializing argument 1 of 'char* strcpy(char*, const char*)' [-fpermissive]
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)         
              ^      





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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 some rights reserved.