SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Downloaden Sie, um offline zu lesen
1
FM-AA-CIA-15 Rev. 0 10-July-2020
PANGASINAN STATE UNIVERSITY
Study Guide in CC102 Fundamentals of Programming Module No. 2
STUDY GUIDE FOR MODULE NO. 2
BASIC PROGRAM STRUCTURE IN C
MODULE OVERVIEW
Welcome to this module! By the time that you are reading this, you have been immersed with the introductory
concepts of programming and started your adventurous journey in programming. Further, problem-solving steps
were introduced to you in the previous module. However, the best way to learn to program is through writing
programs directly. Thus, ready your tools and enjoy programming!
MODULE LEARNING OBJECTIVES
By the end of this module you should be able to:
▪ Dissect a basic C program structure
▪ Explain the use of various program statements such as input/output statements and comments
▪ Implement input/output statements and comments in creating a simple C program
LEARNING CONTENTS (BASIC PROGRAM STRUCTURE)
Introduction
Turbo C is commonly known as C language programming is a structure oriented language, developed by
Dennis Ritchie at Bell Laboratories in 1972.
C programs have a specific structure that you need to discover. It also features specific language syntax that
you need to follow. Step-by-step, you will learn what specific statement does. Please take the time to read the
discussions and try to run the actual codes.
1.1 General Structure
Let’s try writing our first program, the “Hello World” program in C. You may examine the following codes:
(save this program with file hello.c)
Coding with MS DOS turbo c++
Start by launching the MS DOS turbo c++. Select Start turbo C++. Create a new source file via selecting
File>New.
/*Example 1.1 Your very first C program-Displaying Hello World*/
#include<stdio.h>
void main()
{
printf(“Hello World!”);
}
2
FM-AA-CIA-15 Rev. 0 10-July-2020
PANGASINAN STATE UNIVERSITY
Study Guide in CC102 Fundamentals of Programming Module No. 2
Type your code in the source file
Save the source file as hello.c
Compile your source file to check for any errors, either thru:
• Alt + F9
• Compile > Compile
Run the C program to create an executive file and run the executable file ,either thru :
• Press Ctrl + F9
• Run > Run in menu bar
A console will be opened like one below:
3
FM-AA-CIA-15 Rev. 0 10-July-2020
PANGASINAN STATE UNIVERSITY
Study Guide in CC102 Fundamentals of Programming Module No. 2
1.2 Dissecting a Simple Program
Now that you’ve seen written and compiled your first program, let’s go through the program and see what the
individual lines of codes do.
Comments
Look at the first line.
/*Your very first C program-Displaying Hello World*/
This isn’t actually part of the program code, in that it isn’t telling the computer to do
anything. It’s simply a comment, and it’s there to remind you what the program does-so you don’t
have to wade through the code (and your memory) to remember. Anything between /* and */ is
treated as a comment.
Comments don’t have to be in line of their own; they just have to be enclosed between /* and */ .
Let’s add some comments to the program:
You can see that using comments can be very useful way of explaining, in English , what’s going on
in the program.
/*Example 1.1 Your very first C program-Displaying Hello World*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hello World!”);
getch();
}
/*Example 1.2 Your second C program-Displaying Hello Universe*/
#include<stdio.h> /* this is the preprocessor directive */
#include<conio.h>
void main() /* this is identifies the function main() */
{ /* this marks the beginning of main() */
clrscr();
printf(“Hello Universe!”);
getch();
} /* this marks the end of main() */
4
FM-AA-CIA-15 Rev. 0 10-July-2020
PANGASINAN STATE UNIVERSITY
Study Guide in CC102 Fundamentals of Programming Module No. 2
Pre-Processor Directives
Look at the line :
The symbol # indicates this is a pre-processor directives, which is an instruction to your
compiler to do something before compiling the source code.
#include<stdio.h> /* this is the preprocessor directive */
#include<conio.h> /* this is the preprocessor directive */
This is not strictly part of the executable program , but still essential in this case – in fact , the
program won’t work without it. The symbol # indicates this is a pre-processor directive, which is
an instruction to your compiler to do something before compiling the source code. The program that
handles these directives is a called a preprocessor because it does some processing before the
compilation process starts.
In this case , the compiler is instructed to ‘include’ in our program the file stdio.h and
conio.h. These files are called the header file that defines the information about functions ( in this
case printf(), clrscr(), and getch()) provided by the standard libraries. In this case , as we’re using the
printf() function from the standard library , we have to include stdio.h header file and clrscr(), and
getch() so we have to include conio.h).This is because stdio.h contains the information that the
compiler needs in order to understand what printf() means and conio.h contains the information
about clrscr(), and getch() .
List of inbuilt C functions in stdio.h file:
Function Description
printf() writes formatted data to a file
scanf() reads formatted data from a file
getchar reads a character from keyboard
putchar writes a character from keyboard
List of inbuilt C functions in conio.h file:
Functions Description
clrscr()
This function is used to clear the output
screen.
getch() It reads character from keyboard
getche()
It reads character from keyboard and
echoes to o/p screen
5
FM-AA-CIA-15 Rev. 0 10-July-2020
PANGASINAN STATE UNIVERSITY
Study Guide in CC102 Fundamentals of Programming Module No. 2
Defining the main() Function
The next statements define the function main();
Every C program consist of one or more functions , and every C program must contain a function
called main() – the reason being that your program will always start execution from the beginning
of this function.
The first line of the definition for the function main() is :
void main() /* this is identifies the function main() */
This defines the start of the function main. The keyword void defines the type of value to be returned
by the function. In this case, it signifies that the function main() returns no value.
Note : We add clrscr() function to to clear the output screen and getch() is used to read a single byte
character from input. It is most frequently used to hold a program waiting until a key is hit.
LEARNING CONTENTS (OUTPUT STATEMENTS)
2.1 The Body of the Function
The general structure of the function main() is illustrated here.
The function body is the bit between the opening and closing braces following the line where the function
name appears. The function body contains all the statements that define what the function does.
void main() /* this is identifies the function main() */
{ /* this marks the beginning of main() */
clrscr();
printf(“Hello Universe!”);
getch();
} /* this marks the end of main() */
6
FM-AA-CIA-15 Rev. 0 10-July-2020
PANGASINAN STATE UNIVERSITY
Study Guide in CC102 Fundamentals of Programming Module No. 2
A very simple function body consisting of just one statement is:
Every function must have a body, although it can be empty and just consist of the two braces without any
statements between them. In this case the function will do nothing.
2.2 Outputting Information
The body of our function main() only includes one statement , which calls the printf() function;
The function printf() is a standard library function , and it outputs information to the display screen based on
what’s contained between the parentheses immediately following the function name . In our case , the call to
the function displays a simple piece of Shakespearean advice . Notice that that this line does end with a
semicolon.
2.3 Arguments
Items enclosed between the parentheses following a function name , as we have with the printf() function ,
are called arguments .
If you don’t like the quotation we have as an argument, you could display something else by simply including
your own choice of words, within quotes, inside parentheses . For instance , you might prefer a line from
Macbeth :
{ /* this marks the beginning of main() *
printf(“Beware the Ides of March!”); /* this line displays a quotation */
} /* this marks the end of main() */
printf(“Beware the Ides of March!”); /* this line displays a quotation */
7
FM-AA-CIA-15 Rev. 0 10-July-2020
PANGASINAN STATE UNIVERSITY
Study Guide in CC102 Fundamentals of Programming Module No. 2
Try using this in the example . When you’ve modified the source code, you need to compile the program
again before executing it.
2.4 Control Characters
We could alter our program to display two sentences on separate lines . Try typing in the following code:
The output looks like this:
My formula for success?
Rise early, work late, strike oil.
Look at the printf() statement . After the first sentence we have inserted the characters n. The combination n
actually represents a single character.
The backslash () is a special significance here . The character immediately following a backslash is always
interpreted as a control character . In this case , it’s n for newline, but there are plenty more .
The combination of  plus another character is referred to as an escape sequence . The next table shows a
summary of escape sequence that you can use.
Escape
Sequence
Action
n Inserts a newline character
t Inserts a horizontal tab
a Makes a beep
” Inserts a double quote ( “ )
’ Inserts a single quote ( ‘ )
 Inserts a backslash
