From: Thomas Demmer Newsgroups: comp.os.msdos.djgpp Subject: Re: A bug in djgpp? Date: Wed, 15 Apr 1998 19:35:58 +0200 Organization: Lehrstuhl fuer Stroemungsmechanik Lines: 73 Message-ID: <3534EFFE.8FF8B8C8@LSTM.Ruhr-UNI-Bochum.De> References: <01bd6885$7fe38c40$a1d06ccb AT bah> NNTP-Posting-Host: bvb.lstm.ruhr-uni-bochum.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Tom Wilson wrote: > > Hi People :I > > I'm a newbie C programmer using djgpp, and while mucking about i found > something weird and i dont really understand it :) > > ok if i compile this peice of code.. > > #include > int main() > { > short b, a; > printf("Choose a value for a: "); > scanf("%i", &a); > printf("Choose a value for b: "); > scanf("%i", &b); > printf("A is equal to %i\n", a); > printf("B is equal to %i\n", b); > return 0; > } > > i get the expected results... The program prints the value of A and B that > you type in. > The problem i have is.. if i declare the variables a and b in the oposite > order.. ie, > > #include > int main() > { > short a, b; [...] > It prints a out as being the value 0, when it shoudn't be. > > I've tried using both peices of code in Visual C++ and they both worked, so > why does DJGPP stuff this up? anybody know? :I Well, type info libc scanf and thoroghly read the format specifiers. And, recompile with -Wall -pedantic. In a nutshell: %i is basically an int specifier, so scanf will affect 4 bytes. You declare them being shorts, two bytes. The first version reads a, then screws up b. Then, you read in b, screwing up two bytes in your stack. It is just plain luck that this version works at all. Why the second version shows the described behavior is left as an exercise. The bottom line: Before suspecting bugs in library code, recompile your code with -Wall -pedantic, or better, -Wall -pedantic-errors and check the warnings. Oh yes, this works in Visual C++ because probably there is sizeof(int)==sizeof(short) -- Ciao Tom ************************************************************* * Thomas Demmer * * Lehrstuhl fuer Stroemungsmechanik * * Ruhr-Uni-Bochum * * Universitaetsstr. 150 * * D-44780 Bochum * * Tel: +49 234 700 6434 * * Fax: +49 234 709 4162 * * http://www.lstm.ruhr-uni-bochum.de/~demmer * *************************************************************