|
T223 C Tutorial 2. The required code for any of C,s
source code and how to Output text to the screen using C's built in functions.
|
||
|
|
T223 'C' Tutorial - Outputting text to the screenC Home page | ' C ' Books | Student Software | Tutorial 2. Outputting text | Tutorial 3. Variables | Tutorial 5. Controlling program flow in plain English | Tutorial 6 Branching & Looping | Tutorial 90.' C ' Error Messages | Tutorial 99. Quick reference | ASCII Codes | Download files | |
|
|
How to code your first C program. Updated 15 March
2003.
|
||
|
Contents |
|
Required CodeExercise 1.All ' C ' programs must contain the following code main()
{
}
Exercise 1. Code explanation main() functionThis code does not do anything that the user can see, but it is required in all 'C' programs.main()
Remember because most free compilers etc. are DOS based when entering file names
Type or Copy and paste this code into the 'C' Editor. If you are a T223 student that will be the PFE main()
{
}
Save as t02ex01 compile, link, and run.The following style in this course, represents what should be displayed in the resulting output window, which may be a separate window or the full screen, depending on the setup of DOS files on your computer. A window should appear that states that the Source file has been successfully compiled, along with other information It is possible that the window may close itself automatically, on modern computers this can be so fast that you may see a quick flicker or nothing at all. If this is the case you will be told how to correct this after the next exercise. | Top of Page |
Students on OU course T223Students on the open University course may like to use their Reference Manual, in conjunction with this course.Warning. Do not make any notes in this Reference Manual because you are allowed to take it into the exam. | Top of Page |
|
First ProgramExercise 2. cprintf(), formatting Most programs place text onto the screen to inform the user about something. The next exercise will show you how to do this. printf( ) writes formatted output to the stdout, standard
output device such as a screen, a printer a stream, or a string. The screen
is the default output. In this lesson, I will use cprintf(), in later lessons I will use printf(). main()
{
cprintf("Hello World.\nThis is my\nfirst ' C ' program");
}
This code should produce the following result.
Hello World.
This is my
first'C' program
Note: the layout of the lines, this is produced by the backslash formatting code \n Premature closing of Dos Window.Important Note: It is possible that the DOS window that displays the above result, may close after the program finishes. This depends on the setup of your computer. This can be cured by entering the 2 additional line as follows. Main()
{
cprintf("Hello World.\nThis is my\nfirst ' C ' program");
cprintf("\n\r\n\r\n\rPress the Space bar to Exit.");
getch();
}
Enter this code, save as t02ex02 compile, link, and run. Result of running Exercise 1 code. Code explanationcprintf()
The line cprintf("\n\r\n\r\n\rPress the Space bar to Exit."); \n\r\n\r\n\r Press the Space bar to Exit. Is the text that will be displayed. It is an informative message to the user, to inform the user what to do. This is a very important step to include when coding. Please not that the pressing of almost any key will end the program Note the position of the cursor. The getch() code is expecting a key to be pressed before
continuing. Almost any key, but not them all, will work. White space main(){cprintf("Hello World.\nThis is my\nfirst'C' program");cprintf("\n\r\n\r\n\rPress the Space bar to Exit.");getch();}
Backslash Format Control Code / Escape sequences.These are used in 'C' strings and character variables | Top of Page |
|
| Code | Meaning | Code | Meaning |
|---|---|---|---|
| \b | backspace | \f | form feed |
| \n | newline | \r | carriage return |
| \t | horizontal tab | \" | double quote |
| \' | single quote | \0 | null (string terminator) |
| \\ | backslash | \v | vertical tab |
| \a | alert | \? | a question mark |
| The characters " ' \ ? can sometimes create problems if used
by themselves, hence the use of the preceding \ backslash. If you require the combined codes \n\r then use them in the order shown
First Program - ImprovedExercise 3. clrscr() ;Read all of this exercise before starting it. In this exercise you are required to improve and change the code you used in exercise 2, as follows
Tip. If the code is not on the screen, load the file t02ex02 into the editor, and immediately save it as t02ex03, this will save a lot of typing time. You only have to add code to it then. Main()
{
cprintf("Hello World.\nThis is my\nfirst'C' program");
}
The above code is your starting point.Tip. You are asked to do 3 things in this exercise.
Enter the code, save as t02ex03 compile, link, and run. If the program is not working see if you can work it out before looking at the solution Go to solution exercise 3. | Top of Page |
|
Error ExercisesYou will experience many error messages when you attempt to compile 'C'
programs. Beginners are puzzled by these messages, because they can not
see the error in there code that caused the message. In the next few exercises
you are going to introduce very common errors, which sometimes the compiler
will tell you exactly what is wrong. In other editors read the documentation to see if line numbers can be displayed. TIP: The line number shown is where the compiler discovered there is an error. The error may be on that line, often it is on the line before, or may be several lines before. Experience helps with finding errors. As a beginner and therefore lacking experience note the next tip. TIP: Make a list of errors messages, what actually caused the error, and where that error took place in relation to the line number quoted in the error message. You will experience the same error messages time and time again. You could copy and paste the following list as your starter for your own List of Compiler Error Messages
main()
{
clrscr();
cprintf("Hello World.\r\n");
cprintf("This is my improved");
cprintf("\r\nfirst 'C' program");
}
Copy and paste this code into the editor.Delete the ; from the line that clears the screen. Save as t02ex04 compile, link, and run. You will receive an error message similar to the following Error \path\filename.c linenumber: Statement missing ; in function mainThe path is the path to where the file is stored. The filename is the name of the file that is being used. The linenumber that C displays is the line in which the compiler discovers the error. This number may not be the line number where the actual error is. It is often in a line after the actual error. Statement missing ; is a very explicit message and the error is usually on the end of the previous line. Note. Other error messages can be returned, depending where the line is with the missing ; The error is then harder to find. Replace the ; Exercise 5a. Statement missing } Now delete the final } Save, compile, link, and run. Error \path\filename.c linenumber: Statement missing } Compound statement missing }in function mainAgain the message is clear, and again the line number is after the error line. Replace the } check the program is working correctly. Exercise 5b. Declaration syntax error Now delete the } Declaration syntax error.This error is not as obvious as the others. Tip. Printout the list of error messages,
provided in Tut. 90. C Error Messages . Add
to this list additional information as you correct your errors. You can
guarantee that an error that causes many hours of frustration, will do
so again in a week, month or whatever.
Correct the error then continue with the next one. Delete a start " in one of the strings such as Hello Multiple error messages returned for a simple single error, look at the first one. Undefined symbol HelloThe compiler thinks that Hello is a symbol, you know it is a string, and you can put guess the cause. Another example see Exercise 7 below Correct the error then continue with the next one. Exercise 6a. Unterminated string or character constant. Delete an end " in one of the strings Unterminated string or character constantEasy once you know the meaning. Correct the error then continue with the next one. Exercise 7. Undefined symbol Delete the ( after line that clears the screen. Undefined symbol 'clrscr'Correct the error then continue with the next one.Exercise 8. Linker errors Misspell some function names to say clrscrn, cprint, mane etc. There has been Linker errorsCorrect the error then continue with the next one.Exercise
9.
You can obtain some very unusual results. | Top of Page |
|
Times Table ProjectYou are required to display a times table similar to those you did at school as a child.The project will be improved as you learn new skills. You do not have the skill to do this in a few lines of code. You would need to write code for every line of the table. Therefore at this stage you are only required to enter a few lines of the table. Your output screen should look like this. 7 times table 1 x 7 = 7 2 x 7 = 14 10 x 7 = 70 The table
Learn to build up your project in simple stages / steps
| Top of Page | Exercise 10.Stage 1 Note that after any of the following steps you may test the code by undertaking steps 5 and 6.
Code explanation"1 x 7= %i" If you look at the "1 x 7=" leaving out the %i, this is a string and because of the printf it will be displayed on the screen. It still requires escape sequence formatting.%i
Your result should be ... 7 times table 1 x 7=7 Stage 3
Note how a comment has been entered into this solution. Comments are discussed below in the next section. Stage 4
| Top of Page |
|
CommentsComments are entered between the opening and closing symbols /* *//* This is an example of a comment */ /* Large comments can be spread over several lines */ /*********************** * This is a comment * * embellished to * * make it stand out * *****************************/ With some compilers, but not all you can use a quicker to enter comment. // This comment does not need an end marker. // It goes as far as the end of the line Compilers that do not allow this form will create errors if used. The PFE is one of them. | Top of Page | Differences between printf( ) and cprintf( )Most courses on C use the function printf(), the Open University course T223 uses the function cprintf( ), basically the 2 functions produce the same results but there are subtle differences listed in the table below. Table: Differences between cprintf() and printf() put device.
| Top of Page | Solutions to ExercisesSolutions to Exercise 3.main()
{
clrscr();
cprintf("Hello World.\r\nThis is my improved\r\nfirst C program");
}
The following is an alternative way of achieving the same output.
main()
{
clrscr();
cprintf("Hello World.\r\n");
cprintf("This is my improved");
cprintf("\r\nfirst C program");
}
Return to exercise 3. Solutions to Exercise 10a. Main()
{
clrscr();
cprintf("7 times table") ;
}
Return to exercise 10. Solutions to Exercise 10b. Main()
{
clrscr();
cprintf("7 times table") ;
cprintf("1 x 7=%i",1*7) ;
}
Return to exercise 10. Solutions to Exercise 10c. Main()
{
clrscr();
cprintf("7 times table\r\n\r\n");
cprintf("1 x 7 = %i\r\n", 1 * 7) ; /* the spacing between , 1 * 7 is white space */
}
Return to exercise 10. Solutions to Exercise 10d. Main()
{
clrscr();
cprintf(" 7 times table\r\n\r\n");
cprintf(" 1 x 7 = %i\r\n", 1 * 7) ;
cprintf(" 2 x 7 = %i\r\n", 2 * 7) ;
cprintf("10 x 7 = %i\r\n", 10 * 7) ;
}
Return to exercise 10. |
| Top of Page |
More courses for Open University students by John McGuinn.
M206 Smalltalk Programming
Home Page | HTML and Web Design tutorial for
T171, TT
Other Sites to Visit
Leeds guide my home town City of Leeds information
Tenerife guide San Marino apartments to rent Relax in the sun Tenerife information.
Retro Clothing retailers of Men's directional fashion clothes
Snakewatches wholesalers of Rope watches
Leeds my home town
Relax in the sun.
Benidorm Tenerife San Marino apartments to rent
Copyright © John G McGuinn 2001-03