"One of the best ways to get yourself a reputation as a dangerous citizen these days is to go about repeating the very phrases which our founding fathers used in the great struggle for independence." --Charles Austin Beard
This tutorial will bring light to the common question:
"How Do I Get Started?"
I will outline the basic concepts and what you need to start programming in the C language.
To start programming in C you need a tool called a compiler. What a compiler does is convert the source code written by the programmer (you) into something the computer can read, in this case an executable file (.exe). Depending on the platform you are going to program on their are various compilers. Another thing you want is an environment to program in, most Windows compilers are placed within an IDE so you don't have to worry. With Linux however you have choices, I use VI or Gedit with syntax highlighting, works fine for me.
Windows:
Dev-C++
Borland
DJGPP
Linux
gcc
g++
I will make the code in this tutorial work on Linux and Windows so no need to worry about compatibility issues.
Your First C Program
I know it is a tradition to start with "Hello World", however I have a better starter program. Don't worry if you don't know what the code means, I will dissect it later.
( I will be using Dev-C++ and Gedit as an example for opening new files in )
Step One:
Open a new source file.
(In Dev-C++: File ---> New ---> Source File || or CTRL + N)
(Same thing in Gedit)
Code:
//FirstProgram.c
#include <stdio.h>
int main(void)
{
printf("Hello HTS");
getchar();
return(0);
}
Make sure you typed the code, and did not just copy and paste.
After typing the code save the file as "FirstProgram.c".
Next we need to compile the program I will show how to do this with Dev and then with gcc.
Dev-C++
Save: File ----> Save As || or CTRL + F12
Compile: Execute ----> Compile || or CTRL + F9
Run: Execute ----> Run || or CTRL + F10
-----
Gedit w/ GCC
Save: File ----> Save || or CTRL + S
Compile: gcc FirstProgram.c -o FirstProgram (note: In terminal in correct dir)
Run: ./ FirstProgram (note: In terminal in correct dir)
------
If your program compiled and ran, congratulations! You are an official C programmer! This make look too simple now, however rest assured all programmers started like this. Soon you will be on your way to writing low-level system drivers, and maybe some other fun system software :]
If your program did not compile and(or) run make sure everything was copied and exactly right. On linux make sure you were in the correct directory, to make sure try: the command "ls" and look for your program. Next lets tear apart this program and start learning some basic concepts of C.
------
The Dissect
We will dissect the program line by line.
//FirstProgram.c
This line is a comment, a comment is used for describing an aspect of your program, listing the license you used and(or) contact info, version info, or maybe just some quick notes to remember what a specific line does so you don't forget down the line. In this example, I used a comment to tell the the file name was. Remember, don't overdue the comments too many will do the exact opposite of what they are intended. Two types of comments are available.
The multi-line C style comment: /* Comment Goes Between These */
Or the C++ single line style comment: //This is a single line comment
#include <stdio.h>
This is an include file. This line basically tells your compiler to add the contents of it to your file when it is compiling. The include file (or more popularly named "Header File" ) is just one of many that are included with your compiler. All of these files normally have a .h extension and are stored in a different file away from your source code. stdio.h is one of the most basic of header files and is almost always needed. It stands for Standard Input Output. Many types of header files exist and they support many different functions. When you get good enough you can even make your own.
int main(void)
This is the main function, it is requirement in EVERY C program. The main functions always has to be called. Everything between the { and } make up the main body of the program.
{ and }
These are code braces, these make up code blocks. All you really need to know is that code that is between { and } executes together.
printf("Hello HTS");
printf() is the standard output function. It can display simple text messages, or even variables.In this example it outputs the text "Hello HTS". The "\n" is a newline tab this is really for aesthetics. Notice that this function ends with a semicolon ";". Every statement in in a C program ends with a semicolon.
getchar();
getchar(); is a function that waits for user input. A common problem with console applications on the windows platform is they do exactly what they are told to, except they do it too fast. If you are on windows take this line out, compile and run and see what happens. Their are other uses for this function, however I just used it here to stop the program from executing to fast.
return(0);
This is a return statement. In this program it returns a value of 0. This means the main function ran as expected with out any errors. Return values are important in other areas. The best example would be user defined function, more on that in the next tutorial.
------
Well that is the the dissection of the program line by line. This tutorial is aimed at complete beginners, remember it was made just to get your feet wet, and give you a small taste of the C language. If I would have outlined fundamentals in this tutorial it would have been way too long. Instead I will be making tutorials on every concept of the C language. I hope this tutorial helped some, and again it was meant to be basic. Expect more tutorials soon :]
/*LogicKills*/
Cast your vote on this article 10 - Highest, 1 - Lowest
Comments: Published: 28 comments.
HackThisSite is the collective work of the HackThisSite staff, licensed under a CC BY-NC license.
We ask that you inform us upon sharing or distributing.