BASIC THINGS IN CREATING A PROGRAM
C++ Character Set
Character set is a set of valid characters that a language can recognize.
Letters | A-Z, a-z |
Digits | 0-9 |
Special Characters | Space + - * / ^ \ () [] {} = != <> ‘ “ $ , ; : % ! & ? _ # <= >= @ |
Formatting characters | backspace, horizontal tab, vertical tab, form feed, and carriage return |
Tokens
A token is a group of characters that logically belong together. The programmer can write a program by using tokens. C++ uses the following types of tokens.
Keywords, Identifiers, Literals, Punctuators, Operators.
Keywords, Identifiers, Literals, Punctuators, Operators.
1. Keywords
These are some reserved words in C++ which have predefined meaning to compiler called keywords. Some commonly used Keyword are given below:
asm | auto | break | case | catch |
char | class | const | continue | default |
delete | do | double | else | enum |
extern | inline | int | float | for |
friend | goto | if | long | new |
operator | private | protected | public | register |
return | short | signed | sizeof | static |
struct | switch | template | this | Try |
typedef | union | unsigned | virtual | void |
volatile | while |
2. Identifiers
Symbolic names can be used in C++ for various data items used by a programmer in his program. A symbolic name is generally known as an identifier. The identifier is a sequence of characters taken from C++ character set. The rule for the formation of an identifier are:
- An identifier can consist of alphabets, digits and/or underscores.
- It must not start with a digit
- C++ is case sensitive that is upper case and lower case letters are considered different from each other.
- It should not be a reserved word.
3. Literals
Literals (often referred to as constants) are data items that never change their value during the execution of the program. The following types of literals are available in C++.
- Integer-Constants
- Character-constants
- Floating-constants
- Strings-constants
Integer Constants
Integer constants are whole number without any fractional part. C++ allows three types of integer constants.
Decimal integer constants : It consists of sequence of digits and should not begin with 0 (zero). For example 124, - 179, +108.
Octal integer constants: It consists of sequence of digits starting with 0 (zero). For example. 014, 012.
Hexadecimal integer constant: It consists of sequence of digits preceded by ox or OX.
Decimal integer constants : It consists of sequence of digits and should not begin with 0 (zero). For example 124, - 179, +108.
Octal integer constants: It consists of sequence of digits starting with 0 (zero). For example. 014, 012.
Hexadecimal integer constant: It consists of sequence of digits preceded by ox or OX.
Character constants
A character constant in C++ must contain one or more characters and must be enclosed in single quotation marks. For example 'A', '9', etc. C++ allows nongraphic characters which cannot be typed directly from keyboard, e.g., backspace, tab, carriage return etc. These characters can be represented by using an escape sequence. An escape sequence represents a single character. The following table gives a listing of common escape sequences.
Escape Sequence | Nongraphic Character |
\a | Bell (beep) |
\n | Newline |
\r | Carriage Return |
\t | Horizontal tab |
\0 | Null Character |
Floating constants
They are also called real constants. They are numbers having fractional parts. They may be written in fractional form or exponent form. A real constant in fractional form consists of signed or unsigned digits including a decimal point between digits. For example 3.0, -17.0, -0.627 etc.
String Literals
A sequence of character enclosed within double quotes is called a string literal. String literal is by default (automatically) added with a special character ‘\0' which denotes the end of the string. Therefore the size of the string is increased by one character. For example "COMPUTER" will re represented as "COMPUTER\0" in the memory and its size is 9 characters.
4. Punctuators
The following characters are used as punctuators in C++.
Brackets [ ] | Opening and closing brackets indicate single and multidimensional array subscript. |
Parentheses ( ) | Opening and closing brackets indicate functions calls,; function parameters for grouping expressions etc. |
Braces { } | Opening and closing braces indicate the start and end of a compound statement. |
Comma , | It is used as a separator in a function argument list. |
Semicolon ; | It is used as a statement terminator. |
Colon : | It indicates a labeled statement or conditional operator symbol. |
Asterisk * | It is used in pointer declaration or as multiplication operator. |
Equal sign = | It is used as an assignment operator. |
Pound sign # | It is used as pre-processor directive. |
5. Operators
Operators are special symbols used for specific purposes. C++ provides six types of operators. Arithmetical operators, Relational operators, Logical operators, Unary operators, Assignment operators, Conditional operators, Comma operator
#include<iostream>
Lines beginning with a pound sign (#) are used by the compilers pre-processor. In this case the directive #include tells the pre-processor to include the iostream standard file. This file iostream includes the declarations of the basic standard input/output library in C++. (See it as including extra lines of code that add functionality to your program).
#include <-- I want to use this file in my program as a reference.
<name of item> <-- this is the header file I want to use as a reference.
iostream= It basically tells the compiler: Yo... I need input and output.. .
cout<<"hai";
its used for outputting
cin>>
basically used for inputting
# means read me before you compile and do what I say essentially...
the < and > are just enclosures for the compiler to read between and import accordingly
the < and > are just enclosures for the compiler to read between and import accordingly
using namespace std;
All the elements of the standard C++ library are declared within what is called a namespace. In this case the namespace with the name std. We put this line in to declare that we will make use of the functionality offered in the namespace std. This line of code is used very frequent in C++ programs that use the standard library. You will see that we will make use of it in most of the source code included in these tutorials.
int main()
int is what is called the return value (in this case of the type integer. Integer is a whole number). What is used for will be explained further down.
Every program must have a main() function. The main function is the point where all C++ programs start their execution. The word main is followed by a pair of round brackets. That is because it is a function declaration (Functions will be explained in more detail in a later tutorial). It is possible to enclose a list of parameters within the round brackets.
{}
The two curly brackets (one in the beginning and one at the end) are used to indicate the beginning and the end of the function main. (Also called the body of a function). Everything contained within these curly brackets is what the function does when it is called and executed. In the coming tutorials you will see that many other statements make use of curly brackets.
CREATING THE FIRST PROGRAM
#include<iostream>
using namespace std; //import just the names they intend to use frequently, with something like: cin ,cout etc
int main()
{
cout<< hai programmer";
return 0;
}
Indentations
As you can see the cout and the return statement have been indented or moved to the right side. This is done to make the code more readable. In a program as Hello World, it seems a stupid thing to do. But as the programs become more complex, you will see that it makes the code more readable. (Also you will make fewer errors, like forgetting a curly bracket on the end of a function).
So, always use indentations and comments to make the code more readable. It will make your life (programming life at least) much easier if the code becomes more complex.
0 comments:
Post a Comment