How can the declaration of an unused variable affect behavior? [message #1006792] |
Fri, 01 February 2013 17:55  |
Eclipse User |
|
|
|
I am using eclipse juno with gcc (from xcode) on MacOS.
The behaviour of the following C program changes radically when the declaration
int x;
is omitted. The variable x does not appear anywhere else in the code. How can this happen?
Here, data.txt is a simple text file with two digit numbers stored as characters. Something like 08 90 88 77 34 etc...
With the declaration of x included, the program opens a file, displays the first two numbers (as integers) and closed the file. Without the declaration, the program opens the file and displays the first number. It doesn't properly close the file.
How can this be?
#include <stdio.h>
int main (void)
{
FILE *fp;
// if I remove this declaration, the program does not work properly!!!
// this variable is not even used in the program
int x;
char str[2];
fp = fopen("data.txt", "r");
if (fp == NULL){
printf("\n unsuccessfull file open \n");
return 1;
}else{
printf("\n successfull file open \n");
}
fscanf(fp,"%2s",str);
printf("%s ",str);
fscanf(fp,"%2s",str);
printf("%s ",str);
if (fclose(fp)) {
printf("\n unsuccessfull file close \n");
}else{
printf("\n successfull file close \n");
}
return 0;
}
|
|
|
Re: How can the declaration of an unused variable affect behavior? [message #1006797 is a reply to message #1006792] |
Fri, 01 February 2013 20:05   |
Eclipse User |
|
|
|
On 02/01/2013 04:55 PM, dan lior wrote:
> I am using eclipse juno with gcc (from xcode) on MacOS.
> The behaviour of the following C program changes radically when the
> declaration
> int x;
>
> is omitted. The variable x does not appear anywhere else in the code.
> How can this happen?
>
> Here, data.txt is a simple text file with two digit numbers stored as
> characters. Something like 08 90 88 77 34 etc...
>
> With the declaration of x included, the program opens a file, displays
> the first two numbers (as integers) and closed the file. Without the
> declaration, the program opens the file and displays the first number.
> It doesn't properly close the file.
> How can this be?
>
>
> #include <stdio.h>
>
> int main (void)
> {
>
> FILE *fp;
>
> // if I remove this declaration, the program does not work properly!!!
> // this variable is not even used in the program
>
> int x;
>
> char str[2];
>
>
>
> fp = fopen("data.txt", "r");
> if (fp == NULL){
> printf("\n unsuccessfull file open \n");
> return 1;
> }else{
> printf("\n successfull file open \n");
> }
>
>
>
> fscanf(fp,"%2s",str);
> printf("%s ",str);
>
> fscanf(fp,"%2s",str);
> printf("%s ",str);
>
>
>
>
> if (fclose(fp)) {
> printf("\n unsuccessfull file close \n");
> }else{
> printf("\n successfull file close \n");
> }
>
> return 0;
> }
>
>
Strings get terminated with a null character. Your array is only 2
bytes long.
|
|
|
|
Re: How can the declaration of an unused variable affect behavior? [message #1006808 is a reply to message #1006798] |
Sat, 02 February 2013 00:40  |
Eclipse User |
|
|
|
On 02/01/2013 08:39 PM, dan lior wrote:
> What really threw me when I was trying to debug this was that the error
> seemingly disappeared when I declared a variable at that spot. I guess I
> was causing the compiler to make some room just after the space
> allocated to the array, thus hiding the mistake.
> Is that right? (or at least possible)
Yes, this is what was happening.
Note that your question really wasn't an Eclipse CDT question, it was a
basic programming question. That really isn't the purpose for these
forums. This forum is for questions specifically relating to using the CDT
|
|
|
Powered by
FUDForum. Page generated in 0.05255 seconds