Page 1 of 7

[Tutorial] Introduction to programming using C

Posted: Sun May 15, 2011 12:03 pm
by m0skit0
<< Prev Next >>

Index
Introduction and scope

This humble tutorial is dedicated to all those that want to start learning programming. I'll be using the C programming language to introduce programming concepts, show some examples and proposing some exercises so you can practice the concepts involved.

I'll be focusing on standard C, and for this I'll suppose you're on a POSIX machine (e.g. Linux). The code should work as well on Windows, but I'll not address any problem specific for this platform, nor provide tools or compilers. Search for yourself, read the documentation or check with Microsoft support if you're having problems with the tools/code presented here.

Installing the required software

We basically need 2 things: the C compiler suite and a text editor.

The C compiler suite comes in the build-essential package for Linux distros. You can search for the package name on your distro's package manager and install it with full dependencies.

On Debian-based systems you can simply execute the following command as root on a terminal:

Code: Select all

apt-get install build-essential
To check if the installation was successful:

Code: Select all

m0skit0@soviet:~$ cc
cc: no input files
Fine.

The text editor can be anyone, although I strongly suggest using a text editor that supports programming features (e.g. syntax coloring, automatic formatting, etc...). For this tutorial I'll be using gedit, the default GNOME text editor.

You can alternatively use an IDE, but setups and configurations differ much, and I'm not going to cover this here. I personally use Eclipse.

Checking the environment

Ok time to write and compile our first program to check if everything installed successfully.

First, we're going to copy the next code into our text editor and save it as foo.c:

Code: Select all

#include <stdio.h>

int main()
{
	printf("Hello wololo.net/talk!\n");
	return 0;
}
Now we compile it with:

Code: Select all

m0skit0@soviet:~/Temp$ cc -o foo foo.c
As usual on POSIX no error message means everything went all right.

Now time to execute it:

Code: Select all

m0skit0@soviet:~/Temp$ ./foo
Hello wololo.net/talk!
Who said C was hard? ;)

See you on the next part.

<< Prev Next >>

Re: [Tutorial] Introduction to programming with C (I)

Posted: Sun May 15, 2011 12:22 pm
by Halvhjearne
dang m0skit0 :o
... this looks really good, i will defenatly follow the upcomming tuts from you, as i know allready that this is no bs guide (like some of the programming guides ive seen here) :lol: ... now i actually got a reason to begin reading "the programming language C" and setup a linux pc to try out the stuff you will teach ... maby a little off topic here, but can i use an older pc for this linux stuff, or does it have to be some high tech w/e for it to work propper, i mean are there any minimum requirement i have to meet with this pc?

Re: [Tutorial] Introduction to programming with C (I)

Posted: Sun May 15, 2011 12:29 pm
by m0skit0
Thanks for the support :)

No high-tech needed, even an ol' good 8086 will do :lol:

Re: [Tutorial] Introduction to programming with C (I)

Posted: Sun May 15, 2011 12:37 pm
by Halvhjearne
m0skit0 wrote:Thanks for the support :)

No high-tech needed, even an ol' good 8086 will do :lol:
lol, not going that low tho, but good to know ... :lol:

im am a real linux noob tho (never really used it before), so i might bring up some really noobish questions regarding this sometimes tho, hope you can live with that ... as you have propperbly noticed (or not) tho, that i didnt write many "help me" threads corse i useually like to learn/figure out stuff on my own, alltho the C language might be too big of a mouthfull without any kind of mentor or w/e ..

Re: [Tutorial] Introduction to programming with C (I)

Posted: Sun May 15, 2011 1:06 pm
by m0skit0
Linux is quite easy to use, don't worry. I'll try to answer any question that arises about that anyway. Just let's not make this thread a Linux one ;)

Re: [Tutorial] Introduction to programming with C (I)

Posted: Sun May 15, 2011 1:19 pm
by ASKidwai
Nice guide(s).
I use

Code: Select all

gcc whatever.c -o whatever
So is that different from the

Code: Select all

cc 

Re: [Tutorial] Introduction to programming with C (I)

Posted: Sun May 15, 2011 1:32 pm
by m0skit0
It's almost the same. cc is the default C compiler, while you're specifying gcc, which will only work on a system with GCC.

Re: [Tutorial] Introduction to programming using C (I)

Posted: Sun May 15, 2011 6:01 pm
by irfanhb7
Oh ! You posted a tutorial.
I have one question and pls don't get angry.
Can we use void main() instead of int main and getch() instead of return 0 ?

Re: [Tutorial] Introduction to programming using C (I)

Posted: Sun May 15, 2011 7:08 pm
by m0skit0
irfanhb7 wrote:Can we use void main() instead of int main and getch() instead of return 0 ?
No to both of them.

Re: [Tutorial] Introduction to programming using C (I)

Posted: Sun May 15, 2011 9:53 pm
by frank
irfanhb7 i bet ur on windows and that the cmd prompt just blinks right
well u can use getch to have it stop till u press a key
but that has nothing to do with return, u still need return