In computer programs we do lots of calculations. To execute these calculations we need memory space which can hold that value. To identify that name we will use a name which will address that memory location. The name is called Variable, if “a” is a name of memory location then “a” is a variable.
Now for Constant definition we will say that Constant is an entity that doesn’t change, where variable is an entity that may change. For example if we put a value in a memory location then that value is a constant. Suppose variable “a” holds value 25, then 25 is a constant here. If we replace 25 with 45 then value that variable holds changed to 45, but 25 is still 25.
There are mainly three types of Primary Constants. These are Integer Constant, Real Constant, and Character Constant. Now an integer constant holds integer values, which can either positive or negative. For examples we will say that 2, 3, -5. The allowable range for integer constant is -32768 to 32767. Real constant also called as floating point constants. For example we say 2.5, 35.700, 120.532, and 9.00. The allowable range for Real constant is -3.4e38 to 3.4e38.
Now particular type of constants only holds by particular type of variables. These variables types are defined by Data types. If “a” is a variable and we want to make it integer type, then declare this as “int a”. There are many type of data types, some common type of data types are int, float, char.
Keyword are “Reserved Words” whose meaning is already been explained in compilers. Keyword are used in programming for making proper instructions. Now a variable name can’t be a keyword.
There are total 32 keywords in C compiler. Each keywords have a special mining. Data types are also keyword. Keywords are:
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
Now let me write a code for Data type, Keyword, Constant and Variables
Now when we printing these values inside “printf()”, we use format specifier. For each data type have it’s own format specifier. For example integer format specifier is %d, floating point specifier is %f. if we want to write variable value inside “printf()” then we need to write these format : “printf(“format specifier”,variable name);” Now let’s see the output for these code
Now for Constant definition we will say that Constant is an entity that doesn’t change, where variable is an entity that may change. For example if we put a value in a memory location then that value is a constant. Suppose variable “a” holds value 25, then 25 is a constant here. If we replace 25 with 45 then value that variable holds changed to 45, but 25 is still 25.
There are mainly three types of Primary Constants. These are Integer Constant, Real Constant, and Character Constant. Now an integer constant holds integer values, which can either positive or negative. For examples we will say that 2, 3, -5. The allowable range for integer constant is -32768 to 32767. Real constant also called as floating point constants. For example we say 2.5, 35.700, 120.532, and 9.00. The allowable range for Real constant is -3.4e38 to 3.4e38.
Now particular type of constants only holds by particular type of variables. These variables types are defined by Data types. If “a” is a variable and we want to make it integer type, then declare this as “int a”. There are many type of data types, some common type of data types are int, float, char.
Keyword are “Reserved Words” whose meaning is already been explained in compilers. Keyword are used in programming for making proper instructions. Now a variable name can’t be a keyword.
There are total 32 keywords in C compiler. Each keywords have a special mining. Data types are also keyword. Keywords are:
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
Now let me write a code for Data type, Keyword, Constant and Variables
#include < stdio.h >
void main()
{
int a=10;
float b=20.3;
char c='A';
double const d=16;
printf("The Value of a is %d\n",a);
printf("The Value of b is %.2f\n",b);
printf("The Value of c is %c\n",c);
printf("The Value of d is %lf\n",d);
}
Here in these program “int”, “float”, “char”, “double” are data types. “const” is a keyword which says that value in variable “d” will not change during program, and “a”, “b”, “c”, “d” is variables. void main()
{
int a=10;
float b=20.3;
char c='A';
double const d=16;
printf("The Value of a is %d\n",a);
printf("The Value of b is %.2f\n",b);
printf("The Value of c is %c\n",c);
printf("The Value of d is %lf\n",d);
}
Now when we printing these values inside “printf()”, we use format specifier. For each data type have it’s own format specifier. For example integer format specifier is %d, floating point specifier is %f. if we want to write variable value inside “printf()” then we need to write these format : “printf(“format specifier”,variable name);” Now let’s see the output for these code
The Value of a is 10
The Value of b is 20.30
The Value of c is A
The Value of d is 16.000000
Now let me write same problem in C++ language. The Value of b is 20.30
The Value of c is A
The Value of d is 16.000000
#include < iostream >
using namespace std;
int main()
{
int a=10;
float b=20.3;
char c='A';
double const d=16;
cout< <"The Value of a is"< <a< <"\n";
cout< <"The Value of b is"< < < <"\n";
cout< <"The Value of c is"< <c< <"\n";
cout< <"The Value of d is"< <d< <"\n";
return 0;
}
As we can see here that format specifier is not important in C++. We can write variable directly. Now let see the output using namespace std;
int main()
{
int a=10;
float b=20.3;
char c='A';
double const d=16;
cout< <"The Value of a is"< <a< <"\n";
cout< <"The Value of b is"< < < <"\n";
cout< <"The Value of c is"< <c< <"\n";
cout< <"The Value of d is"< <d< <"\n";
return 0;
}
The Value of a is 10
The Value of b is 20.30
The Value of c is A
The Value of d is 16
The Value of b is 20.30
The Value of c is A
The Value of d is 16

0 comments:
Post a Comment