RT programming without using "if else"

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1488 Views - Last Post: 30 November 2012 - 05:54 AM Rate Topic: -----

#16 crippler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 9
  • Joined: 29-November 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:56 AM

View PostXupicor, on 29 November 2012 - 09:52 AM, said:

Never ever use gets(). It's unsafe. It's asking for trouble. It's deprecated in C11, and it's definitely not going to look good on your job interrogation interview.

Use code tags (see the red text on your editor?).
And by the way, you're not done yet. Where's the function?


To implement it as a function, is not the trouble

it's not a problem to write

void foo(char *str)
{
...
}

My problem was just to implement the concept

Thank you, my friend
Was This Post Helpful? 0
  • +
  • -

#17 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:56 AM

I'm just being pedantic, but the main() function is supposed to return an integer value, not void.
Was This Post Helpful? 1
  • +
  • -

#18 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,746
  • Joined: 25-December 09

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:57 AM

Quote

It's deprecated in C11,

The gets() function was deprecated in C99 it was actually removed from the standard in C11.

Jim

This post has been edited by jimblumberg: 29 November 2012 - 09:58 AM

Was This Post Helpful? 1
  • +
  • -

#19 crippler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 9
  • Joined: 29-November 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:58 AM

View Postvividexstance, on 29 November 2012 - 09:56 AM, said:

I'm just being pedantic, but the main() function is supposed to return an integer value, not void.




Says who? It doesn't matter if you write

int main()
{...
return 0;
}

or

void main()
{...

}
Was This Post Helpful? -2
  • +
  • -

#20 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,111
  • Joined: 19-March 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 10:00 AM

Okay, no. It actually does matter, and if you're thinking of interviewing for a job writing C for someone you should probably do a little boning up on current practice.
Was This Post Helpful? 1
  • +
  • -

#21 crippler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 9
  • Joined: 29-November 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 10:03 AM

View Postjon.kiparsky, on 29 November 2012 - 10:00 AM, said:

Okay, no. It actually does matter, and if you're thinking of interviewing for a job writing C for someone you should probably do a little boning up on current practice.


What is the difference between the two?
Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,746
  • Joined: 25-December 09

Re: RT programming without using "if else"

Posted 29 November 2012 - 10:05 AM

Quote

Says who? It doesn't matter if you write

The C and C++ standards say main() must be defined to return an int, in a hosted environment, and it does matter when using an operating system that actually uses that return value.

You say you are trying to "hone" your skills for job interviews, these interviews usually are very concerned about people using the proper modern techniques. Using the proper form of main() is very important, as is not using known dangerous functions that have been removed from the standard.

Jim

This post has been edited by jimblumberg: 29 November 2012 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

#23 crippler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 9
  • Joined: 29-November 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 10:08 AM

I see

Well, that would be all my friend.
Thank U very much.
Was This Post Helpful? 0
  • +
  • -

#24 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,111
  • Joined: 19-March 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 10:14 AM

The short answer is, the standard says "return an int". That really should end the discussion there.

I'm only an occasional C programmer and a proper answer involves details that I don't deal with every day, so I'd be likely to make some mistakes in my explanation if I tried to go into more detail.

Probably the best thing to do is to assume that there may be a good reason why all of the people who can lead you to an answer on your question say "return an int", and, acting on that assumption, go do the research to find out why we all say this. This is the course that will best prepare you for that job interview you're looking for.

The medium answer is, an explicit return value of 0 on success and some other value on error allows your code to play well with others. If you don't give an explicit return value, you leave room for unnecessary bugs which will be annoying to track down.

(EDIT: As Jim already said, while I was trying to phrase this properly...)

This post has been edited by jon.kiparsky: 29 November 2012 - 10:15 AM

Was This Post Helpful? 1
  • +
  • -

#25 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 10:36 AM

View Postjimblumberg, on 29 November 2012 - 04:57 PM, said:

Quote

