How can the declaration of an unused variable affect behavior? [message #1006792] |
Fri, 01 February 2013 22:55 |
dan lior Messages: 4 Registered: October 2012 |
Junior Member |
|
|
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] |
Sat, 02 February 2013 01:05 |
David Wegener Messages: 1445 Registered: July 2009 |
Senior Member |
|
|
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.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05107 seconds