In the previous post we went over a few basic programming concepts. Now we'll jump right into writing an actual C++ code.
Comments: these are ignored by the compiler so they are basically used to document programs and to improve program readability. In C++ a single-line comment starts with // and a multiple line comment is enclosed in between /* and */
Preprocessor directives: these are processed by the preprocesser before compiling. C++ Compilers automatically invoke a preprocessor that takes care of statements starting with # . for example when you write #include directive the preprocessor is replacing the line with the contents of the file you include. Other directives tell the preprocessor to look for patterns in the program and do some fancy processing.
A Simple Program: Writing a line of text
The program given below prints a simple line of text, the use of single line comments and preprocessor directives is shown. other things int the program are explained below:
Escape Characters and Escape Sequences:
The backslash \ is called the escape character and indicates special character output. along with some characters and escape character gives rise to special escape sequences as shown below
Comments: these are ignored by the compiler so they are basically used to document programs and to improve program readability. In C++ a single-line comment starts with // and a multiple line comment is enclosed in between /* and */
Preprocessor directives: these are processed by the preprocesser before compiling. C++ Compilers automatically invoke a preprocessor that takes care of statements starting with # . for example when you write #include directive the preprocessor is replacing the line with the contents of the file you include. Other directives tell the preprocessor to look for patterns in the program and do some fancy processing.
Preprocessing |
The program given below prints a simple line of text, the use of single line comments and preprocessor directives is shown. other things int the program are explained below:
- Function main() is used only once in a c++ program. in this program "int" return before main indicates that program returns an integer.
- function body is enclosed between braces.
- statements end with a semicolon.
- std:: indicates that cout belongs to namespace std. it can be removed by writing "using namespace std;" at the start when you write preprocessor directives.
- cout is the output stream object. << is the stream insertion operator and indicates that value to the right is inserted into the output stream.
- Keyword return is one of the several means to exit a function, value zero indicates that program terminated successfully.
the
Escape Characters and Escape Sequences:
The backslash \ is called the escape character and indicates special character output. along with some characters and escape character gives rise to special escape sequences as shown below
The usage of escape sequences is shown in these two examples:
Another Simple Program: Adding two integers
First we will explain variables, stream input operator and assignment operator and then we'll look at our program.
- Variables: these are values that we work with in our program, there names can be a series of characters but cannot begin with a digit. we have to be careful while naming a variable since these are case sensitive.
- Input stream object: indicated by cin and is followed by >> (stream insertion operator) , It waits for the user to input a value and then press the enter key. it stores the value in the variable to the right of the insertion operator. the value is converted to the variable data type.
- Assignment Operator: indicated by = , this assigns a value to the variable. It is a binary operator i.e it works on two operands
Post a Comment