Geekpedia Programming Tutorials






Introduction to C/C++ Part 1

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

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

Introduction to C/C++ Tutorial Part 1



C for beginners, not C++.

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 it. 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. You will always find trouble in learning C++ unless you understand the difference. Let's take a look at our favorite/boring/hard/confusing "Hello World" program written in C++.


#include <iostream>
using namespace std;

int main()
{
   cout << "Hello World" << endl;
   return 0;
}


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 the 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 binary computation, pretty much the same like + and -, only that it performs in binary. 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 things that differ C++ from C, and those are advanced. 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 was like "Wow, that's so weird." And I looked at another symbol: >> used by cin, I got confused even more, "Do I have to do this in C++? It doesn't make any sense." It doesn't make sense because C++ uses 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 be like "What the...." You don't understand what it actually does because of the 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++ before? 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 these things called classes (and some more). For beginners, they are the same. All you learn here is still valid and still used by people. 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 program 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 useless. 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 or console. We're not talking about Windows or GUI (Graphical User Interface, such as buttons and dialogboxes) programming here because it's different and much more 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 the most important thing in C is that you must have in every C program write (that runs under DOS/console mode). Look at the code below:


main()
{

}



That is the most important thing I was referring to. However, 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 talk about them one by one.

The above code must always present in any C program written for DOS/console. The main() in the above code is called the main function. The main function is the function that defines 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.


a_something
{
   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 belongs 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 something like this:
main(argument)

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 a program in DOS or from the console, 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 the main function when we want to make our program to accept some 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 a 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 numeric value. So, the statement int main() simply means that the function main() will return a number..

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.



This is the end of Part 1.
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 Tuesday, January 25th 2005 at 04:47 AM

sda

by ted williams on Thursday, February 17th 2005 at 09:16 PM

me think this is really fun i hope you feel like me about program i'm going to take a crap now bye

by Christopher McCarter on Sunday, March 13th 2005 at 08:05 AM

I Get a great deal from this tutor, but my only complant is I can't put a hard copy on my pc or lap top. Out side of this it is an excellent tutor.

by Anthony on Sunday, December 11th 2005 at 09:33 PM

Well done. Keep it up!

by justin johnson on Saturday, September 16th 2006 at 12:35 PM

ALL I CAN SAY IS THAT I'M REALLY ENJOYING THIS TUTORIAL PROGRAM - MY CONFUSIONS ARE REALLY REDUCING.

by Kian Fatt, Ting on Friday, January 26th 2007 at 02:35 AM

Your tutorial is good, but it dosent include anything for handling arguments. Would really benefit alot of struggling C++ nebie programmers like me. Thank you !!!

by Indika on Sunday, October 28th 2007 at 12:05 AM

It is more usefull for beginners.

by DreameR on Wednesday, March 12th 2008 at 12:57 PM

It's nice and simple, simply the best tutorial. The only problem is that it doesn't have direct links like www.cprogramming.com to go on to the next tutorial. But it's a little easier to understand. I come here when there's something I can't understand on cprogramming.com

by Radha on Saturday, July 19th 2008 at 03:53 AM

This tutorial is good and beginners like me want to know brief about the difference between Turbo C and C .Please give us more summary about this topic.


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 >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons