C/C++ 101: Introduction to C/C++ (Part III)

Submitted on: 1/3/2015 2:17:00 AM
By: Albert Tedja (from psc cd)  
Level: Beginner
User Rating: By 12 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 3377
     This tutorial is made to help people learning C/C++ language which is commonly believed as a hard language.


C/C++ 101: Introduction to C/C++ (Part III)

C/C++ 101: Introduction to C/C++


Part III: more about printing, special characters, comments.


Quote from the previous part:

There's one thing I need to say regarding this include stuff. #include is unique to C/C++ and sometimes it causes great confusion for non-C programmers. C language doesn't have built-in functions like other languages. It only has some keywords and symbols. So, every time we want to use a function, we need to get it from other sources. That sources are called libraries. In order to access these libraries, we need header files. The header files are just regular files that contain C/C++ language, just like our code. However, they are made to access the libraries which are written using Assembly language. Different compilers have different header files. stdio.h in compiler A may be different with stdio.h in compiler B, so, don't mix them up.


OK, we have learned how to print in C using printf() function. However, what we printed was only one line. In this tutorial, we are going to learn how to print multiple lines by calling printf() function multiple times. Therefore, we will also learn some special characters we can use. I'll also discuss about comments in this tutorial.

Printing multiple lines of text by calling printf() function multiple times seems very promising and the most logical way. Why don't we try it? Here's an example:

#include <stdio.h>

int main() {
    printf("Hello World");
    printf("Hello World line 2");
    printf("Hello World line 3");

    return 0;

But, when we compile it, what do we get?

Hello WorldHello World line 2Hello World line 3

It seems that our code doesn't work so well. Here's why. We all know what the word "cursor" means. It is a pointer to location where we are going to type words. printf() function is "cursor dependent." It means that it prints where the cursor is. Before we print the first string ("Hello World"), the cursor position is at the top-left corner of the screen. Therefore, the "Hello World" string will be printed at the top-left corner. Once the "Hello World" is printed, the cursor points after the word "World." Here's an illustration:

Hello World_

The underscore ("_") is the cursor.

After "Hello World" is printed, we call another printf() function. This time the string is "Hello World line 2." As stated before, printf() will print where the cursor is, therefore, the string "Hello World line 2" will be printed at the position of the cursor--after the word "World"-- resulting a result like this:

Hello WorldHello World line 2_

And the same thing happens with the third call of the printf() function, and we get a continuous string in one line instead of three lines. How do we fix this? C has provided a special character to perform an action that we call "line feed and carriage return." Line feed means change to the next line, and carriage return means back to the beginning of the line. If combined, it has a meaning: "go to the next line at the beginning," the same like when we press Enter key. The special character is a letter 'n' preceded by a backslash: \n. We want our code to work properly, so we are going to add the special character we have just learned to our code. Here's the modified code:

#include <stdio.h>

int main() {
    printf("Hello World\n");
    printf("Hello World line 2\n");
    printf("Hello World line 3\n");

    return 0;

We put \n at the end of each string because we want our compiler to know that our string ends there and move to the next line to print another string. \n must be put inside a string and should not be excluded from the string. Here's what we get when we compile the modified code above:

Hello World
Hello World line 2
Hello World line 3

Maybe you're not used with this, but keep practicing as you are going to need this for the rest of your programming life (especially in DOS).

\n that we learned is one of the special characters that we can use in C/C++. There are many special characters and what interesting is that they are all preceded by a backslash character. Backslashes play important role in printing some output on the screen because they like "identifier" of the special action we are going to take. Let me give you brief descriptions of these special characters.

\n line-feed and carriage-return.
\b backspace
\t tab
\" double quotation mark
\\ backslash
\0 null terminator

There are more than these but they are not used so often. As you can see from the above table, we can use some special characters like double quotation mark (") or a tab in our string. Use them to see what they do.



I guess you already know what comments are, so I'll be straight forward for this one.

There are two ways of commenting in C/C++. One is used for commenting a line, and the other one is used for commenting a block of code. The one we use for commenting a line is a double slash (//). Here's an example of how to do that.

#include <stdio.h>

int main() {
    // This is a comment
    printf("Hello World");
    printf("Hello World line 2");
    printf("Hello World line 3");

    return 0;

A double slash comments only one line and it won't interfere with the other lines. The other one, however, will comment a block of code. It means that it will comment the rest of the code until it reaches its terminator. Somewhat similar like BEGIN and END. We use /* to BEGIN commenting, and */ to END commenting.

#include <stdio.h>

int main() {
    This whole block is commented
    printf("Hello World");
    printf("Hello World line 2");

    printf("Hello World line 3");

    return 0;


Note: all words, materials, and definitions defined in this tutorial are my own opinions which I consider true. If in any case you find them wrong, corrections would be very helpful. This tutorial is made to help people learning C/C++ language which is commonly believed as a hard language.

Copyright (C) 2001, Albert Tedja.

Other 3 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.