Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

[Tutorial] Introduction to programming using C

Post by m0skit0 » Sun May 15, 2011 12:03 pm

<< 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 >>
Advertising
Last edited by m0skit0 on Mon Jul 09, 2012 11:44 am, edited 7 times in total.
Reason: stickying :)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Halvhjearne
Posts: 664
Joined: Mon Mar 14, 2011 1:58 am
Location: Denmark

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

Post by Halvhjearne » Sun May 15, 2011 12:22 pm

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?
Advertising
Dr. Evil wrote:I used to use Windows, but it was designed by freakin' idiots.
Now i use linux allowing me to conrol the "lasers" on my "death star" with ease.
I'm Dr. Evil, and I'm aspiring to take over the world.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

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

Post by m0skit0 » Sun May 15, 2011 12:29 pm

Thanks for the support :)

No high-tech needed, even an ol' good 8086 will do :lol:
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Halvhjearne
Posts: 664
Joined: Mon Mar 14, 2011 1:58 am
Location: Denmark

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

Post by Halvhjearne » Sun May 15, 2011 12:37 pm

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 ..
Dr. Evil wrote:I used to use Windows, but it was designed by freakin' idiots.
Now i use linux allowing me to conrol the "lasers" on my "death star" with ease.
I'm Dr. Evil, and I'm aspiring to take over the world.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

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

Post by m0skit0 » Sun May 15, 2011 1:06 pm

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 ;)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

ASKidwai
Posts: 937
Joined: Mon Jan 10, 2011 7:42 am
Location: 'Ere and There
Contact:

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

Post by ASKidwai » Sun May 15, 2011 1:19 pm

Nice guide(s).
I use

Code: Select all

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

Code: Select all

cc 
Image
Image
Image
Image

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

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

Post by m0skit0 » Sun May 15, 2011 1:32 pm

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.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

irfanhb7
Posts: 466
Joined: Wed Jan 26, 2011 2:46 pm
Location: In your nightmares

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

Post by irfanhb7 » Sun May 15, 2011 6:01 pm

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 ?
Languages I know : C , C++ & Java
Image
Boy : There is something wrong with my phone.
Girl : What ?
Boy: It don't have your Phone Number.
Image

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

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

Post by m0skit0 » Sun May 15, 2011 7:08 pm

irfanhb7 wrote:Can we use void main() instead of int main and getch() instead of return 0 ?
No to both of them.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

frank
Posts: 211
Joined: Wed Mar 23, 2011 5:15 am
Location: northwest usa
Contact:

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

Post by frank » Sun May 15, 2011 9:53 pm

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
Sig shrink!
PSP 2000 6.60 ME-1.8
G1 - Super D 1.11
Samsung Galaxy Tab 2 GT-P5113 - CyanogenMod 10
PC - Windows 7 - P4 3.20ghz HT - 3 gb 756 mb RAM - ATI Radeon x600
Cardboard box server - Ubuntu Server :D - AMD Athlon XP 3000+ - 512mb RAM

Post Reply

Return to “Programming and Security”