Programming I Laboratory - lesson 0 Introduction in C++ programming environment. The C++ program structure. Computer programs are composed of instructions and data. Instructions tell the computer to do things, such as to add and subtract. Data is what the computer operates on (such as the numbers that are added and subtracted). A solution to a problem is called an algorithm. It describes the sequence of steps to be performed for the problem to be solved. To be intelligible to a computer, an algorithm needs to be expressed in machine language, a language understood by it. Programs expressed in the machine language are said to be executable. A program written in any language needs to be translated to the machine language before it can be executed. The Development Environment The compiler may have its own built-in text editor, or you may be using a commercial text editor or word processor that can produce text files. The important thing is that whatever you write your program in, it must save simple, plain-text files, with no word processing commands embedded in the text. Examples of safe editors include Windows Notepad, etc. The files you create with your editor are called source files, and for C/C++ they typically are named with the extension.cpp,.cp, or.c. (We will name all the source code files with the.cpp extension.) Compiling the Source Code To turn your source code into a program, you use a compiler. How you invoke your compiler, and how you tell it where to find your source code, will vary from compiler to compiler; check your documentation. Compilers may do things slightly differently. After your source code is compiled, an object file is produced. C/C++ programs are typically created by linking together one or more OBJ files with one or more libraries. A library is a collection of linkable files that were supplied with your compiler, that you purchased separately, or that you created and compiled. All C/C++ compilers come with a library of useful functions (or procedures) and classes that you can include in your program. The steps to create an executable file are:. Create a source code file, with.c or.cpp extension.. Compile the source code into an object file.. Link your OBJ file with any needed libraries to produce a file with the.exe extension that is an executable program. Figure
Programming I Laboratory - lesson 0 A Simple C++ Program Figure Traditional programming books begin by writing the words Hello World to the screen, or a variation on that statement. Preprocessing directive Listing Error! No text of specified style in document.. First.cpp includes an header file that contains definitions for input/output functions Body of function main(). printf("hello World!\n"); Header of function main() Comments: First line uses the preprocessor directive #include to include the contents of the header file stdio.h in the program. stdio.h is a standard C/C++ header file and contains definitions for input and output. Line defines a function called main(). A function may have zero or more parameters; these always appear after the function name, between a pair of parenthesis. All C++ programs must have exactly one main function. When your program starts, main() is called automatically. This function is unique (it cannot be overloaded); main is a reserved word, but we can use as different identifier Main, MAIN, so on (languages C/C++ are case-sensitive); main is the starting point of execution for all C and C++ programs function main can have parameters or not; A program usually stops executing at the end of main if it is not a decision to stop it in other point, e.g. using the statement return or function exit. The word void appearing between the brackets indicates that main has no parameters.
Programming I Laboratory - lesson 0 A function may also have a return type; this always appears before the function name. The return type for main is int (an integer number). In line, the brace marks the beginning of main body. Line is a statement. A statement is a computation step which may produce a value. It consist in the call of the function printf(), defined in stdio.h. The end of a statement is always marked with a semicolon (;). The printf() function causes the string"hello World\n" to be sent to the output stream (stdout - standard output usually means your computer monitor screen). A string is any sequence of characters enclosed in double-quotes, like Hello world. The last character in this string (\n) is a newline character (is similar to a carriage return on a type writer). In line, the brace marks the end of the body of main. The preprocessor runs before your compiler each time the compiler is invoked. The preprocessor translates any line that begins with a pound symbol (#) into a special command, getting your code file ready for the compiler. Try running FIRST.EXE; it should produce the dialog : Dialog Since C program execution happen in milliseconds, we cannot see the output ( Hellow World ) on the screen. That s because Program output window closes immediately after execution. Listing. First.cpp - modified 8 9 #include <conio.h> printf("hello World!\n"); getch(); Contains definition of printf() Contains definition of getch() So we need a way to stop the program before the output window closing. We can use a function named getch() that will wait for key press in our program execution and hence, we can see the result Hellow World. After viewing the output we can close the output by pressing any key in the keyboard. Comments: To use getch() we need to include the header file conio.h that contains the definition of this function.
Programming I Laboratory - lesson 0 Compiler Errors Unfortunately, almost every program, no matter how trivial, can have errors, or bugs. Some bugs will cause the compile to fail, some will cause the link to fail, and some will only show up when you run the program. Whatever type of error or bug you find, you must fix it, and that involves editing your source code, re-compiling and re-linking, and then re-running the program. Compilers will tell you what you did wrong (they show error or warning code and its description), they'll point you to the exact place in your code where you made the mistake, even suggest a remedy. Listing is a demonstration of compiler error: Listing. Syntax error example printf("hello World!\n") Recompile your program and you should see in Message window an error that looks similar to the Dialog. Messages are written with the message class first, followed by the source file name and line number where the error was detected, and finally with the text of the message itself. Those messages are very useful tools to debug the program. Dialog. Error messages Compiling LISTING.CPP: C:\Test\listing.cpp() : error C: syntax error : missing ';' before 'return' C:\Test\listing.cpp() : fatal error C00: unexpected end of file found Error executing listing.exe. listing.exe - error(s), 0 warning(s) Compiler Errors and Warnings: Compile-time error messages indicate errors in program syntax, command-line errors, or errors in accessing a files or memory. When compile-time errors occur, the compiler completes the current phase of the compilation and stops. When fatal compile-time errors happen, compilation stops completely. If an error occurs, you must fix the error and restart compilation. Run-time errors occur after the program has successfully compiled and is running. Run-time errors are usually caused by logic errors in program code. Warnings indicate that conditions which are suspicious but legitimate exist. Warnings do not stop compilation. They must be attentively analyzed to decide if it is necessary to eliminate them or not. Linker Errors do not make the linker stop or cause.exe files to be deleted. When such errors happen, fix the error and re-link. A fatal linker error stops the linker immediately. In such a case, the.exe file is deleted.
Programming I Laboratory - lesson 0 Using Comments A source code can include text that is ignored by the compiler. This is called comment. Comments can come in two forms: multi-line comments (C style) and single-line comments (C++ style). C-style comments are framed between characters slash-star (/*) and star-slash (*/).The slash-star comment mark tells the compiler to ignore everything that follows until it finds a starslash (*/) comment mark. Every (/*) must be matched with a closing (*/). C++-style commentsuses the double-slash (//) characters and tells the compiler to ignore everything that follows this comment, until the end of the line. Many C++ programmers use the C++-style comment most of the time, and reserve C-style comments for blocking out large blocks of a program. The comments provide an easy way to set off specific parts of code, insert explanations about the code, as well as marking various parts of your code. As a general rule, the overall program should have comments at the beginning, telling you what the program does. Each function should also have comments explaining what the function does and what values it returns. Any statement in your program that is obscure or less than obvious should be commented as well. Listing demonstrates the use of comments, showing that they do not affect the processing of the program or its output. Listing. Using comments 8 9 0 8 9 0 /******************************************************** * Comments.CPP * * This is my program that demonstrates * * the use of comments. * ********************************************************/ int main (void) /* this is a comment on many lines and it extends until the closing star-slash comment mark */ printf("hello World\n"); //it prints on screen a message // this comment ends at the end of the line printf("hello World, again\n"); /*it prints on screen a message */ // this comment ends at the end of the line Comments: The comments on lines through, through are completely ignored by the compiler; similar are the comments on lines,, 9, 0 and. So, execution of this program produces Dialog. Dialog Hello World Hello World, again