Skip to main content

C++ tokens, rule for naming variable and rule for constructing integer constant, real constant and character constant

 C++ character set :-
The following image shows the valid character that are used in C++. 


C++ data types:-
1st of all we have to know that What is a data? Data is a raw material for constructing a information. For example, the following list are the data:-
Ram,17, XII C.
And from this data we can construct the information as Ram Aged 17 years is in class XII C.
Data may be any number,words, amount,quantity.

Now, Let's come on the topic:
Latter i will discus about 'structure', 'Union', 'Class', Array', 'function','pointer','reference' for now there are five basic data types that are:
1. int(integer) 
2. char(Character)
3. float
4. double 
5. void 

Now look at variable: 
Variable is a entity whose value may change and in programming language it is used to store data.
A integer type variable can hold a integer number(number must be a whole number either in positive or in negative but not in fraction),  a char type variable can store a character and so on.
Syntax for variable declaration in the program is:
data_types  name;

Example of some valid declaration:
int a;   // 'a' is actually a name of a variable that can hold a integer number either positive or negative                  but not fraction
char ch; // 'ch' is the name of a variable that can hold a character 
float f; // 'f' is a name of a floating type variable that can hole a fractional number up to 7 digit after                               decimal point
double c;  // 'c' is a name of a double type variable that can hold a fractional number up to 14 digit                        after decimal point. 


C++ tokens:-
A smallest individual units in a program are known as tokens. Let's compare C++ with English, A word is a smallest individual units of a sustenance similarly a token is also a smallest individual unit of a program statement.
C++ has the following tokens:'-
1.  Keywords 
2. Identifiers
3. Constants
4. String
5. Operators

1. Keywords:
   The keywords implemented specific C++ language features. They are explicitly reserved identifiers and cannot be used as names for the program variables or other user-defined  program elements.
The following table shows the complete set of C++ keywords.

Many of them are common for both C and C++. The ANSI C keywords are shown in boldface. Additional keywords have been added to the ANSI C  keywords in order to enhance its features and make it an object oriented language. ANSI C++ standard committee has added some more keywords to make the language more versatile. These are shown separately.
Identifier and consents:-
Identifier refer to the name of variables, functions, arrays, classes,etc. created by the programmer. They are the fundamental requirement of any language. Each language has its own rule for naming these identifiers. The following rule are common for both C and C++:


 1. Only alphabetic characters, digits and underscore are permuted.
2. The name cannot start with digit. 
That is a name '12number' is in valid. It should be number12. 
3. Uppercase and lower case letter are distinct. 
That is a NUM, Num, num are three distinct identifiers.
4. A declared keyword cannot be used as a variable name .

Now, a constant is a entity whose vale can not be changed. A constant may be a number,  a character or a string.
The following are the some constant examples:

 123         // decimal integer
 12.34      // floating point integer
 O37        // octal integer
Ox2         // hexadecimal constant
"C++"     // string constant
'A'             // character constant
L'ab'          // wide-character constant
The wchar_t type is a wide-character literal introduced by ANSI C++ and is intended for character sets that cannot fit a character into a single byte. Wide character literals start with  L.

Rules for Constructing Integer Constants:-
(a) An integer constant must have at least one digit.
(b) It must not have a decimal point.
(c) It can be either positive or negative.
(d) If no sign precedes an integer constant it is assumed to be positive.
(e) No commas or blanks are allowed within an integer constant.
(f) The allowable range for integer constants is -32768 to 32767.

Truly speaking the range of an Integer constant depends upon the compiler. For a 16-bit compiler like Turbo C or Turbo C++ the range is –32768 to 32767. For a 32-bit compiler the range would be even greater. 
Ex.: 426
+782
-8000
-7605

Rules for Constructing Real Constants:-

Real constants are often called Floating Point constants. The real
constants could be written in two forms—Fractional form and
Exponential form.
Following rules must be observed while constructing real
constants expressed in fractional form:
(a) A real constant must have at least one digit.
(b) It must have a decimal point.
(c) It could be either positive or negative.
(d) Default sign is positive.
(e) No commas or blanks are allowed within a real constant.

Ex.: +325.34
426.0
-32.76
-48.5792

Following rules must be observed while constructing real
constants expressed in exponential form:

(a)The mantissa part and the exponential part should be separated by a letter e.
(b) The mantissa part may have a positive or negative sign. Default sign of mantissa part is positive.
(c) The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive.
(d)Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.

Ex.: +3.2e-5
4.1e8
-0.2e+3
-3.2e-5

Rules for Constructing Character Constants:-

(a) A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not.
(b) The maximum length of a character constant can be 1 character.

Ex.: 'A'
'I'
'5'
'='

Comments

Popular posts from this blog

Advantage and disadvantages of Structure and object oriented programming

In my last post, i discussed about structured oriented programming, object oriented programming and language processors. Today i am going to discuss about :- 1. Advantages and Disadvantages of structured oriented programming. 2. Advantages and Disadvantages of object oriented programming. 3.Difference between them. And in the next post i will discuss about:- 1. 1st C++ program and explanation. 2. Structure of a C++ program. 3. Data types, Identifiers, Variable and Keywords. Let's start: Advantages of Structure oriented programming:- 1. It is easy to understand. 2. It is to test a program that is divided into modules(sub-problem) and sub-module(sub-sub-problem). 3. Modification is easier. 4. It results in speedy execution of the program. 5. Development of modules is easier. Disadvantages of Structured oriented programming:- 1. Modules occupy more space in the memory. 2.  Testing of every module is time consuming process. 3. Combination of modules becomes di

size and range of basic data types and modifing the basic types.

Size and range of basic data types:-   Modifying the basic types:-  Except for type void, the basic data types may have various modifiers preceding them.  You use a modifier to alter the meaning of the base type to fit various situations more  precisely. The list of modifiers is shown here: signed unsigned long short You can apply the modifiers signed, short, long, and unsigned to integer base  types. You can apply unsigned and signed to characters. You may also apply long to  double. The above  table shows all valid data type combinations, along with their minimal ranges and approximate bit widths. (These values also apply to a typical C++  implementation.) Remember, the table shows the minimum range that these types will  have as specified by Standard C/C++, not their typical range. For example, on  computers that use two's complement arithmetic (which is nearly all), an integer will have a range of at least 32,767 to –32,768. The use of signed on integers is al