Skip to main content
Code Review

Return to Answer

changed some formatting
Source Link
Malachi
  • 29k
  • 11
  • 86
  • 188

Another hint: Never use scanfscanf to read numbers from a terminal. If the user types in a letter, the input gets "stuck" - this and anything later in the same scanfscanf will not parse any input. (Read the spec for scanfscanf to see what I mean.)

Best way to do this sort of thing: (1) declare a buffer of size MAX_INPUT_LENGTH (of your choosing), (2) use fgets to read a buffer of that size, (3) use sscanf to parse the number out.

  1. Declare a buffer of size MAX_INPUT_LENGTH (of your choosing)
  2. Use fgets to read a buffer of that size
  3. Use sscanf to parse the number out.

The C library functions dealing with strings are a real disaster (as they can easily cause buffer overflow, among other problems). It's annoying that the safest way to do something as simple as what you're doing is as complex as this.

Another hint: Never use scanf to read numbers from a terminal. If the user types in a letter, the input gets "stuck" - this and anything later in the same scanf will not parse any input. (Read the spec for scanf to see what I mean.)

Best way to do this sort of thing: (1) declare a buffer of size MAX_INPUT_LENGTH (of your choosing), (2) use fgets to read a buffer of that size, (3) use sscanf to parse the number out.

The C library functions dealing with strings are a real disaster (as they can easily cause buffer overflow, among other problems). It's annoying that the safest way to do something as simple as what you're doing is as complex as this.

Another hint: Never use scanf to read numbers from a terminal. If the user types in a letter, the input gets "stuck" - this and anything later in the same scanf will not parse any input. (Read the spec for scanf to see what I mean.)

Best way to do this sort of thing:

  1. Declare a buffer of size MAX_INPUT_LENGTH (of your choosing)
  2. Use fgets to read a buffer of that size
  3. Use sscanf to parse the number out.

The C library functions dealing with strings are a real disaster (as they can easily cause buffer overflow, among other problems). It's annoying that the safest way to do something as simple as what you're doing is as complex as this.

Another hint: Never use scanf to read numbers from a terminal. If the user types in a letter, the input gets "stuck" - this and subsequent scanfsanything later in the same scanf will not parse any input. (Read the spec for scanf to see what I mean.)

Best way to do this sort of thing: (1) declare a buffer of size MAX_INPUT_LENGTH (of your choosing), (2) use fgets to read a buffer of that size, (3) use sscanf to parse the number out.

The C library functions dealing with strings are a real disaster (as they can easily cause buffer overflow, among other problems). It's annoying that the safest way to do something as simple as what you're doing is as complex as this.

Another hint: Never use scanf to read numbers from a terminal. If the user types in a letter, the input gets "stuck" - this and subsequent scanfs will not parse any input. (Read the spec for scanf to see what I mean.)

Best way to do this sort of thing: (1) declare a buffer of size MAX_INPUT_LENGTH (of your choosing), (2) use fgets to read a buffer of that size, (3) use sscanf to parse the number out.

The C library functions dealing with strings are a real disaster (as they can easily cause buffer overflow, among other problems). It's annoying that the safest way to do something as simple as what you're doing is as complex as this.

Another hint: Never use scanf to read numbers from a terminal. If the user types in a letter, the input gets "stuck" - this and anything later in the same scanf will not parse any input. (Read the spec for scanf to see what I mean.)

Best way to do this sort of thing: (1) declare a buffer of size MAX_INPUT_LENGTH (of your choosing), (2) use fgets to read a buffer of that size, (3) use sscanf to parse the number out.

The C library functions dealing with strings are a real disaster (as they can easily cause buffer overflow, among other problems). It's annoying that the safest way to do something as simple as what you're doing is as complex as this.

Source Link

Another hint: Never use scanf to read numbers from a terminal. If the user types in a letter, the input gets "stuck" - this and subsequent scanfs will not parse any input. (Read the spec for scanf to see what I mean.)

Best way to do this sort of thing: (1) declare a buffer of size MAX_INPUT_LENGTH (of your choosing), (2) use fgets to read a buffer of that size, (3) use sscanf to parse the number out.

The C library functions dealing with strings are a real disaster (as they can easily cause buffer overflow, among other problems). It's annoying that the safest way to do something as simple as what you're doing is as complex as this.

lang-c

AltStyle によって変換されたページ (->オリジナル) /