learncpponline

Variables and Declarations in C++

Variables in C Plus Plus (C++): A variable can be defined as “a quantity that varies during program execution”. A variable is a symbol that represents a storage location in the computer’s memory. The information that is stored in that location is called the value of the variable. One common way for a variable to obtain a value is by an assignment. This has the syntax: variable = expression First, the expression is evaluated and then the resulting value is assigned to the variable. The equal sign “=” is the assignment operator in C++. Declarations in C++: A declaration associates a group of variables with a specific data-type. All variables must be declared before they can appear in executable statements. A declaration consists of a data-type, followed by one or more variable names, ending with a semicolon. Example: int a, b, c; float root1, root2; char flag; Here,a, b and c are...

Data Types in C++

Data types supported by C++ can be broadly divided into six heads as stated follows: int data-type char data-type float data-type double data-type bool (boolean) data-type enum (enumeration) data-type 1) int data-type: int data-type represents the whole number (i.e., integer) quantities. Integers are required not to contain a decimal point or an exponent. int data-type can also be further sub-divided into two broad categories, viz, short int and long int signed int and unsigned int short int and long int The qualifier short, when placed in front of the int declaration, tells the C++ system that the particular variable being declared will be used to store fairly small integer values. On the other hand, the qualifier long, when placed in front of the int declaration, tells the C++ system that the particular variable being declared will be used to store fairly large integer values. signed int and unsigned int In case...

Identifiers and Keywords in C++

In a C++ program (i.e., a program written in C++ language), every word is either classified as an identifier or a keyword. Identifiers, as the name suggests, are used to identify or name various program elements such as variables, constants, functions etc. On the other hand, Keywords are a kind of reserved word which have standard, predefined meanings in C++. Now, let us discuss these concepts in details: Identifiers: As stated before, identifiers are names that are given to various program elements such as variables, constants, functions, arrays etc. There are certain rules regarding identifier names in C++, as stated below: Identifiers must consist of letters and digits, in any order, except that the first character must be a letter Both upper-case and lower-case letters are permitted, though common usage favours the use of lower-case letters for most types of identifiers. Upper-case and lower-case letters are not interchangeable i.e., an upper-case...

Comment Statements in C++

Unlike C, C++ supports two types of comments. C programmers are familiar with the /*..*/ style of commenting. Anything lying within the /*..*/ pair is ignored by the compiler. C plus plus (C++) additionally supports the // notation of commenting. Here, everything following // till the end of line is treated as comment. Usually /*..*/ style is used for commenting out of a block code, whereas, // is used for single-line comments. Example of single-line comment: // This is a single line comment Example of multiple-line comment: /*This is a multiple line comment*/