article

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

Email
Submitted on: 1/3/2015 1:46:00 AM
By: Albert Tedja (from psc cd)  
Level: Beginner
User Rating: By 8 Users
Compatibility: C, C++ (general)
Views: 1256
 
     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 I)


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

 

Part II: return keyword, void data type, and printing text on screen.

 

Quote from the previous part:

But wait, before this one, we didn't mention the type of return value of function main(), would that be okay? Technically, no. However, it depends on the compiler you use. Some compilers put a default type if we don't specify it. For example, Borland Turbo C++ 3.0 use int as the default.

 

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. Last time I did it, I got a blue screen. I'm sure you don't want to crash your own computer, so 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}, 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.

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. But keep in mind that return can also be used to end a function before the closing brace (}).

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 printing 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 put 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 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 one by one. 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 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 other symbol: the quotation mark ("). If we do this, the compiler will find the file in the specified location, if not found, it will find it in include directories. Let's look at these examples:

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

Number 1 will find file filename.h in the current directory, if not found, include directories. Number 2 will find file filename.h in C:\, if not found, include directories. It's really simple, huh?

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.

This is the end of Part II. Next part, we will talk more about printing text on the screen and some special characters we can use in C/C++.

 

Some words used in this tutorial:

directives directions that tells the compiler what to do before compiling the code.
include directories special directories where the header files are. Some compilers may only have one directory, but other compilers can have more than one.
current directory current working directory, usually where the source code is.

 

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.