Learning C, but do not understand the int thing.

p4p1 [lexostras]
9 years ago | edited 9 years ago

0

Hy so I am learning C, I am really new to this kind of programming language so I am having a tought time learning it.

So I just did this C exercise , Yes this is kind of a noob thing but we all have to start somewhere ;)
And after doing this exercise I wanted to train a bit so I searched online for all of the C escape sequences and format character’s like told in the book. While I was testing out I did this:
```

include <stdio.h>

int main()
{
num = 30(-2);
char
ward = “What is wrong with you”;

printf("Hello \n------- %5 cl%*d \n %s!", 3, num, ward);  

return 0;  

}
After a bit of research I found my error, the thing is that near this part: num = 30*(-2)```
I forgot to put int in front of it so that it would work and that got me thinking why do you need to put int in front of main then because for my noob point of view is that int = integer so yeah and the “main” part of the code is not just printing out numbers, it is printing string so my question is: dkes the int part have different meanings depending on the place in the code??

5replies
4voices
203views
dloser
9 years ago

1

The place doesn’t change the meaning of int itself, but it is used in different ways. For int num, you are declaring a variable num and saying that its type is int. For int main(), you are declaring a function main and saying that its return type is int. So, it only says something about the values that you can put after return.

You shouldn’t see what printf does as output of the function, but as (a form of) output of the program.

p4p1 [lexostras]
9 years ago

0

Ok thank you @dloser

Mart
9 years ago

2

The “int” before main goes with the “return 0” at the end of main - the function (and hence the program) returns this as its “exit value”. In this case it doesn’t really mean much, but it it usually used for testing the success or failure of something you just ran.

The format of main is predefined and has to be like this, but for functions you create yourself obviously you can decide if they return an int, a different type, or nothing at all (with the keyword “void”).

Mr. Cyph3r [MrCyph3r]
9 years ago | edited 9 years ago

1

I’ve just read some of the first sections and the question you just asked is exactly why I don’t like articles like this one…
Most of the basic concepts are taken for granted and not even mentioned, hence I wouldn’t call this an article for beginners, unless you supplement this with a lot of other reading.

Of course, you still need to keep going deeper but I think the basics are not something that can be skipped so easily.

Note: It is not a critique to the author or to his skills, and I really appreciate what he wrote in the preface.

[quote=http://c.learncodethehardway.org/book/preface.html]This is a rough in-progress dump of the book. The grammar will probably be bad, there will be sections missing, but you get to watch me write the book and see how I do things.[/quote]

p4p1 [lexostras]
9 years ago

0

Thank you @MrCyph3r , the first time I read you awnser I was confused but I get it now and you are right, but I have no idear where to ask for those type of question. And searching on google what is int in
int main()
Is not really successful and at the time of writing this post I had no clue on how to explain my problem. Sorry if those type of articles ar a pain but I do not know where and who to ask, please PM me if you have an idear on where I could send post about programming questions that I can’t find the awnser by myself :)

Discussion thread has been locked. You can no longer add new posts.
1 of 6

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss