CP1 - Unit 2 - C ProgLag and Its Structure
CP1 - Unit 2 - C ProgLag and Its Structure
Introduction
Because of its simplicity and good features, many languages that were developed after C
have borrowed its syntax and features directly or indirectly. Like syntax of Java, PHP,
JavaScript, and many other languages are mainly based on C language.
C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions
of UNIX. C++ was originally known as “C with classes” and was renamed C++ in 1983. It has
now different versions.
Learning Objectives
8
C Language and Its Structure
Course Materials
2.1 Applications of C
C was intended for system development work, particularly the programs for operating
system. C was adopted as a system development language because it produces codes that
are comparable to codes written in assembly language in terms on how fast they run/execute.
Also, the format and syntax of C is much easier to understand than in Assembly. Some
examples of the use of C are:
Assemblers
Databases
Language Compilers
Language Interpreters
Modern Programs
Network Drivers
Operating Systems
Print Spoolers
Text Editors
Utilities
Some of today's most visible used systems have their critical parts written in C++.
Applications that involve local and wide area networks, user interaction, numeric, graphics, and
database access highly depend on C++ language.
9
C Language and Its Structure
Consider the famous “Hello World!” program of C language, and let’s examine every line
of it.
1. headers,
2. class definition,
3. member functions definitions and
4. main function.
Note that in C++, a program can be written with or without a class and its member
functions definitions. A simple C++ program (without a class) includes comments, headers,
namespace, main() and input/output statements.
Line 1 is a comment line. Comments are important element of a program that is used to
increase the readability of a program and to describe its function. Comments are not executable
statements so they are ignored by the compiler. There are two ways to write comments. It can
be written with double slash at the beginning (//) for single-line comment or can be enclosed
within /* and */ for multi-line comments.
10
C Language and Its Structure
Line 2, 4 and 6 Whitespace is a term that refers to characters that are used for formatting
purposes. In C++, this refers primarily to spaces, tabs, and newlines. The C++ compiler
generally ignores whitespace, with a few minor exceptions (when processing text literals or
when they are inside “ “ ). We use whitespace to make our program easier to read.
Examples:
Although both are correct, cout << “Hello World!”; is better than cout<<”Hello World!”;
Line 3 #include <iostream>. It instructs the compiler to include the standard stream I/O library.
Without this header inclusion, the expression cout << “Hello World!”; would not compile
https://ecomputernotes.com/cpp/introduction-to-oop/structure-of-a-cpp
11
C Language and Its Structure
Since its creation, C++ has gone through many changes by the C++ Standards Committee.
One of the new features added to this language is namespace. A namespace permits
grouping of various entities like classes, objects, functions and various C++ tokens, etc.,
under a single name. Different users can create separate namespaces and thus can use
similar names of the entities. This avoids compile-time error that may exist due to identical-
name conflicts.
The C++ Standards Committee has rearranged the entities of the standard library under a
namespace called std. In Figure, the statement using namespace std informs the compiler
to include all the entities present in the namespace std. The entities of a namespace can be
accessed in different ways which are listed here.
• By specifying the using directive using namespace std;
cout<<"Hello World";
https://ecomputernotes.com/cpp/introduction-to-oop/structure-of-a-cpp
Line 7 int main().This is the main function of the program. Functions are denoted by the
parentheses(). Before the main function is "int". This means that the main function will return an
integer to the function or process that called it. A program can have many function but only the
main() is executed automatically, others need to be called to be run.
Line 8 and 11 Curly braces, { }, express grouping in C++. Here, they indicate the start and end
of the function body. We prefer the opening brace and the closing brace appear on their own line
and indented on the same level. If you get a compile error of missing { or missing } it will be easy to
find the one with the missing pair.
12
C Language and Its Structure
Example:
int main()
{
for ( ; ; )
{
statement;
if ( condition)
{
statement;
}
else
{
statement;
}
statement;
}
}
Compared to:
int main() {
for ( ; ; ){
statement;
if ( condition) {
statement;
}
else {
statement;
}
statement;
}
}
13
C Language and Its Structure
This line displays Hello World! on your computer screen. We use cout << if we want to
display something on the screen. Whatever is inside the double quotation marks are displayed.
If not surrounded by “ “, like cout << tax; the compiler will treat tax as a variable that holds a
certain value and that value will be displayed on the screen. C++ statement ends with a
semicolon.
endl is like moving the cursor to the next line. So, if we issue:
Hello World!
Here in Laguna.
Spaces within the double quotation marks are counted and displayed.
output:
Hello World!
Hello World!
Line 10 return 0; This is the last command in the main function. Its purpose is only to return a
value to the function or process that called the main function. It is required by the "int" in front of
the main function definition. It should return a zero from the main function meaning the program
ran successfully and exited.
14
C Language and Its Structure
Ouput:
The sum of 8 + 10 is 18
Line 9 and 10 declare a and b as storage for values 8 and 10 respectively. int means that they
will store an integer value or whole numbers.
Line 11 cout << "The sum of " << a << " + " << b << " is " << a+b;
Will display on your screen what’s inside the “ “ and the value represented by a and b (notice
they are not enclosed with double quotes) and the answer to the direct computation a+b.
We can declare another variable, c to hold the answer for a + b and substitute it to a+b in cout.
15
C Language and Its Structure
Activities
#include<iosteam>
int main()
{
int inches = 15;
cout << "The equivalent of " << inches << inches in centimeter is " << b << inches * 2.45;
return 0;
}
5. Write a program that will compute the number of churchgoers that are allowed to attend
the mass if only 50% of the total capacity is mandated. Use:
C. Install C++ compiler in your pc, laptop or smart phone. Steps on how to install are
available online.
https://favtutor.com/blogs/best-ide-
cpp#:~:text=VSCode%3A%20From%20a%20beginner's%20point,many%20other%20langua
ges%20as%20well.
https://www.youtube.com/watch?v=F9LcfFlDIJs
16
C Language and Its Structure
Online References
https://www.guru99.com/cpp-tutorial.html#3
https://ecomputernotes.com/cpp/
https://www.geeksforgeeks.org/history/
17