Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 212 characters in body
Source Link
Mark Lakata
  • 21.1k
  • 6
  • 114
  • 128

I would say that all 3 have drawbacks, but I favor the 3rd with a modification.

The first 2 examples use a feature of scanf that I didn't even know existed, and I'm sure a lot of other people didn't know. Being able to support a feature in the future is important. Even if it was a well known feature, it will be less efficient and harder to read the format string than your 3rd example.

YourThe third example does have a potential buglooks fine. In

(edit history: I made a mistake saying that ANSI or K&R C, there is no guaranteed-C did not guarantee left-to-right orderevaluation of execution, so checking that c != EOF does not necessarily occur after c = getchar(). The optimizer could do strange things with it&& and proposed a change. (In C90However, you can rely on && to force theANSI-C does guarantee left side to be evaluated before the right side)-to-right evaluation of &&.

  I'm not sure about K&R C, but I would rewritecan't find any reference to it asand no one uses it anyways...)

//3rd approach int c; while((c = getchar()) != EOF) if (c == '\n') break; 

I would say that all 3 have drawbacks, but I favor the 3rd with a modification.

The first 2 use a feature of scanf that I didn't even know existed, and I'm sure a lot of other people didn't know. Being able to support a feature in the future is important. Even if it was a well known feature, it will be less efficient and harder to read the format string than your 3rd example.

Your third example does have a potential bug. In ANSI or K&R C, there is no guaranteed left-to-right order of execution, so checking that c != EOF does not necessarily occur after c = getchar(). The optimizer could do strange things with it. (In C90, you can rely on && to force the left side to be evaluated before the right side).

  I would rewrite it as

//3rd approach int c; while((c = getchar()) != EOF) if (c == '\n') break; 

The first 2 examples use a feature of scanf that I didn't even know existed, and I'm sure a lot of other people didn't know. Being able to support a feature in the future is important. Even if it was a well known feature, it will be less efficient and harder to read the format string than your 3rd example.

The third example looks fine.

(edit history: I made a mistake saying that ANSI-C did not guarantee left-to-right evaluation of && and proposed a change. However, ANSI-C does guarantee left-to-right evaluation of &&. I'm not sure about K&R C, but I can't find any reference to it and no one uses it anyways...)

added blurb on C90
Source Link
Mark Lakata
  • 21.1k
  • 6
  • 114
  • 128

I would say that all 3 have drawbacks, but I favor the 3rd with a modification.

The first 2 use a feature of scanf that I didn't even know existed, and I'm sure a lot of other people didn't know. Being able to support a feature in the future is important. Even if it was a well known feature, it will be less efficient and harder to read the format string than your 3rd example.

Your third example does have a potential bug. In ANSI or K&R C, there is no guaranteed left-to-right order of execution, so checking that c != EOF does not necessarily occur after c = getchar(). The optimizer could do strange things with it. (In C90, you can rely on && to force the left side to be evaluated before the right side).

I would rewrite it as

//3rd approach int c; while((c = getchar()) != EOF) if (c == '\n') break; 

I would say that all 3 have drawbacks, but I favor the 3rd with a modification.

The first 2 use a feature of scanf that I didn't even know existed, and I'm sure a lot of other people didn't know. Being able to support a feature in the future is important. Even if it was a well known feature, it will be less efficient and harder to read the format string than your 3rd example.

Your third example does have a potential bug. In C, there is no guaranteed left-to-right order of execution, so checking that c != EOF does not necessarily occur after c = getchar(). The optimizer could do strange things with it.

I would rewrite it as

//3rd approach int c; while((c = getchar()) != EOF) if (c == '\n') break; 

I would say that all 3 have drawbacks, but I favor the 3rd with a modification.

The first 2 use a feature of scanf that I didn't even know existed, and I'm sure a lot of other people didn't know. Being able to support a feature in the future is important. Even if it was a well known feature, it will be less efficient and harder to read the format string than your 3rd example.

Your third example does have a potential bug. In ANSI or K&R C, there is no guaranteed left-to-right order of execution, so checking that c != EOF does not necessarily occur after c = getchar(). The optimizer could do strange things with it. (In C90, you can rely on && to force the left side to be evaluated before the right side).

I would rewrite it as

//3rd approach int c; while((c = getchar()) != EOF) if (c == '\n') break; 
Source Link
Mark Lakata
  • 21.1k
  • 6
  • 114
  • 128

I would say that all 3 have drawbacks, but I favor the 3rd with a modification.

The first 2 use a feature of scanf that I didn't even know existed, and I'm sure a lot of other people didn't know. Being able to support a feature in the future is important. Even if it was a well known feature, it will be less efficient and harder to read the format string than your 3rd example.

Your third example does have a potential bug. In C, there is no guaranteed left-to-right order of execution, so checking that c != EOF does not necessarily occur after c = getchar(). The optimizer could do strange things with it.

I would rewrite it as

//3rd approach int c; while((c = getchar()) != EOF) if (c == '\n') break;