Computer Science, asked by thangamba9563, 1 year ago

How to create Variables and Constants in C++?

Answers

Answered by NiksDwivedi
0
>>>Using const keyword: The const keyword specifies that a variable or object value is constant and can’t be modified at the compilation time

&lt;font color=blue&gt;// C program to demonstrate&lt;br&gt; const specifier&lt;br&gt;<br /><br />#include &lt;stdio.h&gt;&lt;br&gt;<br /><br />int main()&lt;br&gt;<br /><br />{&lt;br&gt;<br /><br />const int num = 1; &lt;br&gt;<br /><br />num = 5; // Modifying the value&lt;br&gt;<br /><br />return 0; &lt;br&gt;<br /><br />} &lt;br&gt;<br /><br />It will throw as error like:&lt;br&gt;<br /><br />error: assignment of read-only variable ‘num’

&lt;font color = black&gt;&gt;&gt;&gt; Using enum keyword: Enumeration (or enum) is a user defined data type&lt;br&gt; in C and C++. It is mainly used to assign names to integral constants,&lt;br&gt; that make a program easy to read and maintain.

&lt;font color = blue&gt;// In C and C++ internally the&lt;br&gt; default&lt;br&gt;<br /><br />// type of 'var' is int&lt;br&gt;<br /><br />enum VARS { var = 42 };&lt;br&gt;<br /><br />// In C++ 11 (can have any integral type):&lt;br&gt;<br /><br />enum : type { var = 42; }&lt;br&gt;<br /><br />// where mytype = int, char, long etc.&lt;br&gt;<br /><br />// but it can't be float, double or&lt;br&gt;<br /><br />// user defined data type.
Similar questions