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

[Tutorial] Introduction to programming using C (III)

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 (III)

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

Back to index
<< Prev Next >>

Explaining our first C program

I am going to explain the program foo.c we used on the first part going line by line.

Code: Select all

#include <stdio.h>

int main()
{
	printf("Hello wololo.net/talk!\n");
	return 0;
}
I'll try to keep it simple, so most of the concepts seen here will be explained with more detail later on.

Code: Select all

#include <stdio.h>
As we've seen on part II, this is a pre-processor directive. It indicates that the file stdio.h should be "included" here, which literally means copy-pasting that file here. stdio.h is what's called a "header" file (.h). Header files are used in C to store prototypes, declarations, type definitions and data, but by convention they do not contain any actually executable code.

Code: Select all

int main()
This is a C function declaration. This means we're going to detail a function named main with no arguments (there's nothing between the parenthesis) that returns an integer (int). A function is a short piece of code that usually does a very specific task. main() function is a special function in C. It marks the point-of-entry, which means it's where the execution of our program will start. It's mandatory (your program MUST have a main() function)

Code: Select all

{
This marks the beginning of the function code.

Code: Select all

printf("Hello wololo.net/talk!\n");
printf() is also a function. It's declared on stdio.h (that's why we include that file). printf() prints a character string to the standard output. A character string is represented between double quotes (") in C. It accepts formatting, which are special characters used to format the presentation. For more information check the manual page for printf (man 3 printf)

A very important thing on this line is the trailing ";" character. This indicates to the C compiler the end of a C expression. This is the way the C compiler distinguishes between expressions, so do not forget it!

Code: Select all

return 0;
This marks the end of the function and instructs it to return a value of 0. The type of the value returned by the return statement must match the one declared on the function header. This effectively ends our program, returning this value to the operating system. On POSIX environments 0 means the program ended with no problem.

Code: Select all

}
This marks the end of main() function declaration.

See you on the next part

<< Prev Next >>
Advertising
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 (III)

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

Nice tutorials.
Will help noobs.
BTW why did you locked my topic ?
Advertising
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 (III)

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

I did not lock your topic. Please do not go off-topic.

EDIT: errr, yes it was me, sorry about that, it's open again. Must've been a mistake... :oops:
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 using C (III)

Post by ASKidwai » Mon May 16, 2011 3:49 am

Could you explain int main() better? I understood but only because of the book I am reading.

Could you explain it in a bit more detail?
Image
Image
Image
Image

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

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

Post by irfanhb7 » Mon May 16, 2011 7:35 am

Yeah I have the same question.
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 (III)

Post by m0skit0 » Mon May 16, 2011 11:04 am

There's nothing to explain further. Everything will be explained in more detail when we reach the C functions part.
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 using C (III)

Post by ASKidwai » Mon May 16, 2011 11:29 am

m0skit0 wrote:There's nothing to explain further. Everything will be explained in more detail when we reach the C functions part.
As you wish, though it would be better if you explained it in a bit more detail...
Image
Image
Image
Image

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

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

Post by m0skit0 » Mon May 16, 2011 11:31 am

I don't know what details you want me to explain. If you can detail what you miss, we can look into it.
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 using C (III)

Post by ASKidwai » Mon May 16, 2011 11:36 am

In other words, it should be a teensy bit more noob-friendly. If you are going to continue with more tutorials specifically for functions, then I suppose it is OK but by itself it seems a little abstract
Image
Image
Image
Image

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

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

Post by m0skit0 » Mon May 16, 2011 1:54 pm

Of course it seems abstract because it's an introduction. I'm not going to explain the whole C stuff before introducing a simple program, that would scare all the people away and it would be even more abstract. I know what I'm doing. I'm no kid and this is not the first time I'm teaching programming (far from there...), but thanks for your comments.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Post Reply

Return to “Programming and Security”