Publicidad
CC 3 - Module 2.pdf
CC 3 - Module 2.pdf
CC 3 - Module 2.pdf
CC 3 - Module 2.pdf
Publicidad
CC 3 - Module 2.pdf
CC 3 - Module 2.pdf
CC 3 - Module 2.pdf
CC 3 - Module 2.pdf
CC 3 - Module 2.pdf
Próximo SlideShare
C structureC structure
Cargando en ... 3
1 de 9
Publicidad

Más contenido relacionado

Publicidad

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
Publicidad