J
John Reye
Hello,
please can someone explain why
(-1 < sizeof("test"))
is false.
What do you think the following will print?
/***********************/
#include <stdio.h>
int main(void)
{
int i;
if (-1 < sizeof("test1")) {
printf("Cool line 1\n");
}
if (-1 < -1 + sizeof("test2")) {
printf("Cool line 2\n");
}
for (i = -1; i >= -3; i--) {
if (i < i + 1U) {
printf("Here %d\n", i);
}
}
return 0;
}
I am truly shocked and amazed.
Is there any coding suggestion that will (in future) save me half an
hour of sprinkling printf's like wild and thinking my compiler is
buggy, and that logic has just died?!!!!!!
What do I need to know to avoid these surprises and is there some
"coding style" that can guard against it.
Thanks!
For the real-world code, that caused my confusion:
#include <stdio.h>
#include <limits.h>
#if EOF != -1
#error EOF is not -1
#endif
int arr[UCHAR_MAX+2]; // storage for every unsigned char, and one
additional value
#define NUM_ARRAY_ELEMENTS(arr) (sizeof(arr)/sizeof(arr[0]))
int main(void)
{
int i;
for (i = EOF; i < EOF + NUM_ARRAY_ELEMENTS(arr); i++) {
printf("%d\n", i);
arr[i+1] = i;
}
return 0;
}
please can someone explain why
(-1 < sizeof("test"))
is false.
What do you think the following will print?
/***********************/
#include <stdio.h>
int main(void)
{
int i;
if (-1 < sizeof("test1")) {
printf("Cool line 1\n");
}
if (-1 < -1 + sizeof("test2")) {
printf("Cool line 2\n");
}
for (i = -1; i >= -3; i--) {
if (i < i + 1U) {
printf("Here %d\n", i);
}
}
return 0;
}
I am truly shocked and amazed.
Is there any coding suggestion that will (in future) save me half an
hour of sprinkling printf's like wild and thinking my compiler is
buggy, and that logic has just died?!!!!!!
What do I need to know to avoid these surprises and is there some
"coding style" that can guard against it.
Thanks!
For the real-world code, that caused my confusion:
#include <stdio.h>
#include <limits.h>
#if EOF != -1
#error EOF is not -1
#endif
int arr[UCHAR_MAX+2]; // storage for every unsigned char, and one
additional value
#define NUM_ARRAY_ELEMENTS(arr) (sizeof(arr)/sizeof(arr[0]))
int main(void)
{
int i;
for (i = EOF; i < EOF + NUM_ARRAY_ELEMENTS(arr); i++) {
printf("%d\n", i);
arr[i+1] = i;
}
return 0;
}