Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Introduction to C/C++ Part 3

A newbie-friendly beginner tutorial with good and descriptive explanation of C/C++ syntax.

On Monday, March 15th 2004 at 12:15 AM
By albert tedja (View Profile)
*****   (Rated 4.8 with 11 votes)
Contextual Ads
More C++ Resources
Advertisement

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.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Ronald Windwaai on Thursday, April 29th 2004 at 03:23 PM

I am really learning something here could you send some more information on writting programms

by Ambrose on Thursday, December 2nd 2004 at 09:32 AM

You should continue the tutorial. It goes through things a lot better then the microsoft book... you should write a book on it ^^

by jikuty on Monday, July 4th 2005 at 10:17 PM

this is fantastic... i'm BRAND NEW to c/c++ ... and i'm learning a lot of answers to my questions. keep up the great work :)

by Jay on Tuesday, July 26th 2005 at 01:43 PM

You really know how to simplify things that may seem complex at first. You have a nack, you really should teach or write some type of book, I'd buy it ...

by kidd orpheus on Thursday, August 31st 2006 at 10:55 AM

As far as it goes this is a very helpful tutorial. The informality of your style makes it so much more easy to understand. More of this would be awesome.

by joselyn on Monday, December 3rd 2007 at 04:38 AM

pls. give me the other special characters and uses

by Divya on Thursday, March 25th 2010 at 05:48 AM

19. main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}
What is the meaning of line feed?


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources