PCBasedControl Section1
PCBasedControl Section1
Programming Language
INTRODUCTION OF PC BASED CONTROL
Header file inclusion section: This is the section where we include all required header files
whose functions we are going to use in the program.
Namespace section: This is the section where we use the namespace.
The main() section: In this section, we write our main code. The main() function is an entry
point of any C++ programming code from where the program's execution starts.
Example
#include <iostream>
using namespace std;
// main() is where program execution begins.
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
The C++ language defines several headers, which contain information that is either necessary
or useful to your program. For this program, the header <iostream> is needed.
The line using namespace std; tells the compiler to use the std namespace. Namespaces are a
relatively recent addition to C++.
The next line '// main() is where program execution begins.' is a single-line comment available
in C++. Single-line comments begin with // and stop at the end of the line.
The line int main() is the main function where program execution begins.
The next line cout << "Hello World"; causes the message "Hello World" to be displayed on the
screen.
The next line return 0; terminates main() function and causes it to return the value 0 to the
calling process.
Compile and Execute C++
Program
Open a text editor and add the code as above.
Save the file as: hello.cpp
Open a command prompt and go to the directory where you saved the file.
Type 'g++ hello.cpp' and press enter to compile your code. If there are no errors in your code
the command prompt will take you to the next line and would generate a.out executable file.
Now, type 'a.out' to run your program.
You will be able to see ' Hello World ' printed on the window.
Semicolons and Blocks in C++
In C++, the semicolon is a statement terminator. That is, each individual statement must be ended with a
semicolon. It indicates the end of one logical entity.
A block is a set of logically connected statements that are surrounded by opening and closing braces. For
example
{
cout << "Hello World"; // prints Hello World
return 0;
}
C++ Identifiers
A C++ identifier is a name used to identify a variable, function, class, module, or any other user-
defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by
zero or more letters, underscores, and digits (0 to 9).
C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a case-
sensitive programming language.
Here are some examples of acceptable identifiers −
A single-line comment starts with //, extending to the end of the line. These comments can last
only till the end of the line, and the next line leads to a new comment.
// Text to be commented
C++ Multi-line Comments
/* This is a comment */
/* C++ comments can also
span multiple lines
*/
Comments within Statements
#include <iostream>
using namespace std;
int main()
{
cout << "This line" /*what is this*/ << " contains a comment" << endl;
return 0;
}
C++ "Hello, World!" Program
Printing "Hello, World!" is the first program in C++. Here, this prints "Hello, World" on the console (output screen). To
start learning C++, it is the first step to print sometime on the screen.
// First C++ program
#include<iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Output : This program will print "Hello, World!" on the output screen. The output will be −
Hello, World!
Parts of C++ "Hello, World!"
Program
Comment Section (// First C++ program)
Comments are used to specify a textual line that is not supposed to be executed when we
compile the code. The compiler ignores the line, and proceeds to the next line. These are used
for better readability and explanation of code in the comments section.
The #include is known as a pre-processor directive in C++. It is used to include header files
with specific methods and elements. Multiple #include statements are used to apply different
header files in the program. The iostream is the header file that defines functions and
operations related to the input/output stream.
#include <iostream>
Namespace (using namespace std;)
Namespaces are used to differentiate code blocks with the same method names. In this
program, the using namespace std; is used to set the namespace as standard for users to apply
all standard methods in programs.
The main() function is the default starting point of any C++ program. It is compulsory for any C+
+ program to have a main function. The program logics are written inside the main program.
The main function body is enclosed inside parenthesis ({}).
int main() {
cout << "Hello, World!";
return 0;
}
Printing Statement (cout)
The print/output statement is cout followed by "<<" operator. This is used to print the given
parameters specified in the statement on the screen. We can also print multiple elements in a
single cout block.
The return statement is also known as the exit statement. It is used to exit from the
corresponding function. The "return 0" is the default statement to exit from the main program.
return 0;