• Welcome to Valhalla Legends Archive.
 

What can go wrong with this code?

Started by Skywing, November 30, 2003, 11:33 PM

Previous topic - Next topic

Skywing

Grok can't answer because I already explained about this to him.


#include <stdio.h>

int main(int ac, char **av)
{
char ch = getc(stdin);
printf("isspace(ch) returns %u.\n", isspace(ch));
return 0;
}

iago

will getc() actually pick up a whitespace?  Or does it wait till the first character?

If it doesn't pick up whitespaces, the printf() line is irrevelant because it'll never be a space.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: iago on December 01, 2003, 02:36 AM
will getc() actually pick up a whitespace?  Or does it wait till the first character?

If it doesn't pick up whitespaces, the printf() line is irrevelant because it'll never be a space.
getc returns the next character in the stream, whitespace or not.

Adron

You'll probably also have to specify what the program is supposed to do... It looks like it will compile just fine.

QuoteThere's the issue of a possible EOF return from getc, and that you have to input your character, then hit enter with the evil stdio functions.

Eibro

I know the answer from reading chat between Skywing and Kp, but I won't reveal. :)
Eibro of Yeti Lovers.

Skywing

Quote from: Adron on December 01, 2003, 11:31 AM
You'll probably also have to specify what the program is supposed to do... It looks like it will compile just fine.

QuoteThere's the issue of a possible EOF return from getc, and that you have to input your character, then hit enter with the evil stdio functions.
The error in question isn't a compile-time problem.  This program demonstrates a real problem I had to fix in a different program of mine.

Banana fanna fo fanna

Probably unicode chars will kill it, since I'll bet that sizeof(char) == 1.

Skywing

Quote from: St0rm.iD on December 01, 2003, 03:12 PM
Probably unicode chars will kill it, since I'll bet that sizeof(char) == 1.
No, those would be automatically converted into a best-fit ASCII character given the active code page.

iago

It looks like it should do exactly what it's supposed to do.. read in a single character and detects whether it's a whitespace?
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Etheran


Skywing

Quote from: iago on December 01, 2003, 04:04 PM
It looks like it should do exactly what it's supposed to do.. read in a single character and detects whether it's a whitespace?
However, there is a problem with it.  Can you find the problem?

Maybe we should have a C/C++ quiz question of the week :p

iago

Is it because it doesn't return 0 for false and 1 for true, instead it returns 0 for false and non-zero for true?

I decided to compile/run it, and for a space/tab/newline/etc it displayed 8192.  Not that that's reallya  problem, though, but iw oudl be better to do this:
printf("isspace(ch) returns %s", isspace(ch) ? "true" : "false");
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: iago on December 01, 2003, 04:50 PM
Is it because it doesn't return 0 for false and 1 for true, instead it returns 0 for false and non-zero for true?

I decided to compile/run it, and for a space/tab/newline/etc it displayed 8192.  Not that that's reallya  problem, though, but iw oudl be better to do this:
printf("isspace(ch) returns %s", isspace(ch) ? "true" : "false");
isspace returns a zero or a nonzero value according to the documentation.  This isn't the problem...

iago

Then the code works fine.  Skywing is crazy.  
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: iago on December 01, 2003, 05:39 PM
Then the code works fine.  Skywing is crazy.  
Simply because it appears to work fine doesn't mean that nothing is wrong.