b Inserts a backspace character
printf(“Out, dammed Spot! Out I say!”); /* this line displays a quotation */
/*Example 1.3 Another C program- Displaying Great Quotations*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“nMy formula for success?nRise early, work late, strike oil.”);
getch();
}
8
FM-AA-CIA-15 Rev. 0 10-July-2020
PANGASINAN STATE UNIVERSITY
Study Guide in CC102 Fundamentals of Programming Module No. 2
Type in the following program :
The output displays the text :
“It is wise father that knows his own child. “Shakespeare
LEARNING ACTIVITY 1
1. You are to write your own program given the output below . Save it with a file name : A1_yourlastname
Programmer : LastName, Firstname
Hi there !
This program is a bit longer than the others.
But really it’s only more text .
Hey, wait a minute!!What was that???
1. A bird
2. A plane?
3. A control character?
“And how will this look when it prints out?”
TIVITY 2
/*Example 1.4 Another simple C program- Displaying Great Quotations*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“n”It is a wise father that knows his own child .”Shakespeare”);
getch();
}
9
FM-AA-CIA-15 Rev. 0 10-July-2020
PANGASINAN STATE UNIVERSITY
Study Guide in CC102 Fundamentals of Programming Module No. 2
LEARNING ACTVITY 2
Instruction 1 : Write a program that produces the following output: (Save the program with file name : A2_yourlastname)
* * * * * * * * * * * * * * * * * * * * *********************
* Fundamental Concept of Programming 1 *
* *
* Author: ??? *
* Due Date: !!! *
* * * * * * * * * * * * * * * * * * * * *********************
In your program, substitute ??? with your name. If necessary, adjust the positions and the number of stars to produce a
rectangle. Substitute !!! with the due date indicated on your assignment in MS teams .
After finishing instruction 1,on the same program add a code that produces the following output:
CCCCCCCCC CCCCCCCCC ++++++++++
CC CC ++
CC CC +++++++++
CC CC ++
CC CC ++
CCCCCCCCC CCCCCCCCC ++++++++++
SUMMARY
In this section, we have covered the basic program structure for creating C programs. Try to remember the basic program
structure as we will be using them in the next modules. You should be confident about editing, compiling and running
your programs. You’re probably a bit fed up with printf() function – all it does, so far, is display what you type between
parentheses .
REFERENCES
Zak, D. (2016). An Introduction to Programming with C++ (8E), pages 23-45
Horton,I, Beginning C.
Online Reading Materials:
• http://cplusplus.com/doc/tutorial/program_structure/
• http://cplusplus.com/doc/tutorial/basic_io/
• https://www.learncpp.com/cpp-tutorial/statements-and-the-structure-of-a-program/
• https://www.learncpp.com/cpp-tutorial/comments/
• https://www.programiz.com/cpp-programming/input-output
• https://www.programiz.com/cpp-programming/comments

Weitere ähnliche Inhalte

Ähnlich wie CC 3 - Module 2.pdf

Ch2 C Fundamentals
Ch2 C FundamentalsCh2 C Fundamentals
Ch2 C FundamentalsSzeChingChen
 
Stucture of c program
Stucture of c programStucture of c program
Stucture of c programVpmv
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c languagefarishah
 
My first program in c, hello world !
My first program in c, hello world !My first program in c, hello world !
My first program in c, hello world !Rumman Ansari
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiSowmya Jyothi
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2Rumman Ansari
 
ICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdfICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdfssuser33f16f
 
ProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdfProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdflailoesakhan
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg PatelTechNGyan
 
2.Overview of C language.pptx
2.Overview of C language.pptx2.Overview of C language.pptx
2.Overview of C language.pptxVishwas459764
 
1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docxtarifarmarie
 

Ähnlich wie CC 3 - Module 2.pdf (20)

C PROGRAMMING p-2.pdf
C PROGRAMMING p-2.pdfC PROGRAMMING p-2.pdf
C PROGRAMMING p-2.pdf
 
Ch2 C Fundamentals
Ch2 C FundamentalsCh2 C Fundamentals
Ch2 C Fundamentals
 
OVERVIEW OF ‘C’ PROGRAM
OVERVIEW OF ‘C’ PROGRAMOVERVIEW OF ‘C’ PROGRAM
OVERVIEW OF ‘C’ PROGRAM
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
Stucture of c program
Stucture of c programStucture of c program
Stucture of c program
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 
C tutorials
C tutorialsC tutorials
C tutorials
 
My first program in c, hello world !
My first program in c, hello world !My first program in c, hello world !
My first program in c, hello world !
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2
 
ICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdfICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdf
 
ProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdfProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdf
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
PPs16.pptx
PPs16.pptxPPs16.pptx
PPs16.pptx
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Basics of c Nisarg Patel
Basics of c Nisarg PatelBasics of c Nisarg Patel
Basics of c Nisarg Patel
 
2.Overview of C language.pptx
2.Overview of C language.pptx2.Overview of C language.pptx
2.Overview of C language.pptx
 
1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx
 
C++ Chapter 3
C++ Chapter 3C++ Chapter 3
C++ Chapter 3
 
Intro To C++ - Class 3 - Sample Program
Intro To C++ - Class 3 - Sample ProgramIntro To C++ - Class 3 - Sample Program
Intro To C++ - Class 3 - Sample Program
 

Kürzlich hochgeladen

NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...Amil Baba Dawood bangali
 
The Triple Threat | Article on Global Resession | Harsh Kumar
The Triple Threat | Article on Global Resession | Harsh KumarThe Triple Threat | Article on Global Resession | Harsh Kumar
The Triple Threat | Article on Global Resession | Harsh KumarHarsh Kumar
 
House of Commons ; CDC schemes overview document
House of Commons ; CDC schemes overview documentHouse of Commons ; CDC schemes overview document
House of Commons ; CDC schemes overview documentHenry Tapper
 
Bladex 1Q24 Earning Results Presentation
Bladex 1Q24 Earning Results PresentationBladex 1Q24 Earning Results Presentation
Bladex 1Q24 Earning Results PresentationBladex
 
(办理学位证)加拿大萨省大学毕业证成绩单原版一比一
(办理学位证)加拿大萨省大学毕业证成绩单原版一比一(办理学位证)加拿大萨省大学毕业证成绩单原版一比一
(办理学位证)加拿大萨省大学毕业证成绩单原版一比一S SDS
 
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...Amil baba
 
Call Girls Near Me WhatsApp:+91-9833363713
Call Girls Near Me WhatsApp:+91-9833363713Call Girls Near Me WhatsApp:+91-9833363713
Call Girls Near Me WhatsApp:+91-9833363713Sonam Pathan
 
Vp Girls near me Delhi Call Now or WhatsApp
Vp Girls near me Delhi Call Now or WhatsAppVp Girls near me Delhi Call Now or WhatsApp
Vp Girls near me Delhi Call Now or WhatsAppmiss dipika
 
fca-bsps-decision-letter-redacted (1).pdf
fca-bsps-decision-letter-redacted (1).pdffca-bsps-decision-letter-redacted (1).pdf
fca-bsps-decision-letter-redacted (1).pdfHenry Tapper
 
BPPG response - Options for Defined Benefit schemes - 19Apr24.pdf
BPPG response - Options for Defined Benefit schemes - 19Apr24.pdfBPPG response - Options for Defined Benefit schemes - 19Apr24.pdf
BPPG response - Options for Defined Benefit schemes - 19Apr24.pdfHenry Tapper
 
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...Henry Tapper
 
Bladex Earnings Call Presentation 1Q2024
Bladex Earnings Call Presentation 1Q2024Bladex Earnings Call Presentation 1Q2024
Bladex Earnings Call Presentation 1Q2024Bladex
 
AfRESFullPaper22018EmpiricalPerformanceofRealEstateInvestmentTrustsandShareho...
AfRESFullPaper22018EmpiricalPerformanceofRealEstateInvestmentTrustsandShareho...AfRESFullPaper22018EmpiricalPerformanceofRealEstateInvestmentTrustsandShareho...
AfRESFullPaper22018EmpiricalPerformanceofRealEstateInvestmentTrustsandShareho...yordanosyohannes2
 
Financial Leverage Definition, Advantages, and Disadvantages
Financial Leverage Definition, Advantages, and DisadvantagesFinancial Leverage Definition, Advantages, and Disadvantages
Financial Leverage Definition, Advantages, and Disadvantagesjayjaymabutot13
 
Call Girls Near Golden Tulip Essential Hotel, New Delhi 9873777170
Call Girls Near Golden Tulip Essential Hotel, New Delhi 9873777170Call Girls Near Golden Tulip Essential Hotel, New Delhi 9873777170
Call Girls Near Golden Tulip Essential Hotel, New Delhi 9873777170Sonam Pathan
 
《加拿大本地办假证-寻找办理Dalhousie毕业证和达尔豪斯大学毕业证书的中介代理》
《加拿大本地办假证-寻找办理Dalhousie毕业证和达尔豪斯大学毕业证书的中介代理》《加拿大本地办假证-寻找办理Dalhousie毕业证和达尔豪斯大学毕业证书的中介代理》
《加拿大本地办假证-寻找办理Dalhousie毕业证和达尔豪斯大学毕业证书的中介代理》rnrncn29
 
Stock Market Brief Deck for "this does not happen often".pdf
Stock Market Brief Deck for "this does not happen often".pdfStock Market Brief Deck for "this does not happen often".pdf
Stock Market Brief Deck for "this does not happen often".pdfMichael Silva
 
Quantitative Analysis of Retail Sector Companies
Quantitative Analysis of Retail Sector CompaniesQuantitative Analysis of Retail Sector Companies
Quantitative Analysis of Retail Sector Companiesprashantbhati354
 
PMFBY , Pradhan Mantri Fasal bima yojna
PMFBY , Pradhan Mantri  Fasal bima yojnaPMFBY , Pradhan Mantri  Fasal bima yojna
PMFBY , Pradhan Mantri Fasal bima yojnaDharmendra Kumar
 

Kürzlich hochgeladen (20)

NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
NO1 Certified Ilam kala Jadu Specialist Expert In Bahawalpur, Sargodha, Sialk...
 
The Triple Threat | Article on Global Resession | Harsh Kumar
The Triple Threat | Article on Global Resession | Harsh KumarThe Triple Threat | Article on Global Resession | Harsh Kumar
The Triple Threat | Article on Global Resession | Harsh Kumar
 
House of Commons ; CDC schemes overview document
House of Commons ; CDC schemes overview documentHouse of Commons ; CDC schemes overview document
House of Commons ; CDC schemes overview document
 
🔝+919953056974 🔝young Delhi Escort service Pusa Road
🔝+919953056974 🔝young Delhi Escort service Pusa Road🔝+919953056974 🔝young Delhi Escort service Pusa Road
🔝+919953056974 🔝young Delhi Escort service Pusa Road
 
Bladex 1Q24 Earning Results Presentation
Bladex 1Q24 Earning Results PresentationBladex 1Q24 Earning Results Presentation
Bladex 1Q24 Earning Results Presentation
 
(办理学位证)加拿大萨省大学毕业证成绩单原版一比一
(办理学位证)加拿大萨省大学毕业证成绩单原版一比一(办理学位证)加拿大萨省大学毕业证成绩单原版一比一
(办理学位证)加拿大萨省大学毕业证成绩单原版一比一
 
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
NO1 WorldWide Love marriage specialist baba ji Amil Baba Kala ilam powerful v...
 
Call Girls Near Me WhatsApp:+91-9833363713
Call Girls Near Me WhatsApp:+91-9833363713Call Girls Near Me WhatsApp:+91-9833363713
Call Girls Near Me WhatsApp:+91-9833363713
 
Vp Girls near me Delhi Call Now or WhatsApp
Vp Girls near me Delhi Call Now or WhatsAppVp Girls near me Delhi Call Now or WhatsApp
Vp Girls near me Delhi Call Now or WhatsApp
 
fca-bsps-decision-letter-redacted (1).pdf
fca-bsps-decision-letter-redacted (1).pdffca-bsps-decision-letter-redacted (1).pdf
fca-bsps-decision-letter-redacted (1).pdf
 
BPPG response - Options for Defined Benefit schemes - 19Apr24.pdf
BPPG response - Options for Defined Benefit schemes - 19Apr24.pdfBPPG response - Options for Defined Benefit schemes - 19Apr24.pdf
BPPG response - Options for Defined Benefit schemes - 19Apr24.pdf
 
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
letter-from-the-chair-to-the-fca-relating-to-british-steel-pensions-scheme-15...
 
Bladex Earnings Call Presentation 1Q2024
Bladex Earnings Call Presentation 1Q2024Bladex Earnings Call Presentation 1Q2024
Bladex Earnings Call Presentation 1Q2024
 
AfRESFullPaper22018EmpiricalPerformanceofRealEstateInvestmentTrustsandShareho...
AfRESFullPaper22018EmpiricalPerformanceofRealEstateInvestmentTrustsandShareho...AfRESFullPaper22018EmpiricalPerformanceofRealEstateInvestmentTrustsandShareho...
AfRESFullPaper22018EmpiricalPerformanceofRealEstateInvestmentTrustsandShareho...
 
Financial Leverage Definition, Advantages, and Disadvantages
Financial Leverage Definition, Advantages, and DisadvantagesFinancial Leverage Definition, Advantages, and Disadvantages
Financial Leverage Definition, Advantages, and Disadvantages
 
Call Girls Near Golden Tulip Essential Hotel, New Delhi 9873777170
Call Girls Near Golden Tulip Essential Hotel, New Delhi 9873777170Call Girls Near Golden Tulip Essential Hotel, New Delhi 9873777170
Call Girls Near Golden Tulip Essential Hotel, New Delhi 9873777170
 
《加拿大本地办假证-寻找办理Dalhousie毕业证和达尔豪斯大学毕业证书的中介代理》
《加拿大本地办假证-寻找办理Dalhousie毕业证和达尔豪斯大学毕业证书的中介代理》《加拿大本地办假证-寻找办理Dalhousie毕业证和达尔豪斯大学毕业证书的中介代理》
《加拿大本地办假证-寻找办理Dalhousie毕业证和达尔豪斯大学毕业证书的中介代理》
 
Stock Market Brief Deck for "this does not happen often".pdf
Stock Market Brief Deck for "this does not happen often".pdfStock Market Brief Deck for "this does not happen often".pdf
Stock Market Brief Deck for "this does not happen often".pdf
 
Quantitative Analysis of Retail Sector Companies
Quantitative Analysis of Retail Sector CompaniesQuantitative Analysis of Retail Sector Companies
Quantitative Analysis of Retail Sector Companies
 
PMFBY , Pradhan Mantri Fasal bima yojna
PMFBY , Pradhan Mantri  Fasal bima yojnaPMFBY , Pradhan Mantri  Fasal bima yojna
PMFBY , Pradhan Mantri Fasal bima yojna
 

CC 3 - Module 2.pdf

  • 1. 1 FM-AA-CIA-15 Rev. 0 10-July-2020 PANGASINAN STATE UNIVERSITY Study Guide in CC102 Fundamentals of Programming Module No. 2 STUDY GUIDE FOR MODULE NO. 2 BASIC PROGRAM STRUCTURE IN C MODULE OVERVIEW Welcome to this module! By the time that you are reading this, you have been immersed with the introductory concepts of programming and started your adventurous journey in programming. Further, problem-solving steps were introduced to you in the previous module. However, the best way to learn to program is through writing programs directly. Thus, ready your tools and enjoy programming! MODULE LEARNING OBJECTIVES By the end of this module you should be able to: ▪ Dissect a basic C program structure ▪ Explain the use of various program statements such as input/output statements and comments ▪ Implement input/output statements and comments in creating a simple C program LEARNING CONTENTS (BASIC PROGRAM STRUCTURE) Introduction Turbo C is commonly known as C language programming is a structure oriented language, developed by Dennis Ritchie at Bell Laboratories in 1972. C programs have a specific structure that you need to discover. It also features specific language syntax that you need to follow. Step-by-step, you will learn what specific statement does. Please take the time to read the discussions and try to run the actual codes. 1.1 General Structure Let’s try writing our first program, the “Hello World” program in C. You may examine the following codes: (save this program with file hello.c) Coding with MS DOS turbo c++ Start by launching the MS DOS turbo c++. Select Start turbo C++. Create a new source file via selecting File>New. /*Example 1.1 Your very first C program-Displaying Hello World*/ #include<stdio.h> void main() { printf(“Hello World!”); }
  • 2. 2 FM-AA-CIA-15 Rev. 0 10-July-2020 PANGASINAN STATE UNIVERSITY Study Guide in CC102 Fundamentals of Programming Module No. 2 Type your code in the source file Save the source file as hello.c Compile your source file to check for any errors, either thru: • Alt + F9 • Compile > Compile Run the C program to create an executive file and run the executable file ,either thru : • Press Ctrl + F9 • Run > Run in menu bar A console will be opened like one below:
  • 3. 3 FM-AA-CIA-15 Rev. 0 10-July-2020 PANGASINAN STATE UNIVERSITY Study Guide in CC102 Fundamentals of Programming Module No. 2 1.2 Dissecting a Simple Program Now that you’ve seen written and compiled your first program, let’s go through the program and see what the individual lines of codes do. Comments Look at the first line. /*Your very first C program-Displaying Hello World*/ This isn’t actually part of the program code, in that it isn’t telling the computer to do anything. It’s simply a comment, and it’s there to remind you what the program does-so you don’t have to wade through the code (and your memory) to remember. Anything between /* and */ is treated as a comment. Comments don’t have to be in line of their own; they just have to be enclosed between /* and */ . Let’s add some comments to the program: You can see that using comments can be very useful way of explaining, in English , what’s going on in the program. /*Example 1.1 Your very first C program-Displaying Hello World*/ #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“Hello World!”); getch(); } /*Example 1.2 Your second C program-Displaying Hello Universe*/ #include<stdio.h> /* this is the preprocessor directive */ #include<conio.h> void main() /* this is identifies the function main() */ { /* this marks the beginning of main() */ clrscr(); printf(“Hello Universe!”); getch(); } /* this marks the end of main() */
  • 4. 4 FM-AA-CIA-15 Rev. 0 10-July-2020 PANGASINAN STATE UNIVERSITY Study Guide in CC102 Fundamentals of Programming Module No. 2 Pre-Processor Directives Look at the line : The symbol # indicates this is a pre-processor directives, which is an instruction to your compiler to do something before compiling the source code. #include<stdio.h> /* this is the preprocessor directive */ #include<conio.h> /* this is the preprocessor directive */ This is not strictly part of the executable program , but still essential in this case – in fact , the program won’t work without it. The symbol # indicates this is a pre-processor directive, which is an instruction to your compiler to do something before compiling the source code. The program that handles these directives is a called a preprocessor because it does some processing before the compilation process starts. In this case , the compiler is instructed to ‘include’ in our program the file stdio.h and conio.h. These files are called the header file that defines the information about functions ( in this case printf(), clrscr(), and getch()) provided by the standard libraries. In this case , as we’re using the printf() function from the standard library , we have to include stdio.h header file and clrscr(), and getch() so we have to include conio.h).This is because stdio.h contains the information that the compiler needs in order to understand what printf() means and conio.h contains the information about clrscr(), and getch() . List of inbuilt C functions in stdio.h file: Function Description printf() writes formatted data to a file scanf() reads formatted data from a file getchar reads a character from keyboard putchar writes a character from keyboard List of inbuilt C functions in conio.h file: Functions Description clrscr() This function is used to clear the output screen. getch() It reads character from keyboard getche() It reads character from keyboard and echoes to o/p screen
  • 5. 5 FM-AA-CIA-15 Rev. 0 10-July-2020 PANGASINAN STATE UNIVERSITY Study Guide in CC102 Fundamentals of Programming Module No. 2 Defining the main() Function The next statements define the function main(); Every C program consist of one or more functions , and every C program must contain a function called main() – the reason being that your program will always start execution from the beginning of this function. The first line of the definition for the function main() is : void main() /* this is identifies the function main() */ This defines the start of the function main. The keyword void defines the type of value to be returned by the function. In this case, it signifies that the function main() returns no value. Note : We add clrscr() function to to clear the output screen and getch() is used to read a single byte character from input. It is most frequently used to hold a program waiting until a key is hit. LEARNING CONTENTS (OUTPUT STATEMENTS) 2.1 The Body of the Function The general structure of the function main() is illustrated here. The function body is the bit between the opening and closing braces following the line where the function name appears. The function body contains all the statements that define what the function does. void main() /* this is identifies the function main() */ { /* this marks the beginning of main() */ clrscr(); printf(“Hello Universe!”); getch(); } /* this marks the end of main() */
  • 6. 6 FM-AA-CIA-15 Rev. 0 10-July-2020 PANGASINAN STATE UNIVERSITY Study Guide in CC102 Fundamentals of Programming Module No. 2 A very simple function body consisting of just one statement is: Every function must have a body, although it can be empty and just consist of the two braces without any statements between them. In this case the function will do nothing. 2.2 Outputting Information The body of our function main() only includes one statement , which calls the printf() function; The function printf() is a standard library function , and it outputs information to the display screen based on what’s contained between the parentheses immediately following the function name . In our case , the call to the function displays a simple piece of Shakespearean advice . Notice that that this line does end with a semicolon. 2.3 Arguments Items enclosed between the parentheses following a function name , as we have with the printf() function , are called arguments . If you don’t like the quotation we have as an argument, you could display something else by simply including your own choice of words, within quotes, inside parentheses . For instance , you might prefer a line from Macbeth : { /* this marks the beginning of main() * printf(“Beware the Ides of March!”); /* this line displays a quotation */ } /* this marks the end of main() */ printf(“Beware the Ides of March!”); /* this line displays a quotation */
  • 7. 7 FM-AA-CIA-15 Rev. 0 10-July-2020 PANGASINAN STATE UNIVERSITY Study Guide in CC102 Fundamentals of Programming Module No. 2 Try using this in the example . When you’ve modified the source code, you need to compile the program again before executing it. 2.4 Control Characters We could alter our program to display two sentences on separate lines . Try typing in the following code: The output looks like this: My formula for success? Rise early, work late, strike oil. Look at the printf() statement . After the first sentence we have inserted the characters n. The combination n actually represents a single character. The backslash () is a special significance here . The character immediately following a backslash is always interpreted as a control character . In this case , it’s n for newline, but there are plenty more . The combination of plus another character is referred to as an escape sequence . The next table shows a summary of escape sequence that you can use. Escape Sequence Action n Inserts a newline character t Inserts a horizontal tab a Makes a beep ” Inserts a double quote ( “ ) ’ Inserts a single quote ( ‘ ) Inserts a backslash b Inserts a backspace character printf(“Out, dammed Spot! Out I say!”); /* this line displays a quotation */ /*Example 1.3 Another C program- Displaying Great Quotations*/ #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“nMy formula for success?nRise early, work late, strike oil.”); getch(); }
  • 8. 8 FM-AA-CIA-15 Rev. 0 10-July-2020 PANGASINAN STATE UNIVERSITY Study Guide in CC102 Fundamentals of Programming Module No. 2 Type in the following program : The output displays the text : “It is wise father that knows his own child. “Shakespeare LEARNING ACTIVITY 1 1. You are to write your own program given the output below . Save it with a file name : A1_yourlastname Programmer : LastName, Firstname Hi there ! This program is a bit longer than the others. But really it’s only more text . Hey, wait a minute!!What was that??? 1. A bird 2. A plane? 3. A control character? “And how will this look when it prints out?” TIVITY 2 /*Example 1.4 Another simple C program- Displaying Great Quotations*/ #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“n”It is a wise father that knows his own child .”Shakespeare”); getch(); }
  • 9. 9 FM-AA-CIA-15 Rev. 0 10-July-2020 PANGASINAN STATE UNIVERSITY Study Guide in CC102 Fundamentals of Programming Module No. 2 LEARNING ACTVITY 2 Instruction 1 : Write a program that produces the following output: (Save the program with file name : A2_yourlastname) * * * * * * * * * * * * * * * * * * * * ********************* * Fundamental Concept of Programming 1 * * * * Author: ??? * * Due Date: !!! * * * * * * * * * * * * * * * * * * * * * ********************* In your program, substitute ??? with your name. If necessary, adjust the positions and the number of stars to produce a rectangle. Substitute !!! with the due date indicated on your assignment in MS teams . After finishing instruction 1,on the same program add a code that produces the following output: CCCCCCCCC CCCCCCCCC ++++++++++ CC CC ++ CC CC +++++++++ CC CC ++ CC CC ++ CCCCCCCCC CCCCCCCCC ++++++++++ SUMMARY In this section, we have covered the basic program structure for creating C programs. Try to remember the basic program structure as we will be using them in the next modules. You should be confident about editing, compiling and running your programs. You’re probably a bit fed up with printf() function – all it does, so far, is display what you type between parentheses . REFERENCES Zak, D. (2016). An Introduction to Programming with C++ (8E), pages 23-45 Horton,I, Beginning C. Online Reading Materials: • http://cplusplus.com/doc/tutorial/program_structure/ • http://cplusplus.com/doc/tutorial/basic_io/ • https://www.learncpp.com/cpp-tutorial/statements-and-the-structure-of-a-program/ • https://www.learncpp.com/cpp-tutorial/comments/ • https://www.programiz.com/cpp-programming/input-output • https://www.programiz.com/cpp-programming/comments