It's deprecated in C11,

The gets() function was deprecated in C99 it was actually removed from the standard in C11.

Jim
Haha, yeah, I felt something was wrong and went ISO hunting. gets() is not even in C11 any more, and I edited my post to reflect that. Still, good catch. :)



Also, not that it's actually all that important, as simply using int main() will be fine, but:

Quote

5.1.2.2.1 Program startup
1
The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner.

I think that wording means that it actually is legal to also use other than int return type (I'm not talking about the equivalent which needs to be int-compatible, like typedef-ed int), but that may be that it applies not to hosted implementations. It is a bit confusing. Later on Standard says:

Quote

5.1.2.2.3 Program termination
1
If the return type of the main function is a type compatible with int, a return from the
initial call to the main function is equivalent to calling the exit function with the value
returned by the main function as its argument;11) reaching the } that terminates the
main function returns a value of 0. If the return type is not compatible with int, the
termination status returned to the host environment is unspecified.

Though standardese isn't my native language at all, maybe I'm wrong. It may be that the last sentence is not valid for hosted implementations.

Actually, that would depend on how we interpret the semicolon in the first quote. Is it "returning int and ... or ..." OR "in other implementation defined manner"?
Or is it "returning int" AND (... or ... or "implementation defined").

That's the deal here. ;)

This post has been edited by Xupicor: 29 November 2012 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

#26 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,111
  • Joined: 19-March 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 10:49 AM

"Not specified" means "could be anything at all".

In other words, it would be perfectly legal for a compiler to blow up your computer under these circumstances - this would be within the standard. In fact, it's probably safest to assume "not specified" means "your computer will halt and catch fire", and avoid whatever leads to that.
Was This Post Helpful? 0
  • +
  • -

#27 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,746
  • Joined: 25-December 09

Re: RT programming without using "if else"

Posted 29 November 2012 - 10:55 AM

Quote

I think that wording means that it actually is legal to also use other than int return type

What standard did you get this information from? It matters because before C11 C allowed default types. So main() would be a valid form, pre-C11. However the current standard C11 no longer allows default return types, you must specify the return type. The only place void main() should be valid would be if you are using a non-hosted environment. In a hosted environment main() should return something that looks like an int.

Jim
Was This Post Helpful? 0
  • +
  • -

#28 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 11:02 AM

View Postjon.kiparsky, on 29 November 2012 - 06:49 PM, said:

"Not specified" means "could be anything at all".

In other words, it would be perfectly legal for a compiler to blow up your computer under these circumstances


You're confusing undefined behavior and unspecified values. If the standard says that the behavior of something is undefined, then, as you said, anything can happen. But if it says that the value is unspecified, that only means that the value could be anything - it does not mean that anything could happen other than a value being produced. So for example it does not mean that it might lead to a crash.
Was This Post Helpful? 1
  • +
  • -

#29 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,111
  • Joined: 19-March 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 11:31 AM

View Postsepp2k, on 29 November 2012 - 01:02 PM, said:

View Postjon.kiparsky, on 29 November 2012 - 06:49 PM, said:

"Not specified" means "could be anything at all".

In other words, it would be perfectly legal for a compiler to blow up your computer under these circumstances


You're confusing undefined behavior and unspecified values. If the standard says that the behavior of something is undefined, then, as you said, anything can happen. But if it says that the value is unspecified, that only means that the value could be anything - it does not mean that anything could happen other than a value being produced. So for example it does not mean that it might lead to a crash.


That's true. One the other hand, assuming that unspecified behavior will lead to fiery death leads to better programming practices, don't you think?

This post has been edited by jon.kiparsky: 29 November 2012 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

#30 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 11:50 AM

I quoted C11 latest draft - see my signature for pdf. Sorry for not including this info before.
So, the issue remains, is it "int (this, or that, or impl. defined)" or "(int this, int that) or impl. defined"? ;) The semicolon is the key symbol here in the text.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3