[Courses] C Programming For Absolute Beginners, Lesson 2: Fun With Printf, Scanf, Puts, and Variables

Sachin Divekar ssd532 at gmail.com
Wed Mar 7 01:52:46 UTC 2012


On Wed, Mar 7, 2012 at 5:19 AM, Kevin Cole <dc.loco at gmail.com> wrote:

>
> On Tue, Feb 21, 2012 at 13:40, Sachin Divekar <ssd532 at gmail.com> wrote:
>
>> I tried a very large input to addition program. Following was the result.
>>
>> ==========================================
>> sachin at sachin-ThinkPad-T420:~/projects/C$ ./addition
>> Please enter any number up to three digits:
>> 1111111111111111111111111
>> Please enter another number up to three digits:
>> 1111111111111111111111111111111111111111111111
>> -1 + -1 = -2
>> ===========================================
>>
>> A quick reading on Wikipedia about C data type "int" says,
>>
>> "basic signed integer type. At least 16 bits in size."
>>
>> So, what exactly "16 bits in size" means? I know 1 bit is either state
>> 1 or 0. But how this(bits) determines size of number?
>>
>
> A bit about bits: If you understand the basic idea of Morse code -- dots
> and dashes -- think of them as 0 and 1 instead: A "letter" in Morse code is
> comprised of several dots and dashes combined.  Numeric values (and
> alphabetic values, and instructions, and memory addresses, and all sorts of
> other weird wonderful stuff) in computers is comprised of multiple bits.
>
> Bit: Binary Digit.  Let's say a "dit" is a "Decimal Digit" for comparison:
> In our standard Western counting system there are only ten digits: 0, 1, 2,
> 3, 4, 5, 6, 7, 8, 9.  If I tell you that a calculator can only handle an 8
> digit number, how does that determine the size of the number?  In our
> day-to-day counting system (base 10, or decimal), each place represents a
> power of ten:
>
> So, 247 is 200 + 40 + 7 or (2x10^2) + (4x10^1) + (7x10^0)
>
> In binary (a.k.a. base 2), each position represents a power of 2 -- when
> talking about positive integers.  (Negative numbers aren't that hard to
> understand, but I'll leave that for another post.)
>
> So, if I say a 4-bit value, or a 10-bit value, or a 16-bit value, I'm
> talking about the number of binary digits in the number.  With 4 bits
> (unsigned), you can represent positive integers from 0 to 15, like so:
>
> 0000 = 0*2^4 + 0*2^3 + 0*2^1 + 0*2^0 = 0 + 0 + 0 + 0 =  0
> 0001 = 0*2^4 + 0*2^3 + 0*2^1 + 1*2^0 = 0 + 0 + 0 + 1 =  1
> 0010 = 0*2^4 + 0*2^3 + 1*2^1 + 0*2^0 = 0 + 0 + 2 + 0 =  2
> 0011 = 0*2^4 + 0*2^3 + 1*2^1 + 1*2^0 = 0 + 0 + 2 + 1 =  3
> 0100 = 0*2^4 + 1*2^3 + 0*2^1 + 0*2^0 = 0 + 4 + 0 + 0 =  4
> 0101 = 0*2^4 + 1*2^3 + 0*2^1 + 1*2^0 = 0 + 4 + 0 + 1 =  5
> 0110 = 0*2^4 + 1*2^3 + 1*2^1 + 0*2^0 = 0 + 4 + 2 + 0 =  6
> 0111 = 0*2^4 + 1*2^3 + 1*2^1 + 1*2^0 = 0 + 4 + 2 + 1 =  7
> 1000 = 1*2^4 + 0*2^3 + 0*2^1 + 0*2^0 = 8 + 0 + 0 + 0 =  8
> 1001 = 1*2^4 + 0*2^3 + 0*2^1 + 0*2^1 = 8 + 0 + 0 + 1 =  9
> 1010 = 1*2^4 + 0*2^3 + 1*2^1 + 0*2^0 = 8 + 0 + 2 + 0 = 10
> 1011 = 1*2^4 + 0*2^3 + 1*2^1 + 1*2^0 = 8 + 0 + 2 + 1 = 11
> 1100 = 1*2^4 + 0*2^3 + 0*2^1 + 0*2^0 = 8 + 4 + 0 + 0 = 12
> 1101 = 1*2^4 + 1*2^3 + 0*2^1 + 1*2^0 = 8 + 4 + 0 + 1 = 13
> 1110 = 1*2^4 + 1*2^3 + 1*2^1 + 0*2^0 = 8 + 4 + 2 + 0 = 14
> 1111 = 1*2^4 + 1*2^3 + 1*2^1 + 1*2^0 = 8 + 4 + 2 + 1 = 15
>
> Notice that the last value above is 2^4 (i.e. 16) minus 1.
>
> With 16 bits, you can represent positive integers between 0 and (2^16 - 1)
> which works out to 65,535.
>
> I guess all that was a wee bit much. Wasn't it? ;-)
>
> P.S. In your program, did you tell it that you were inputting binary
> numbers?  I mentioned that I didn't want to get into negative integers, but
> from the look of your result, you did indeed add the binary value for
> negative 1 to the binary value for negative 1, and then printed out the
> decimal values of each, plus their sum.  If you want to slog through it on
> your own, look up "two's complement" math. (Or I can write  slightly
> shorter dissertation on that one. ;-) For now, suffice it to say that
> negative numbers in binary are represented in a rather different way and
> when you see a slew of ones 11111111111 the mind leaps to "Ah, -1".)
>

Dear Kevin,

Thanks for the bit about bit.

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.

Here is my program.

// addition.c

#include <stdio.h>

int main()
{
int a, b, c;
 puts("Please enter any number up to three digits: ");
scanf("%d", &a);
 printf("You have entered %d\n", a);
puts("Please enter another number up to three digits: ");
 scanf("%d" ,&b);
printf("You have entered %d\n", b);
 c = a + b;
printf("%d + %d = %d\n", a, b, c);
 return 0;
}



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$
sachin at sachin-ThinkPad-T420:~/projects/C$
sachin at sachin-ThinkPad-T420:~/projects/C$
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".

--
Regards,
Sachin Divekar


More information about the Courses mailing list