0% found this document useful (0 votes)
35 views10 pages

CP1 - Unit 2 - C ProgLag and Its Structure

The document discusses the C programming language and its structure. It describes how C was initially developed in 1972 as a system programming language to write operating systems like UNIX. It then summarizes the basic structure of a C++ program, including headers, namespaces, the main function, and input/output statements. Finally, it provides details on specific lines of a sample "Hello World" C++ program to illustrate how each part of the structure works.

Uploaded by

Nathaniel Napay
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)
35 views10 pages

CP1 - Unit 2 - C ProgLag and Its Structure

The document discusses the C programming language and its structure. It describes how C was initially developed in 1972 as a system programming language to write operating systems like UNIX. It then summarizes the basic structure of a C++ program, including headers, namespaces, the main function, and input/output statements. Finally, it provides details on specific lines of a sample "Hello World" C++ program to illustrate how each part of the structure works.

Uploaded by

Nathaniel Napay
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
You are on page 1/ 10

C Language and Its Structure

UNIT 2: C Programming Language and Its Structure

Introduction

C is a general-purpose and procedural programming language. It was initially developed


by Dennis Ritchie in the year 1972 at the Bell Telephone Laboratories. It was mainly developed
as a system programming language to write an operating system (UNIX OS). C language is still
one of the most popular and most widely used among computer programming languages.
Among the reasons why C is popular are: it is machine-independent, easy to learn, can handle
low-level activities and produces efficient programs. It is a must for students and software
professionals to learn C programming if they want to develop other systems software.

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++ is an improved version of C language. It has additional features such as type


checking, object-oriented programming, exception handling etc. It was developed by Bjarne
Stroustrup starting in 1979 at Bell Labs.

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

After successful completion of this lesson, you should be able to:

1. State some of the uses of C programming language.


2. Identify the basic parts of the program written in C++.
3. Describe the function of each part of the program structure.
4. Write, compile and run your first program.

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

2.2 Who uses C++? https://www.guru99.com/cpp-tutorial.html#3

Some of today's most visible used systems have their critical parts written in C++.

Examples are Amadeus (airline ticketing)

 Bloomberg (financial formation),


 Amazon (Web commerce), Google (Web search)
 Facebook (social media)

Many programming languages depend on C++'s performance and reliability in their


implementation. Examples include:

 Java Virtual Machines


 JavaScript interpreters (e.g., Google's V8)
 Browsers (e.g., Internet Explorer, Mozilla's Firefox, Apple's Safari, and Google's
Chrome)
 Application and Web frameworks (e.g., Microsoft's .NET Web services framework).

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

2.3 Basic Structure of C++ Language

A program is composed of sequence of statements or instructions. You use a text editor


to type or encode your program and then save it on your storage medium (e.g. hard disk or flash
dish), this is now your source code. C++ is a compiled language. The source code is compiled
into object files. Object files are then combined by a linker creating an executable program.

Consider the famous “Hello World!” program of C language, and let’s examine every line
of it.

1 // my first C++ program


2
3 #include<iostream>
4
5 using namespace std;
6
7 int main()
8 {
9 cout << "Hello World!" << endl;
10 return 0;
11 }

C++ program structure is divided into various sections, namely:

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.

Let us now examine our Hello World program.

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!”;

a=b+c/d; is harder to read than a = b + c / d;

int main(){ looks cramp without proper indention


cout << "Hello World!"<<endl; and spacing
return 0;
}

cout << "Hello world!\n"; // cout resides in the iostream library


cout << "It is my first time here.\n"; // these comments make the code hard to read
cout << "Cheers!\n"; // especially when lines are different lengths

is more readable than

cout << "Hello world!\n"; // cout resides in the iostream library


cout << "It is my first time here.\n"; // these comments make the code hard to read
cout << "Cheers!\n"; // especially when lines are different lengths

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

A program includes various elements like built-in functions, classes, keywords,


constants, operators, etc., that are already defined in the standard C++ library. In order to
use such pre-defined elements in a program, an appropriate header must be included in
the program. The standard headers contain the information like prototype, definition and
return type of library functions, data type of constants, etc. As a result, programmers do
not need to explicitly declare (or define) the predefined programming elements.
Standard headers are specified in a program through the preprocessor directive
#include. In our program, the iostream header is used. When the compiler processes the
instruction #include<iostream>, it includes the contents of iostream in the program. This
enables the programmer to use standard input, output and error facilities that are
provided only through the standard streams defined in <iostream>.

https://ecomputernotes.com/cpp/introduction-to-oop/structure-of-a-cpp

11
C Language and Its Structure

Line 5: using namespace std;

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";

• By specifying the full member name std: :cout<<"Hello World";

• By specifying the using declaration using std:: cout;


cout<<"Hello World";
As soon as the new-style header is included, its contents are included in the std
namespace. Thus, all the modern C++ compilers support these statements:
#include<iostream>
using namespace std;
However, some old compilers may not support these statements. In that case, the
statements are replaced by this single statement.
#include<iostream.h>

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:

Below matching {} are aligned,

int main()
{
for ( ; ; )
{
statement;
if ( condition)
{
statement;
}
else
{
statement;
}
statement;
}
}

Compared to:

int main() {

for ( ; ; ){
statement;
if ( condition) {
statement;
}
else {
statement;
}
statement;
}
}

It is hard to look for the matching {}.

13
C Language and Its Structure

Line 9: cout << “Hello World!” << endl;

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:

cout << “Hello World!” << endl;


cout << “Here in Laguna.”;

output will be:

Hello World!
Here in Laguna.

cout << “Hello World!”;


cout << “Here in Laguna.”;

output will be:

Hello World!Here in Laguna.

Spaces within the double quotation marks are counted and displayed.

cout << “Hello World!” << endl;

will give a different output form

cout << “Hello World!” << endl;

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

Here is another example program,

1 // program that adds two integer values


2
3 #include<iostream>
4
5 using namespace std;
6
7 int main()
8 {
9 int a = 8;
10 int b = 10;
11 cout << "The sum of " << a << " + " << b << " is " << a+b;
12 return 0;
13 }

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.

1 // program that adds two integer values


2
3 #include<iostream>
4
5 using namespace std;
6
7 int main()
8 {
9 int a = 8;
10 int b = 10;
11 int c = 0;
12 c = a + b;
13 cout << "The sum of " << a << " + " << b << " is " << c;
14 return 0;
15 }

15
C Language and Its Structure

Activities

A. Answer the following:

1. What are the reasons why C language is still popular today?


2. Give examples of other software or applications not mention in this module that is written
using C or C++.
3. Enumerate the various sections of a C++ program.
4. What is wrong with the following program?

// program that converts inches to centimeter

#include<iosteam>

using namespace std;

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:

int totalCapacity = 325;

B. Read the following:

1. history of C++ in https://www.geeksforgeeks.org/history-of-c/


2. Why do we need using namespace std; https://medium.com/breaktheloop/why-
using-namespace-std-is-used-after-including-iostream-dc5ae45db652
3. Applications of C++ https://www.mycplus.com/featured-articles/top-10-applications-
written-in-c-cplusplus/

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

You might also like