What happens when you try to compile a program if you declare the iostream header file before #include "stdafx.h"
If you're using visual studio and precompiled headers are turned on (which they are by default), the compiler will error. This is because visual studio requires #include "stdafx.h" to be the first line of a code file when precompiled headers are turned on. You can either comply with that restriction, or turn precompiled headers off.
I don't understand. what's the importance of this lesson ??
Is this even used a lot? 'cause it doesn't really look like it does ?
Integers are used everywhere in C++. This lesson talks about the different kinds of integers, and some of the things you need to watch out for (e.g. overflow).
Alex, you made an error on your website "In addition to being *less* typing, adding the prefix int makes the type harder to distinguish from variables of type int. This can lead to mistakes if the short or long modifier is inadvertently missed."
Alex, can you explain the error happen.
"error: '::main' must return 'int'"
thank you mr nascardriver. one more thing, why when the max value of int and unsigned int were add by 1 they return to minimum value?
All bits get set to 0 and the first bit turns into a 1.
What if that first bit wasn't there, because you've already used all bits?
This is what should happen, but we're limited to 4 bits (In this example), so the most significant bit disappears and we're left with 0.
ohh.. so in this case it called interger overflow?
Correct, because the resulting number overflows into bits that aren't part of the variable.
Ah, the overflow issue reminds me of this glitch from the original Civilization game. Ghandi's aggression rating starts at 1, and researching democracy reduces aggression by 2... lo and behold, nuclear armed Ghandi with 255 aggression in the late game. Normally, leaders are given a rating from 1 to 10, but I guess they had an unsigned integer there.
Yes, this is a great example of a famous overflow glitch! And one that wouldn't have happened if they'd used a signed integer instead of an unsigned char!
Thanks for bringing this up, undoubtedly other readers will find this interesting.
how can a char be signed?
A char is an 8bit integer. Your compiler/console just know how to read/display it as a character.
excuse me mr nascardriver sir but um what does this have to do with bernie sanders?
"Note that C++ only guarantees that integers will have a certain minimum size, not that they will have a specific size."
Does it mean that on some architecture the int data type can have a size of 4 bytes, but on some other architecture it can have a size of 8 bytes? If the answer is true, does it affect the range of int (on the architecture with 4-byte sized int a range of -2,147,483,648 to 2,147,483,647 and on the architecture with 8-byte sized int a range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.) Thanks!
Yes, though most often an int will be 2 or 4 bytes. I'm not aware of any architectures that have an int size of 8 bytes, but that doesn't meant there aren't or won't ever be.
Thank you! I'm still wondering if it does affect the range. I don't think the maximum value of an 4-byte sized int can fit in a 2-byte sized int. Basically, a 2-byte sized one is the same thing as the short data type. Right?
The maximum of a 4-byte sized integral date type cannot fit in a 2-byte sized one. If that was the case there wouldn't be a need for anything bigger than 2 bytes.
"A" .....97- "a" ..... 256 "smily" ..etc + my program continuously executing countless time with computer warning sound "beep"
When you print a char using std::cout it prints as the ASCII value with that code. So your program is printing all of the ASCII code points from 0 to 255. However, not all ASCII code points are printable, and the ones above 127 aren't well defined. If you try to print these, the results are indeterminate. In your case, one of those unprintable characters is translated by your console as a beep. The smiley is your console's font showing a smiley for one of the characters (probably one of the ones above 127).
Quick (and perhaps stupidly obsessive) question: Is overflow the same (or does it at least work the same) as "narrowing"?
I would also assume that overflow occurs on all the arithmetic types, since it is usually referred to as either "arithmetic overflow" or "arithmetic underflow," but is that even true?
Thank you Alex for your help, I'm 13 and I learned a course on Openclassrooms but the course was obsolete, I looked for a book in the web I saw C++ Primer and few time after I saw your site Learncpp.com. Since I had these two ressources, I don't lose the time. I have to say that you did the layoutfor the homepage very attracting. I'm very happy for reaching a site like this and a book like C++ primer which is a good ressource too. I moving very well. God Bless you, you deserve it.
WHO'S THERE TO TELL THE COMPILER TO PRODUCE AN INTEGER RESULT?
WILL BOTH THE INTEGERS NOT BE TYPECASTED TO FLOAT BY THE COMPILER ITSELF TO PRODUCE A FLOAT RESULT?
The compiler produces an integer result if both of the operands are integers, which they are in this case. They will not be typecast to a float. If either or both of the operands are float, then you'll get a float result.
In future lessons we talk more about how type conversions happen and more about how operators work, which should help make this clearer.
Hi Alex, I want to say I'm enjoying the site and the C++ lessons are outstanding. I just have 1 concern for this topic about integers. That being, I really don't understand the purpose of these integers and their limits. Like char, short, long, and long long I dont understand why they would be used in a code and why having a limit to their size would be about. Keep in mind I'm a beginner with absolutely no experience, just the lessons following up to this one.
Integers are used when you want to store whole numbers (e.g. 1, 2, 3). For example, someone's age in years, or the number of times something has occurred. The types (char, short, int, long, etc...) determine how large of an integer that type can store.
What else can I help clarify for you?
That helps me all I needed, thanks a lot.
I was curious to know how the example with unsigned short x = 0; x = x -1 got the result of 65335, but I was able to work out why using the complement method as detailed on this page here: http://www.wikihow.com/Subtract-Binary-Numbers.
You can get the same result by initializing an unsigned short to -1.
I talk more about binary representation and twos complement in chapter 3.
how to find the absolute difference between 2 integers?
is there any built in function for it?
Yes, you can use the abs() function that lives in the cstdlib header.
I played with numbers after going through this part and noticed an odd thing. If I use the code below, and add a decimal number to X, like say, 0.5. The program goes wild and starts spamming "Add to X: X is -1". What happens here and why does it completely break the program like that?
It has to do with how cin and operator>> process input. If you type 0.5, the 0 gets extracted to x, but the '.' gets left in the input stream. Next iteration, it tries to extract the '.', which fails because character '.' can't be extracted to an integer. This causes cin to go into failure mode. I talk about this in more detail in lesson 5.10, if you want to read ahead.
hey guys i have a small problem.why does this program give no value?Is it because the +1 is out of the range and one gets and over flow of the bit.
That's not a program, it's an expression. If it's giving no value, it may be because you're not sending the result of the expression to std::cout?
An unsigned integer is one that can only hold positive values.
When I assigned "-7" to "unsigned int a", I expected something wrong in the output but it works! The output is -9.
Essentially, -7 and 4294967289 are stored as the same number in binary -- the difference is in how they are interpreted based on type. Because your result variable is signed, your result is being interpreted as a signed number (-9) instead of a large unsigned number (4294967291 I think).
I cover related topics in chapter 3, so if the above seems a bit incomprehensible at this point, keep reading.
I've finished Chapter 3. Please correct me if I am wrong.
-7 is represented in binary as 1111 1111 1111 1111 1111 1111 1111 1001.
2 is represented in binary as 0000 0000 0000 0000 0000 0000 0000 0010.
result = 1111 1111 1111 1111 1111 1111 1111 0111.
Because result is signed, it is being interpreted as a signed variable that can hold both negative & positive numbers (-9 & 4294967287). Both numbers (-9 & 4294967287) are represented in binary as 1111 1111 1111 1111 1111 1111 1111 0111.
Now I need to know which number (-9 or 4294967287) should be printed.
From what I've learned in Chapter 3- *Signed numbers and two's complement, I am able to tell (-9) should be printed.
*Signed numbers and two's complement: Signed integers are typically stored using a method known as two's complement. In two's complement, the leftmost (most significant) bit is used as the sign bit. A 0 sign bit means the number is positive, and a 1 sign bit means the number is negative.
To convert a two's complement binary number back into decimal, first look at the sign bit.
If the sign bit is 0, just convert the bits for unsigned numbers (I modified this sentence for my example).
So, looking at "1"111 1111 1111 1111 1111 1111 1111 0111, I can tell the number is negative (a 1 sign bit).
-Covert to decimal: 0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+9+0+0+0 = 9.
-Since the original sign bit (1) was negative, the final value is -9.
How can I learn easily the C++?
I don't often read the comments, but I'm glad I did so I could see this gem.
Somewhere in this article, you talk about how a variable integer can be named 32-bits integer instead on long. Is that because, in some machines, long takes 32 bits? What am I not understanding correctly?
On 32-bit systems, long is often 32-bits. On 64-bit systems, long is often 64 bits. So yes, we typically use the number of bits when discussing integers because the names (int, long) can vary in size on different machines/architectures.
Thank you for your answer you're really helpful. Are you the author of this website?
this happened when i do'nt use "signed" too.
Something else must be going on, because that line is fine.
There are 10 people in the world who understand binary. Those who do and those who don't.
Better phrasing: There are 10 types of people in this world, those who understand binary and those who don't.
Hello and many thanks for putting up such a well written tutorial. It's perfect for people like me who want to start from scratch (or almost).
I am very confused right now. Could you please shed some light?
You get -1. Why? Because 1 is a signed number, whereas x is unsigned. Because you're mixing types, the compiler does an implicit conversion, and x is converted to a signed integer (the rules for this are covered in lesson 4.4). Thus, x - 1 is signed, and so you get -1 instead of 65535.
This avoids the type conversion.
In general, you should not mix signed and unsigned numbers, because the results are often unexpected.
Thank you for the clarification. And, again, for the whole tutorial, even though I'm only at bitwise operations right now. I have to say that these 3 days (4 this one) since I started reading this, made me realize I *understood*.
char data type doesn't stores any numeric value and signed /unsigned just tells us that whether the data type would have -ve values or not.
Char does store numeric values, and you can do integer math with them. They're just not _typically_ used for that purpose.
@static_cast is more restricted in the types in can cast. For int, there is no difference between the two. The second version shouldn't be used in C++. Casts are covered in a later lesson.
Thanks for replying! Sorry I don't understand what you mean by "For int, there is no difference between the two"
If you don't understand it, don't worry, it's covered later. For now, just use the first version.
Great Tutorial,i have read many pdf tutorials but i gave up along the way because i wasn't understanding,i thank God i have found this online, and now am back again with C++. Better understanding May Almighty Allah(God) Bless you Abundantly Great ALEX.
When you will add more hindi chapters....?
Alex, I've been trying to get this for a long long time... in 2 articles (2.3 and 2.4) you managed to explain it to me in such a simple way. Thank you for your work and thank you for sharing it.
prints 255, 256 normally. What am i missing?
Shorts are normally 2 bytes, so the range of a signed short is usually -32768 to 32767.
Yeeea i just got it... 15 bits used for numbers amd the 16th used for +/-.
Keep up the amazing work. Thanks for replying so quickly.
I understand the wraparound result in the previous example, but not quite sure why this wraparound results in 65535? Trying to understand via the binary, but I can't figure it out. Can anyone help??
Note to Alex: Thanks for this tutorial! You're awesome.
In the previous example, we showed that 65535 + 1 = 0. If we subtract one from each side, we get 65535 = - 1. So symmetrically, this makes sense.
Same with the binary version. 0 is 0000 0000 0000 0000 in binary. If we subtract 1, then we get binary 1111 1111 1111 1111, which is 65535. We talk more about how integers convert to binary in chapter 3.
Good point. However, your solution is equally incorrect. :) It should be -(2^n-1), so the 2^n-1 part evaluates first, and then the negative is applied.
I've updated the lesson accordingly.