Shop OBEX P1 Docs P2 Docs Learn Events
int meaning — Parallax Forums

int meaning

FrannyFranny Posts: 127
edited 2013-08-09 17:01 in Propeller 1
What's the difference between typing "int main()" and just "main()"?

Comments

  • Heater.Heater. Posts: 21,230
    edited 2013-08-09 00:40
    That "int" is saying that your function "main" will return a signed integer.
    That's what your operating system or C run time expects so it's the correct thing to do.
    If you don't specify any type for a function the C compiler will by default assume you mean integer so that is also correct.

    It's conidered bad practice to not specify types on functions so don't do it.

    propgcc with option "-Wall" to turn on extra warnings, which you should always use, will generate a warning for this.

    I always like to get the full signature of main correct, giving the parameters and their types like so:
    int main (int argc, char* argv[])
    {
    ....
    
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-08-09 07:24
    Heater. wrote: »
    That "int" is saying that your function "main" will return a signed integer.
    That's what your operating system or C run time expects so it's the correct thing to do.
    If you don't specify any type for a function the C compiler will by default assume you mean integer so that is also correct.

    It's conidered bad practice to not specify types on functions so don't do it.

    Don't you hate it when tools encourage bad practice?

    It always stings eventually, like lemon juice in a paper cut. I hate that.
  • Heater.Heater. Posts: 21,230
    edited 2013-08-09 07:39
    Braino,
    Don't you hate it when tools encourage bad practice?

    Ah, gotcha!

    I presume you mean the fact that C can be a bit lax about things.

    I guess you hate Forth then as checks nothing ever:)
  • doggiedocdoggiedoc Posts: 2,241
    edited 2013-08-09 07:46
    ........................
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-08-09 07:55
    No, I think prof_braino was serious. Programming in Forth is like pouring lemon juice on a paper cut. :) But once you're done the antiseptic nature of lemon juice makes it all better, just like with Forth.

    Now if he was actually referring to the Gnu C compiler, then I think he has the wrong impression. With warnings turned on the tools encourage good programming practice. However, the tools do allow for writing quick and dirty programs, where good programming practice can get in the way.
  • Heater.Heater. Posts: 21,230
    edited 2013-08-09 08:08
    Well sort of. In C I can write:
    a = b
    
    where "a" is a byte sized char and "b" is a 32 bit int without any complaint from the compiler. You may be meaning to do this but such implicit type conversions and potential loss of data are a source of bugs that many would frown on.

    There are those who say that such programming is "bad practice" as you haven't written there that you mean to do that type conversion. Your intent may be unclear to whoever reads it next. There are a billion other things a C compiler does not check. Even with warnings wound up 11.

    Having all warnings switched on is good practice, it can save you from a lot of debugging and help you deliver more reliable and portable code, but it does nothing to discourage many bad practices.

    Of course we like it that way. Not too lax but not as mind numbingly strict as ADA.

    Forth of course does not give a sh...

    Mind you I'm not really complaining about C or Forth. My current fad is JavaScript which also does not care much what you do. Without jslint to check over your work it's a minefield.

    I would always like to keep the power to point the gun at my foot if I want to:)
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-08-09 09:19
    Heater. wrote: »
    I presume you mean the fact that C can be a bit lax about things.

    I guess you hate Forth then as checks nothing ever:)

    Different, FORTH demands we always know what we are doing. We know to check this stuff.

    In this case the tool allows you to do something bad under the assumption its fine. So many hours of my life have been wasted by bugs from average (or slightly below) programmers getting caught by this. And how they argue! They always get so defensive rather than just figure it out and fix it. Where are those tasers? I still feel the need to have one in hand when discussing this topic.

    Anyway, we don't have to worry about forth, nobody uses it anyway.
  • RaymanRayman Posts: 14,662
    edited 2013-08-09 09:44
    Heater. wrote: »
    Well sort of. In C I can write:
    a = b
    
    where "a" is a byte sized char and "b" is a 32 bit int without any complaint from the compiler.

    Strange... Visual Studio C++ warns about things like this... Maybe it's just GNU that doesn't?
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-08-09 10:16
    Dave Hein wrote: »
    ... Gnu C compiler, then I think he has the wrong impression. With warnings turned on the tools encourage good programming practice. However, the tools do allow for writing quick and dirty programs, where good programming practice can get in the way.

    Yes, that's half the problem. Folks think this is ok is the other half of the problem.

    ..."good programming practice can get in the way"... I love that. Pretty much says it all.

    I don't particularly mind, aside from the "problem" parts that are the cause of all the problems we have to fix. Remove those, and the problems didn't have a change to happen.

    Don't get me wrong, experts are aware and take care to resolve this stuff. But n00bs are not aware, and leave land mines and booby traps for themselves without knowing it. Most people are not experts, most are about average.
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-08-09 11:28
    I think you misunderstood my comment about quick and dirty programs. I do this all the time when I need to write a quick program to test out something, or to generate text, etc. However, production code should always have warnings enabled, and it's also a good idea to treat warnings as errors. That's what we do where I work.

    It's up to project management to establish coding guidelines and practices. Turning on warnings in the compiler should be part of the project coding requirements. If your project manager isn't doing that you should probably get a new project manager. If your programmers don't agree with turning on warnings you should hire new programmers.
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-08-09 15:58
    Dave Hein wrote: »
    I think you misunderstood my comment about quick and dirty programs. I do this all the time when I need to write a quick program to test out something, or to generate text, etc. However, production code should always have warnings enabled, and it's also a good idea to treat warnings as errors. That's what we do where I work.

    And I'm sure you do great work. But others see this, and do the same, and forget to go back and enable the warning. I'm pretty sure I understand your comment completely. I have to deal with this constantly at the bug review meetings.
    It's up to project management to establish coding guidelines and practices. Turning on warnings in the compiler should be part of the project coding requirements. If your project manager isn't doing that you should probably get a new project manager. If your programmers don't agree with turning on warnings you should hire new programmers.

    Preaching to the choir, pal. I agree completely. Somehow, we keep getting stuck with the same project management, and the same programmers that forget to enable warning in the production code, because they turned them off "to test something" and it worked. On almost every project, at almost every company, just with different faces.
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-08-09 17:01
    And I'm sure you do great work. But others see this, and do the same, and forget to go back and enable the warning. I'm pretty sure I understand your comment completely. I have to deal with this constantly at the bug review meetings.
    You're still not getting it. I never turn off -Wall or -Werror in the product code that I'm working on. Why would I do that? The compiler options are set in makefiles that I normally wouldn't touch anyhow. If you have engineers that are checking in changes that are turning off warnings that should be addressed by their manager. If the engineering manager doesn't care about it, and his manager doesn't care, then there seems to be a serious management problem.

    The quick and dirty code that I referred to earlier are small throw-away programs that are usually less than 20 lines. I write these almost every day or so to do things like generate an ASCII table when I don't have one handy, or process a text file to extract information.
Sign In or Register to comment.