
C++ Diving Deeper
Prateek Narang
Topics
-
Header Files
-
Preprocessor Directive
-
main()
-
Keywords & Identifiers
-
Comments
-
Namespaces



Hello World!
#include<iostream>
using namespace std;
int main(){
// Your Code Goes Here
cout <<"Hello World!";
return 0;
}
Header files contain the set of predefined standard library functions & data type definitions. Include a header file in your program by including it with the preprocessing directive “#include”

Header File
#include<iostream>
#include<algorithm>
#include<string>
...
The preprocessors are the directives, which give instructions to the compiler to preprocess some code before actual compilation starts.
The directive begins with '#'
The actual code is compiled by compiler only

Preprocessor Directive
#include<iostream>
#include<algorithm>
#include<string>
...
#include
directive tells the compiler to include the header file in the source code.
#define
directive tells the compiler to create symbolic constants. The symbolic constant is called a macro

#include<iostream>
#define PI 3.14
int main(){
//Area of Circle
int r = 5;
float area = PI*r*r;
return 0;
}
iostream: iostream stands for standard input-output stream. This header file contains definitions of objects like cin, cout etc.

<iostream>
#include<iostream>
...
int main(){
cout <<"Hello!";
}
The cout object in C++ is an object of class ostream. It is defined in iostream header file. It is used to display the output to the standard output device

cout
cout << "Hello!";
The cout object in C++ is an object of class ostream. It is defined in iostream header file. It is used to display the output to the standard output device. More than one variable can be printed using the insertion operator(<<) with cout

cout
String name = "Prateek";
cout << "Hello " << name ; // Hello Prateek
#include<iostream>
#include<string>
using namespace std;
int main(){
// Your Code Goes Here
String name = "Prateek";
cout <<"Hello World!";
cout << 5 << endl;
cout <<"Hi " + name;
return 0;
}


Keywords
Keywords are those words that have a special meaning for the Compiler.
These keywords cannot be used as an identifier.
int main(){
int x = 10;
if(x>5){
cout<<"yes";
}
return 0;
}

Example Keywords
There are a total of 95 reserved words in C++
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
asm dynamic_cast namespace reinterpret_cast
bool explicit new static_cast
catch false operator template
class friend private this
const_cast inline public throw
delete mutable protected true
try typeid typename using
using virtual wchar_t

💡Identify Keywords
Can you find keywords in the following program?
#include<iostream>
using namespace std;
int main(){
long x = 10;
if(x>5){
cout<<"yes";
}
while(x<=100){
x = x+1;
}
return 0;
}

Identifiers
Both an identifier and a variable are the names allotted by users to a particular entity in a program.
The identifier is only used to identify an entity uniquely in a program at the time of execution whereas, a variable is a name given to a memory location, that is used to hold a value.
int factorial(int n){
int ans = 1;
for(int i=1;i<=n;i++){
ans = ans*i;
}
return ans;
}

Main Function
Every C/C++ Program Execution start with main()
There is exactly 1 main() function.
return 0 indicates successful program execution.
int main(){
// Your Code goes here
return 0;
}

Is main a keyword?
main is not a keyword in C/C++.
main is not predefined, but it is predeclared.
In C++, your code is linked against a small runtime library that constitutes the true starting point of your program.
It is this small library calls a function called main--it's hardcoded to do so.
Your code runs because you supply the code inside main, also called function definition.
int main(){
// Your Code goes here
return 0;
}

Comments
A comment is text that the compiler ignores but that is useful for programmers. Comments are normally used to annotate code for future reference.
A C++ comment can be single line or multi-line comment.
// Single Line Comment
int marks;
/* Multi-line
Comment */
int x = 10;

Summary
- Keywords have special meaning for the compiler.
- Header files include some pre-written code required to execute our program.
- Program execution always starts with main()
- { } are used to enclose a block (function, if, while etc.}.
- C++ Compiler Ignores whitespace (space, carriage returns, linefeeds, tabs, vertical tabs, etc.)
- Output using cout
- Input using cin
- Comments (// & /*… */)
- Every statement must end with a semicolon ;
[Topics 05] Diving Deeper
By Prateek Narang
[Topics 05] Diving Deeper
- 7