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 2

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

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

Introduction to C/C++ Part 2



return keyword.

Okay, in the previous section, we have discussed about the int data type and how it affects the main return value. However, how do we return the value? To do this, we use the keyword return, followed by the number we wish to return. So, if we modify our previous code to return a value, we will get something like this:


int main()
{
   return 0;
}


The above code will return a number 0, and pass it to the operating system. Why 0, not 1 or 999? In this case, 0 means "success." It means that our program exits in the proper way. There are no such errors that terminate our program. What about if we replace the 0 with 1 or other numbers? It still compiles, but, you have to be careful. You really are passing that number to the operating system. If you pass a non-zero number, it means "error," and you don't know what operating system is going to do with that number. It's a good practice to always put 0 even though you must exit your program because of an error.

If we look more carefully in the code above, we will see a new symbol: a semi-colon (;). What is it doing there? In C/C++, it means "end of statement." So, we tell the compiler that we want to end our statement (return 0) there. What about if we didn't include it? The compiler would interpret the code above like this: return 0} (yes, with the curly brace), and gives you error messages because it doesn't recognize it. So, every time you want to end a statement, always use a semi-colon.



void data type.

There is another data type that is commonly associated with main(). It is called void. As the name suggests, it means empty or nothing; therefore, main() returns nothing. In this case, we don't specify any number after the keyword return, leaving the code like this:


void main()
{
   return ;
}


You may also want to omit the keyword return, removing it completely from the code. It still does the same thing. However, keep in mind that return can also be used to end a function even before it reaches the closing brace (}).


Printing text.

So, I think you have been bored with all of the details, now let's code the real stuff. The simplest program in C that gives you some output is a program that prints out a text on the screen. How do we do that? In C, this is done by using a function called printf(). What printf() does is printing a text on the screen. Suppose we want to print a text "Hello World" on the screen using printf(). How do we do that? Look at the following code:


int main()
{
   printf( "Hello World" );

   return 0;
}


We pass the string "Hello World" as an argument of printf(). Then, printf() uses our "Hello World" and put it on the screen, and don't forget the semi-colon. However, when you try to compile the above code, you will get an error message telling you that the compiler doesn't recognize the printf() function, or it's undeclared (error messages may vary, depend on the compilers). What's wrong with this? Nothing wrong. At this point, it's true that the compiler doesn't recognize our printf() function because we haven't defined/declared it yet. So, we need to define it first before using it. The following code will show you how:


#include < stdio.h >

int main()
{
   printf( "Hello World" );

   return 0;
}


Now it should compiles. However, you may get confused with the new line we put in our code, but let's discuss it. What is #include? It is a directive used to include the file specified, in this case: stdio.h, to our code. What about the pound sign? Is that really necessary? Yes. That pound sign shows actions that the compiler must take before compiling our code. It has nothing to do with the code. In the above code, the action that the compiler must take is to include file stdio.h to our code. But what the heck is stdio.h? stdio.h is called a header file and it's where printf() function is defined; that's why we include it. We want the compiler to know what printf() is before we use it. Now, what about those less-than and greater-than symbols enclosing the stdio.h? They tell the compiler to find the file stdio.h in the include directories. Include directories? Yes, they are special directories containing header files that we can include in our program. What about if we want to include a file outside the include directories? There are two ways of doing this. First, we copy that file to the include directories and include it to our code as usual. Second, we replace the less-than and greater-than symbols with the other symbol: the quotation mark ("). If we do this, the compiler will try to find that particular file in the directory where the source file, that #include-ing this header file, is. If not found, it will try to find it in include directories. Let's look at these examples:


1: #include "filename.h"
2: #include "C:\filename.h"

Number 1 will include file filename.h from where the source file is, if not found, include directories. Number 2 will include file filename.h from C:\, if not found, include directories. It's really that simple.

There's one thing I need to say regarding this include stuff. C/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. These sources are called libraries. In order to access these libraries, we need header files. The header files are just regular files that contain the C/C++ language, just like our code. However, they are made to access the libraries. Different compilers have different header files (but they still function the same). stdio.h in compiler A may be different with stdio.h in compiler B, so, don't mix them up.

In C++, header files are also used to define classes. That's pretty much the reason why we do #include because the cout object is defined there.



This is the end of Part 2
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 sadegh on Saturday, February 26th 2005 at 08:09 AM

tmtmt

by Alexandre on Thursday, September 15th 2005 at 10:54 PM

Muito bom seus artigos! Estou acompanhando com atenção. Está usando uma linguagem clara e expansiva.
Obrigado!


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