C++ Program Writing Style (CPWS)

C language provides flexibility in writing program forms. Program 2-3, for example, can be rewritten without changing the program execution results to: 

#include <stdio.h>

main()  {
     printf("Selamat datang\n");
     printf("di program pra-pasca\n");
}

Or

Program 2-7

#include <stdio.h>

main()
    {
    printf("Selamat datang\n");
    printf("di program pra-pasca\n");
}

Even writing like the following, by omitting a number of spaces is still permitted:

Program 2-8

#include <stdio.h>

main()
    {
     printf("Selamat datang\n");
     printf("di program pra-pasca\n");
}

Programmers just have to choose the form they like. As a guideline, writing or choosing a form should provide convenience for those who want to read the program, especially for large programs. If we look at programs 2-8, very compressed writing can make the program difficult to read/understand.

Comments in C Program

For the purposes of documentation and program maintenance with the intention of making the program easy to understand at a later time, usually when writing a program, a comment or description of the program is included. This comment or description can be placed at the beginning of a program or function, or even at the end of a line of instructions, if necessary. In C, a comment is written by starting with a sign  /* and ending with a sign  */.

An example comment is as follows: 

/* Ini hanya sebagai komentar*/

Examples containing more than one line of information:

/* ---------------------------------------*
*   Program Invers Matriks            *
*   Dibuat oleh : Rudy Hartanto       *
*   Tanggal     : 2   Juni 2002       *
*   Direvisi    : 12 Juni 2002        *
*----------------------------------------*/

Questionnaire

1. Create a program to display the following text: 

Selamat datang

Di program

"Magister Teknik Elektro"

2. What is the result of the following program:

#include <stdio.h>
main() {
    printf("%d kelas\nper kelas %d", 5, 10);
}

3. Indicate the following program errors and correct them:

#include <stdio.h>

main() {
        printf("%d kelas\nper kelas %f", 5, 10)
        printf("%f Jumlah siswa total\n")
}

Getting to know the printf() function

The printf() function has a wide range of uses in C, used to display strings or various other types of data. By using this function, the display can be arranged (formatted) easily. In program 2 above, the main() function only contains one statement, namely:

printf ("Selamat datang di program pra-pasca");

The above statement can be written as two statements as follows, which will produce the same output.

Now, what if you want the output to be:

Selamat datang
Di program pra-pasca

The solution can be obtained by including a character called a newline character, which is  \n at the end of the first string ("Welcome"). As shown in the following program example 2-3.

#include <stdio.h>

 main()
 {
  printf("Selamat datang\n");   printf("di program pra-pasca");
 }

an example of the execution result is:

C>Prog2-3
Selamat datang
Di program pra-pasca

The \ sign in the string passed as an argument to printf( ) has a special meaning, namely it is used to indicate special characters such as newline characters or backslash characters (left slash). So the \n character actually indicates a character. Other examples of characters written with the \ sign prefix are:

\"  declare double quote character 

\\ declare backslah character 

\t   declare tab character

To obtain the output in the form of Welcome to the  "pre-post" program  The required program writing is:

Printf("Selamat datang di program \"pra-pasca\"");

The following program illustrates the use of  \t (tab) to set the output image format to be indented to the right.

#include <stdio.h>

main()
{
    printf("\t*******\n");
    printf("\t*          *\n");
    printf("\t*******\n");
}

Example execution:

C>Prog2-4
    *******
    *         *
    *******

The general form of the format  printf( )is as follows:  printf("string kontrol", daftar argumen). With the control string can be one or a number of characters to be displayed or in the form of a format specifier that will regulate the appearance of the arguments located in the argument list. The list of format specifiers in C includes those shown in table 2.1 below.

Table 2.1 Control string formats

| Format                       | Fungsi untuk menampilkan                                                                                                                                          |
|------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| %d %ld %u %x %f %lf %e %c %s | bilangan bulat (integer) long integer unsigned integer hexadesimal integer float (bilangan pecahan) double float float tipe exponen menggunakan e karakter string |

An example of its use is shown in the following program 2-5.

#include <stdio.h>

main( )
    {
    printf("Nama siswa  : %s\n, "Amir");
    printf("No. Mhs.      : %d\n", 12547);
    printf("Nilai : %f  Predikat : %c\n", 75.6, 'B');
    }

Example of execution results:

C>Prog2-5
Nama Siswa : Amir
No. Mhs.    : 12547
Nilai : 75.600000   Predikat: B

Figure 2.2 Use of Format Specifiers in printf ()
Figure 2.2 Use of Format Specifiers in printf()


Post a Comment

Previous Next

نموذج الاتصال