0% found this document useful (0 votes)
244 views4 pages

C++ Basics: DevC++ Setup and Coding

The document provides instructions for a lab introduction to C++. The objectives are to learn the DevC++ editor software, write basic C++ programs using cout statements, and gain familiarity with syntax errors. The steps are to run DevC++ and create a new file, save the file with a .cpp extension, and write and run a "Hello World" program that displays the message using cout and ends with a return 0 statement. The document also provides additional details on C++ code elements like headers, namespaces, comments, and tasks for students to practice writing, compiling and running simple programs.

Uploaded by

Ainur Nasuha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views4 pages

C++ Basics: DevC++ Setup and Coding

The document provides instructions for a lab introduction to C++. The objectives are to learn the DevC++ editor software, write basic C++ programs using cout statements, and gain familiarity with syntax errors. The steps are to run DevC++ and create a new file, save the file with a .cpp extension, and write and run a "Hello World" program that displays the message using cout and ends with a return 0 statement. The document also provides additional details on C++ code elements like headers, namespaces, comments, and tasks for students to practice writing, compiling and running simple programs.

Uploaded by

Ainur Nasuha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab 1: Introduction to C++

Objectives:

1. Learn about the DevC++ editor software


2. Practice writing, compiling and running basic C++ programs with cout statements
3. Gain familiarity with syntax error

Step 1: Run DevC++ and create file

 Run the DevC++ from the Start Menu of the lab computer
 In DevC++, create new C++ program: Click File >> New >> Source File from top menu. An
empty white text window should appear

Step 2: Save the program

 Click the File >> Save As.. from the top menu.
 The file name must end .cpp.

 Any C (C++) program must be saved in a file with extension “.c” (“.cpp”).
 File names should begin with lower case letters.
 File names should limited up to 30 characters.
 File names and directory paths should not contain blanks, parentheses “(”, or crazy characters.
 Smallest non-empty C/C++ file, which is compilable, must have at least the main().
Step 3: Compile and run the program
 Write the following code.

1 #include <iostream>
2 using namespace std;
3 // main () is where program execution begins.
4 int main ()
5 {
6 cout << "Hello World!" << endl;
7 return 0;
8 }

 The program starts with what is called preprocessor directive, #include.


 The main program starts with keyword main(), each program must have a main()
function.
 All the coding is included in the body of the main(), within the curly braces { }.
 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.
 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!" << endl; 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.

 Click Execute >> Compile & Run from the top menu
Task 1: Trace the output for the following statements.

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello " << "world, " << "again!" << endl;
cout << "hello," << endl << "one more time." << endl;
return 0;
}

Task 2: How many lines of output are produced?

int main()
{
cout << "Testing, testing" << endl;
cout << "one two three" << endl;
cout << endl;

cout << "How much output" << endl;


cout << endl;
cout << "will there be?" << endl;
return 0;
}

Task 3: Create a program that display the output as shown below.

Task 4: Create a program to demonstrate comments

Task 5: The following program contains 7 errors! What are they?


(Once you think you have found the errors, create/compile/run a corrected version of this program)

#include
using std;
int main ();
{

Cout << "Hello World" << endl;


cout << "Do you like this program? << endl;
cout << "I wrote it myself" >> endl;
return 0

You might also like