[Courses] C Programming For Absolute Beginners, Lesson 2: Fun With Printf, Scanf, Puts, and Variables
Kevin Cole
dc.loco at gmail.com
Wed Mar 7 03:32:56 UTC 2012
On Tue, Mar 6, 2012 at 20:52, Sachin Divekar <ssd532 at gmail.com> wrote:
>
> In my program I did not tell it to consider the input as binary.
> As we are going further I tried different inputs and found some
> strange results.
>
> And following are some inputs I tried.
>
>
> ---------------------------------------------------------------------------------------------------
>
> sachin at sachin-ThinkPad-T420:~/projects/C$ ./addition
> Please enter any number up to three digits:
> 1111111111111111111111111
> You have entered -1
> Please enter another number up to three digits:
> 1111111
> You have entered 1111111
> -1 + 1111111 = 1111110
> sachin at sachin-ThinkPad-T420:~/projects/C$ ./addition
> Please enter any number up to three digits:
> 1111
> You have entered 1111
> Please enter another number up to three digits:
> 111111
> You have entered 111111
> 1111 + 111111 = 112222
> sachin at sachin-ThinkPad-T420:~/projects/C$ ./addition
> Please enter any number up to three digits:
> 1111111111
> You have entered 1111111111
> Please enter another number up to three digits:
> ^C
> sachin at sachin-ThinkPad-T420:~/projects/C$ ./addition
> Please enter any number up to three digits:
> 1111111111111111
> You have entered -1223331385
> Please enter another number up to three digits:
> ^C
> sachin at sachin-ThinkPad-T420:~/projects/C$ ./addition
> Please enter any number up to three digits:
> 32432522134234234324
> You have entered -1
> Please enter another number up to three digits:
> ^C
> sachin at sachin-ThinkPad-T420:~/projects/C$
>
>
> --------------------------------------------------------------------------------------------------------
>
> Is compiler doing something behind the something behind the scene?
>
> I have compiled it in normal ways, like
>
> gcc -o addition addition.c
>
> gcc I am using is "gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5".
>
The compiler has no idea what number you're going to feed it at runtime.
So, it's not intentionally doing anything special. At least, I don't THINK
it is.
However. as to your weird results, my theory: The executable attempts to
grab memory to hold the integer value that you fed it, and when you
overflow, honestly, I would have expected it to error out. But apparently
scanf() is doing bizarre stuff instead. My guess is that it's not actually
storing anything but is establishing a pointer to a memory location. When
you then ask it to add what's in memory at location referred to by variable
"a" to the memory location referenced by variable "b", that it grabs
whatever random crapola is lying there and says "It's all just bits to me,
boss" and happily adds them. Since that memory is freed when your program
exits, other applications and processes running between runs of your
program can take a crack at making use of that space -- meaning that
subsequent runs with identical overflow values as inputs shouldn't
consistently give the same answer. (Try it.)
I leave it to C experts here to either confirm my guess or give you better
details on what's happening.
More information about the Courses
mailing list