can anyone explain to me what am i doing wrong?! i tryed int main () - shows this and when i do it like in the book just main() it still outputs an error.
Im on manjaro and vscode.
im also trying to figgure out what the error is but cant quite wrap my head around it. I figugred out, lets just do a gcc update but gcc is up to date. Do i need to configure something in the system like windows has those path to variables or something?!
Tip Your Landlord Shirt $21.68 |
DMT Has Friends For Me Shirt $21.68 |
Tip Your Landlord Shirt $21.68 |
you have no return statement at the end of the main function
well that was easy. i tried it before and it gave me somewhat same error code, but you did it anon. wtf that was ez
you declared main as an int, so it needs to return something
if you dont want it to return a value declare it as 'void'. You can still return in the function, like say for instance you don't want it to do something if it checks a value, but it won't be required.
but integer is a whole number. why do i need to declare a string of letters that is to be printed out as an integer
i get it. if you have some numbers that you want to
>want it to do something if it checks a value
you'd write somehing in the "__" and then after n you'd put the variables to check if true or false
something like this
int main()
{
a = 2;
b = 5;
if a >= b;
then
print("bla %i bla %i blan", a ,b);
return 1/0;
}
correct me if im wrong
>i% .... i% i put the placeholders wrong i think :'D
Because you declared your function as an integer, so it needs to return an integer. It has nothing to do with anything else other than that.
If you don't want your function to have to return a value, create it as void.
void main ()
{
printf("Henlon");
}
Otherwise the compiler will expect it to return a value as the one it was created as.
The print has nothing to do with your return type. Printing is just putting text in the console and nothing else. The value of the function is assigned by the return statement.
Normally, the main function in a C program has an int return value because that is then returned to the OS as whether your program ran sucessfully (value 0) or whether there was an error (some other value).
ich küss deine augen habibi
>-Wall -Wextra -Wpedantic -Wformat=2 -Wcast-align -Wconversion -Wsign-convirsion -Wnull-dereference
lmao do cniles REALLY
and all of that isnt a fraction of a fraction of rust's power
>trying to catch all errors at compile time is le bad
>explicitly declares the main function return an int
>doesn't return an int
>surprised when it doesn't work
idk man i feel like you should maybe look up some basics about programming before diving into an actual application
3/10 bait tho, only non-zero cause you got me to reply
main() isn't declared here. It's just called.
allowing main() to exit w/o status just leaves whatever was in the memory reserved for it's return as-is.
All the -W probably forces the compiler to fail. OP try to compile with gcc ./build/debug/test.c. If that fails, rename test.c to main.c. There's nothing wrong here that should actually fail to execute.
>main() isn't declared here. It's just called.
not called either, it's defined
>main() isn't declared here. It's just called.
No, it's declared AND defined. C itself calls main(), nothing in this program does. In C++, you're straight up not allowed to call main().
It looks like you're encountering a warning because your `main()` function doesn't specify a return value. In C, the `main()` function should return an integer value to indicate the program's exit status to the operating system. You can resolve this by adding `return 0;` at the end of your `main()` function, which signifies successful execution. Here's how your code should look:
```c
#include <stdio.h>
int main() {
printf("Hello World, looking if its working!");
return 0;
}
```
This change should eliminate the warning. Also, there's no need to configure system path variables for this issue in Manjaro as you would in Windows. The warning is specific to the code itself, not the system configuration.
>Also, there's no need to configure system path variables for this issue in Manjaro as you would in Windows
thats why im using linux, the probability to frick up windows configuring something wrong is pretty high for me at this stage.
lets say i want some arrays too, then i would have to declare main as int and void because there is blank spaces if i want it to stay empty and to wait for user input?!
int main(void) - to wait for user input? is that possible?
> i would have to declare main as int and void because there is blank spaces if i want it to stay empty and to wait for user input
> you're encountering a warning because your `main()` function doesn't specify a return value
im dumb i guess
No that's not at all why you'd specify the return type of main.
Here try this:
int main() { return 5; }
void main() {}
Compile those as separate programs, run them, and then check their exit code.
./example_1; echo $?
./example_2; echo $?
The return value of main is NOTHING to do with what you do inside main except what you return. You can print text, you can play a song, you can be the critically acclaimed MMORPG Final Fantasy XIV? With an expanded free trial which you can play through the entirety of A Realm Reborn and the award-winning Heavensward expansion up to level 60 for free with no restrictions on playtime, and you still either return an int, or nothing (void) from main, to tell the OS how it went. Generally 0 means it went well, and anything other than 0 means it didn't go well.
This is all very esoteric though, just use void for now, and get to the stage where you're writing functions that you're calling yourself, THEN you'll understand return values.
You need a mentor btw.
Why has nobody pointed out yet that main is the only function whose return statement can be omitted?
Didn't catch this before writing my reply. Yes, this.
Imagine being such a Black person that you missed my examples teaching the newbie in
I don't even fricking USE C, I'm a Python web dev babby, and I knew it could be omitted.
oooh but anon, void main is implementation-defined, whereas we can expect int main to behave the same anywhere. Also, an unintended return code could come from the code just before the end of a void main, whereas the return code of an int main is guaranteed to be zero if you omit the return statement at the end.
Why the frick would I care what the return code of my program is if I define void main()? I am LITERALLY telling the compiler "do what you want".
Good practice though - returning 0 very clearly tells readers "I didn't forget to return 1".
>Why the frick would I care what the return code of my program is if I define void main()? I am LITERALLY telling the compiler "do what you want".
Now that you put it that way, please accept my concession. Go ahead and make programs with undeterministic return codes to frick with shell scripts that use set -e and riced prompt lines that change color depending on the return code of the last command.
Ironic shit code is cringe while unironic shit code is based.
>shell scripts that use set -e and riced prompt lines that change color depending on the return code of the last command
You know what, you have a great point. All sensible compilers will probably return 0 from a void main, but terminal ricers and people who write ~~*scripts*~~ instead of using real programming languages DESERVE my ire. I will now explicitly return 17 from main from now on.
You asked the compiler to be pedantic, so it is being pedantic. You don't have to explicitly return 0 from main.