Introduction to C/C++ Part 3

Introduction to CC++ Part 3
A newbie-friendly beginner tutorial with good and descriptive explanation of C/C++ syntax.

Introduction to C/C++ Part 3

More on printing and special characters.

OK, we have learned how to print some text in C using printf() function. However, what we printed was only one line. Now, we are going to learn how to print multiple lines by calling printf() function multiple times. In order to do that, we must learn some special characters we would use.

Printing multiple lines of text by calling printf() function multiple times sounds pretty logical. Why don’t we try it? Here’s an example:

#include < stdio.h >

int main()
{
   printf( "Hello World." );
   printf( "Hi There! This is in line 2." );
   printf( "Yay! This is line 3." );

   return 0;
}

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

Hello World.Hi There! This is in line 2.Yay! This is line 3.

All in one line. 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 would print wherever 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 “Hi There! This is in line 2.” As stated before, printf() will print where the cursor is, therefore, the string “Hi There! This is in line 2.” will be printed at the position of where the cursor is — after the word “World.”– resulting in something like this:

Hello World.Hi There! This is 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 go to the next line, and carriage return means go back to the beginning of the line. If combined, it has a meaning: “go to the beginning of the next line,” the same effect as when we press the Enter key. The special character to carry out this command is a letter ‘n’ preceded by a backslash: \n. Now, we want our code to work, so we are going to add the special character we have just learned. Here’s the modified code:

#include <stdio.h>

int main()
{
   printf( "Hello World.\n" );
   printf( "Hi There! This is in line 2.\n" );
   printf( "Yay! This is 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.
Hi There! This is in line 2.
Yay! This is line 3.
_

Maybe you have not get used to this, but keep practicing as you are going to need this for the rest of your programming life (especially when dealing with string).

\n is one of the special characters that we can use in C/C++. There are many other special characters and what interesting is that they are all preceded by the backslash character. Backslashes play an important role in string constructions. Let me give you a brief description of some 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, we can use some special characters like double quotation mark (“) or even a tab in our string. Use them to see what they do.

Comments.

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 comment the other lines. The other one, however, will comment a block of code. It means that it will start commenting the code until it reaches its terminator. So it has its own 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 all three tutorials are my own opinions which I consider true. If in any case you find them wrong, corrections would be very helpful.

Copyright (C) 2001, Albert Tedja.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top