C/C++ Basic Structures of a Simple Program

Why Learn Programming?
Programming is an important part of using the computer to do work for you. Even if you're not a programmer you can still learn a language and it will help you in every day life, not just in the computer, and it can also help you automate things that you consistently do and it will make your life easier, and perhaps if you share your code, it will help others as well. This tutorial will teach you the basics of programming in C and C++, a language that is shrouded in mystery and learning it is very beneficial!

What is a program?
A program is basically a list of commands that the machine will understand. All computers work with 1s and 0s, and combinations of thousands create millions of possibilities. However, to make things easier a machine language was invented which made it easier for programmers to create programs, and then a higher level language such as the new ASM languages were created. Then, even higher level languages were created such as C and C++. Although now even higher level languages like C#, Java, and Delphi exist, C++ still has the greatest advantages as it's not too low, but yet its not too high either, so it can do ANYTHING.

Basics of C++ Programming
Programs are first sent through the process of compiling by a compiler, allowing all the syntax to be translated to code, then it is linked with libraries and other cpp/h files to create the program itself using a linker. They are turned into an EXE file in windows. EXE files are actually encrypted Assembly code. The Assembly code is fed to your processor which actually allows you to execute all your commands. This is why exe files cannot be decompiled, they can only be disassembled, (turned into Assembly code). Many programmers use Microsoft Visual Studio 2003/2005 .NET, some use DevC++, CodeBlocks, or C++ Builder for their C++ purposes, but these are IDE and Compilers (So they are programs where you type in the code and you compile & link to create EXE files).

The process of compiling is very similar in all program, but let's talk about DevC++ an IDE that can also compile and link using the mingw compiler. You can download DevC++ for free from DevC++ 4.9.9.2 Binary Release. Now, after you install DevC++, try and start an empty project. Click your project in the tree view on the left, and then press Ctrl+N (or just create a new file). Now save this as main.cpp, and then add this code to your file:

// First Program
#include <stdio.h>
#include <stdlib.h>
 
int main(){
 printf("My First Program!\n");
 system("PAUSE");
 return 0;
}

This is your first program.

We used // to indicate a comment, a comment is NEVER used in your code, it is just there for human programmers to read. The compiler ignores the line with //. Other comments include encasing text with /* some comment */.

We included the STD IO library (stdio.h) to use our printf function. printf is used to print out information onto your console window.
The STD IO library, is not a C++ library, it is a C library. C and C++ are not the same but C can be used in C++ and as you advance you will understand the hundreds of differences between them, such as Object Oriented Programming capabilities in C++, which help organize your code better.

int main(), is how we declare our main function, which is a special function for console programs. it starts with int, because it will return an integer type. You could have written void main(), and then instead of return 0; you would just return;.

Printf is used to print out information to the console program, it will show up in the black screen. In contrast you can use scanf, in the sense of something like (This code must be inside your main() function):

printf("Enter a name: \n");
char myCharString[50];
scanf("%s", myCharString);
printf("You entered %s", myCharString);

These 4 lines would be a very simple approach to input and output between the user and your program. A char is a type in memory that allows you to store keyboard characters. The maximum you can enter in this example is 50 letters. Entering any more than that will crash your program, and therefore you should take precautions for this, which we will discuss in the future.

Scanf is used to take in the input and store it in myCharString or any other series of chars. The %s symbol means a series of chars or another string type that has multiple characters, which you can use in both printf and scanf.

We used "\n" to indicate a new line in the console, so that the PAUSE will appear below it rather than on the same line. If we used "Hey\n how are you?", it would put "Hey" and "how are you", in different lines.

system("PAUSE"); is a shell command for DOS. It displays a pause statement and waits for you to press enter. If you remove this piece of code your program will end so fast you will not even see the message. The Pause allows you to see your result and then press enter to exit your console program.

return 0; is used in main() function to return to end the program.

This was the basic structure of a C/C++ program. We hope you enjoyed this tutorial and hope it explains to you how to learn programming and perhaps will allow you to actually practice this field without all the confusion surrounding programming. If you continue to have any questions, or if you want to help improve this tutorial please come to our forums, register there, and feel free to post a topic somewhere.

bernard's picture

This website is perfect for

This website is perfect for learning C/C++ programming!I've been reading information on c/c++ for almost 3 months and this is the most easy and broken and info I've seen so far by any website. I been trying to figure out how to allow a user to input their name in one of my programs and by reading just a few line in this page. I was able to do just that and mind you it took less than 5 minutes. Compare 5 minutes to 3 months! 10*'s

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.