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

Submitted on: 1/3/2015 1:19:00 AM
By: Albert Tedja (from psc cd)  
Level: Beginner
User Rating: By 17 Users
Compatibility: C, C++ (general)
Views: 3185
     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 I: C for beginners, not C++, and the main function.

This tutorial (or article) is designed for those who want to learn C++ but find a great difficulty in learning it. I also had the same experience when I tried to learn C++, but then, I discovered something that causes the learning gets very difficult. I'll show you that C/C++ is an easy language.

I'll assume that you are beginners to C/C++ because you're reading this. I'll ask you something, what is the difference between C and C++? You probably don't know or try to guess. At first, I thought the difference was that C was old, C++ was new. If you think the same way like I did, you're wrong. That's not the difference, there's something else--more important. That what makes the language C++ gets so hard. Unless you understand the difference, you will always find trouble in learning C++. Let's take a look at our favorite/boring/hard/confusing "Hello World" program written in C++, not C.

#include <iostream.h>

    cout << "Hello World" << endl;

For those who haven't seen this code before, welcome to the wonder of confusion. I'm sure you get really confused looking that code. That code will print a text "Hello World" on the screen. For those you have seen the code and still confused with C++, you will recall that confusion. Let's ask, what does symbol << actually mean? If we consult our trusty Help manual, it will give you something like this: "bitwise shift operation." Hmm...and what is that supposed to mean? Well, honesty, it's something related to computation, the same like + and -, only it relates to binary operation. Maybe you're asking, do we really need those stuff if we want to print just a "Hello World" on screen? No. The symbol << in the above code is overloaded by the object cout. The object cout uses symbol << to print the "Hello World." Wait. Overload? Object? Yes, overload, object. Don't get it? Of course, those terms are used when you learn about classes which is unique to C++. Classes are the actual thing that differ C++ from C, and those are advanced stuff. I never understand why they use such thing to teach beginners. cout is definitely not for beginners, especially that symbol << that initializes the confusion. At the first time I looked at that symbol, I said, "Wow, that's so weird." And I looked at another symbol: >> used by cin, I commented even more, "Do I have to do this in C++? It doesn't make sense." It doesn't make sense because people use it like this:

cin >> a_variable;

when they want to get input from user and put it into a_variable. They don't use the popular equal sign operator like other languages. If you know Pascal or BASIC, you might say, "What the...." and your face starts to get squashed. You don't understand what it actually does because of the weird symbol << and >>, then you give up learning, and say, "C++ is hard." I'm telling you, there's nothing special with C/C++. They're languages, just like Pascal and BASIC, and are learnable. Books and tutorials out there do not give you an easy way to learn it. They give you the hard way by using classes and overloaded operators without giving any details about them. They did that because they think that it's the most basic thing they can teach you, while in fact, it's not basic.

So, now do you understand why you couldn't learn C++ in the past? I hope you do. Let's forget about all those things and start this tutorial. This tutorial will teach you about C (not C++) language. Don't worry, you are not going to learn something obsolete. As I mentioned before, the biggest difference about C and C++ are an advanced stuff calles class. For beginners, they are the same. All you learn here is still valid and still used by people. In fact, it will become your road in learning more advanced stuff about C and C++. Once you grasp the knowledge of C, you can start learning about classes (C++) and then you can say, "Hey, C++ is easy."



The C Structure

Before we start, I highly recommend that you have a working C/C++ compiler on your computer. It may be C or C++ compiler, doesn't matter, as long as it works. Without one, all learning process will be vague. Learn how to compile using your compiler so you know that your code works. One more thing, all we are going to program here run under DOS. We're not talking about Windows programming here because it's different case and much difficult. So, if everything is ready and you feel ready too, let's get started.

What is the simplest C program? The "Hello World?" Could be, but I'll tell you what are the most important things in C, that you must have in every C program (that runs under DOS). Look at the code below:




If you try to compile it, it will compile but does nothing. Why? Because we haven't put anything in the code. What about that main() and those two braces? What are they doing there? Let's discuss them one by one.

The above code must always present in any C program written for DOS. The main() in the above code is called the main function. The main function is the function that is considered the body of your program. Without one, your code won't compile. You will type your actual program inside this function. But, how do we know that we are typing inside the main function? The two braces do the job. They simply mean BEGIN and END. The open brace ( { ) means BEGIN, and the closing brace ( } ) means END. This illustration shows how they actually works.

    whatever between these braces..
    belong to a_something

So, if we apply the above illustration to our code above, we know that whatever inside the braces belong to main(). But in our code, we see nothing between the braces, that's why the code does nothing.

Now, let's discuss more about main(). Why are those parentheses sitting there? Can't we just use main instead of main(). As said before, main() is a function, and a function can take arguments or parameters. The parentheses are used to define those arguments even if we don't have any arguments. If we define an argument, the main() will look like this:


Since we don't have any arguments, we define nothing between the parentheses. But what about if we do? What happens? If you ever tried to run an old program in DOS, sometimes you are asked to specify some special commands like this:

program.exe /mode /open:sample.txt /scan

/mode, /open:sample.txt, and /scan are the arguments of the main function of program.exe. We define arguments for main function when we want to make our program to accept some special commands like the above example. These arguments are called command-line arguments. For now, we don't talk about it, so just leave it blank.

Now, let's complicate the code a little bit by adding one more word.

int main()


We've just added one word called int. We put it before the function main() because it has special meaning to main(). The word int represent the type of the return value of the function main(). Type of return value? Correct. A function can generate a return value to tell many different things. It can say whether the function has succeed or not. It can say other things too, depends on the programmers. So, what about the type. The type defines what kind of return value the function pass. Is it a number? How big the number? Does the number has decimals? Or maybe it's a string. It can be anything, and again, depends on the programmers. What we have here is int. int means integer, a 16-bit numeric value that vary from -32768 to 32767. So, the statement int main() simply means that the function main() will return a value vary from -32768 to 32767.

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.


OK, if I continue this tutorial, it will be very long. I think I will stop here and continue it in the next section. The next section will talk about the data types, how to return a value, and how to print a text on screen in a more understandable C way.


Some words used in this tutorial:

to overload to use operators in different ways.
classes a group of variables and functions, concept of object-oriented programming.
objects variables defined of type class.
functions a set of code used to perform special tasks using available arguments.
arguments variables passed to a function.


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.