// Variables
Rules of Declaring Variables in C++
Example 1:
#include <stdio.h>
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
printf("Sum of a and b is %d\n",sum);
}
int main()
{
add();
return 0;
}
//Video Tutorial
Example 2:
#include <iostream>
using namespace std;
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
cout <<"Sum of a and b is "<<sum<<"\n";
}
int main()
{
add();
return 0;
}
Rules of Declaring Variables in C++
- Variable name can consist of letter, alphabets and start with underscore character.
- First character of variable always should be alphabet and cannot be digit.
- Blank spaces are not allowed in variable name.
- Special characters like #, $ are not allowed.
- A single variable can only be declared for only 1 data type in a program.
- Upper and lowercase letters are distinct because C++ is case-sensitive, so if we declare a variable name and one more NAME both are two different variables.
- C++ has certain keywords which cannot be used for variable name.
- A variable name can be consist of 31 characters only if we declare a variable more than 1 characters compiler will ignore after 31 characters.
The following is a table of the names of the types of variables:
| Type | Description |
|---|---|
| bool | Stores either value true or false. |
| char | Typically a single octet(one byte). This is an integer type. |
| int | The most natural size of integer for the machine. |
| float | A single-precision floating point value. |
| double | A double-precision floating point value. |
| void | Represents the absence of type. |
| wchar_t | A wide character type. |
Example 1:
#include <stdio.h>
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
printf("Sum of a and b is %d\n",sum);
}
int main()
{
add();
return 0;
}
//Video Tutorial
Example 2:
#include <iostream>
using namespace std;
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
cout <<"Sum of a and b is "<<sum<<"\n";
}
int main()
{
add();
return 0;
}
thank alots sir...
ReplyDelete