15
orhun
5y

Do you sense something weird on this dead simple code?
The teacher showed a similar code recently in my C class. It was weird to me since I know C for years. I want to know what you guys think about it.

Comments
  • 1
    I see no argument with it.
  • 9
    return (0);

    I usually don’t use parentheses for single values like that, but whatever passes code review.
  • 6
    @bkwilliams Exactly, return is a statement. It seems like a function in here.
    I don't know who writes return like this.
  • 2
    @orhun I do - although I know it isn't necessary. I just like it that way. Except if I get into an existing codebase where it's without parentheses, then I'll keep the style for consistency.
  • 1
    Not really a big deal tbh. Depends on the codebase style, I've seen both.
  • 1
    @Fast-Nop That's reasonable. At least you're not saying "why this is so important" like my teacher.
    ugh
  • 3
    return (a >= b ? 0 : 1);
    return (a >= b);

    They look the same, but one of them is probably banned in your company.
  • 8
    @bkwilliams Aren't they logically inverted?
  • 4
    Nope nothing weird going on here.

    return(0) is xor eax,eax; ret, this actually makes more sense if assembly is your first language.
  • 0
    @Pyjong I don't get it. Can you explain your statement more?
  • 2
    Does main require arguments?

    I usually see main like this:

    int main(int argc, char* argv[])

    Are those absolutely required? I did a search but all I saw was conflicting statements.

    That is why I originally posted "no argument here".
  • 0
    @orhun R does. Fuck R
  • 4
    @orhun there is no "return value" instruction. return statement translates to load to whatever register dedicated for holding the return value and ret which loads program counter to address after function call. So technically return(0) is the simplest function.
  • 1
    @Demolishun main can be a void function - however, using empty parentheses leaves the argument question open, i.e. allows any arguments.
  • 1
    @Fast-Nop yes, that is why one is banned and you should always test before pushing to prod.
  • 4
    Add main args, kill the parenthesis on return. Printf really should include a newline, too.
  • 4
    @Root summed up everything quite nicely here.

    Side note: main takes 3 arguments, not a whole lot of people know that 🤷‍♂️
  • 2
    @olback No it doesn't. argc and argv, that's as per the standard. You have envp also - but only under Unix systems, so that's not "C". It's implementation defined and not portable.
  • 0
    @Fast-Nop yes, my bad. I rarely work with windows.
  • 2
    @olback There are a few more OS than Windows and Unix. Not quite as many as it used to be, but still.
  • 0
    When I was taught C, the hello world code used `fprintf(stdout, "Hello World\n")`. It was cool to know fprint before printf.
  • 0
    @nitnip Yup, but they still try to teach 'printf'.
Add Comment