learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
VB - soon here!

Search

 

 

 

 

Next Back

 

This lesson will show you how to:

  • Write good readable programs
  • Use comments in programs
  • Use the clrscr() function
  • Use different printf escape sequence keys
  • Be able to identify compilation and runtime errors

Q. How does a blonde turn the light out after sex?
A. She shuts the car's door.

Writing good readable programs

    Perhaps we should say programming style or readability. The most common complaints we would have about beginning C programmers can be summarized as:

    • They have poor layout
    • Their programs are hard to read

Your programs will be quicker to write and easier to debug if you get into the habit of actually formatting the layout correctly as you write it.

For instance, look at the program below (Do not worry at this stage of what the program does, just noticed the indentation of the program code).

          #include <stdio.h>

int main(void)
   {
                    int sum, loop, kettle, job;
                 char Whoknows;

 

                         sum = 9;

              loop = 7;

whoKnows = ‘A’;

            printf(“Whoknows = %c, kettle = %d\n”, whoknows, kettle);

return 0;
}

It is our contention that the program is hard to read, and because of this, will be difficult to debug for errors by an inexperienced programmer. It also contains a few deliberate mistakes!

Okay then, lets rewrite the program using good form:

#include <stdio.h>

int main(void)
{

int sum, loop, kettle=0, job;
char whoknows;

sum=9;
loop=7;
whoknows=’A’;

       printf(“Whoknows = %c, kettle = %d\n”, whoknows, kettle );

       return 0;

}

We have also corrected the mistakes. The major differences are:

    • The { and } braces directly line up underneath each other. This allows us to check the indent levels and ensure the statements belong to the correct block of code. Thi becomes vital as programs become more complex.
    • Spaces are inserted for readability
    • Good indentation – Indent levels (tab stops) are clearly used to block statements, here we clearly see and identify functions, and the statements which belong to each { } program body.
    • Initialization of variables – The first example prints out the value of kettle, a variable tat has no initial value. This is corrected in the second example.

Comments

Comments are used as a note to yourself (or others) that you put into your source code. All comments are ignored by the compiler. They are used to document the meaning and purpose of your source code so you can remember later how it functions and how to use it.

The double slash // or slash star /* This is a comment */ are used in C for comment delimiters. Note that the double slash // is not actually a C comment but a C++ comment. Non the less, most compilers will execute it as most C compilers are also C++ compilers. The comment is now widely used in both languages but was designed for use in c++ not for c.

    /* This is a comment. */

A comment can occupy more than one line.
    /* This is a longer comment
        that extends over
        few lines.
    */

Example 1:

#include <stdio.h>

int main(void)
{

printf(“Good form “);
printf(“can aid in understanding “);
printf /*in the code */ (“ a program.\n”);
printf(“And bad form “);
printf(“can make a program “);
printf(“unreadable. \n”);

return 0;

} // Program end

Clear screen function clrscr()

clrscr() is a standard function which clears the output screen and positions the cursor in the top left hand corner of the screen.

Example 2

#include <stdio.h>
#include <conio.h>

int main(void)
{


clrscr();
printf(“Hello World”);
getch();

return 0;

}

printf Escape sequence

Controlling the cursor position

The following characters, placed after the \ character in a printf() statement, have the following effect.

Modifier Meaning
   
\b backspace
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\" double quote
\' single quote
\<enter> line continuation
\nnn nnn = octal character value
\Oxnn nn = hexadecimal value (not all compilers)

 

 

Example 3:

#include <stdio.h>

int main(void)
{


printf(“Good form \n”)
printf(“can aid in understanding “);
printf(“And bad form \r ”);
printf(“can make a program “);
printf(“unreadable. \n”);

return 0;

}

Identifying compilation and runtime errors

Compiler errors:

The example below has a semicolon missing after the printf function, as displayed in the example below. Type the program in Example 4 and notice the compiler error message:

#include <stdio.h>
#include <conio.h>

int main(void)
{


clrscr();
printf(“Hello World”);
getch();

return 0;

}

Runtime erroes:

    Runtime errors occur when errors are found only at runtime and not at compilation of the source code. An example of a runtime error is a division by 0. Another good example is when a statement of the program is pointing to a file that has been deleted or moved after the compilation of the source code.

Example 5:

#include <stdio.h>

int main(void)
{

int Num, Answer;

Num = 5;
Answer = Num / 0;

return 0;

}

back to top          exercises       lesson 5        lesson 7

 


 






Hosted by www.Geocities.ws

1