I've been Studying macros for past few hours and created a simple one which calculates the square of the argument provided as follows:
#include<conio.h>
#include<iostream.h>
#define SQR(val) val*val //This is the macro for squaring a number
void main()
{
clrscr();
int vble;
cout<<"Enter any character: ";
cin>>vble;
cout<<"The square of the charater entered is: "<<SQR(vble);
getch();
}
Now if the input is 1 the output is 1
if the input is 3 the output is 9
if the input is 100 the output is 10000
if the input is 200 the output is -25536 meaning that the answer is out of range of int datatype...
Now the same program with a little modification would be as follows:-
#include<conio.h>
#include<iostream.h>
#define SQR(val) val*val //This is the macro for squaring a character
void main()
{
clrscr();
char vble;
cout<<"Enter any character: ";
cin>>vble;
cout<<"The square of the character entered is: "<<SQR(vble);
getch();
}
Now if the input is A the output is 4225 which is bcoz it multiplies the ASCII values....
if the input is 1 the output is 2401 bcoz the ASCII code for the character '1' is 49
My questions are:
1:- When we are giving input in character form why is the answer in int form????
2:- In the first program when we provided input as 200 , the answer should have been 40000 ; however since it was outside range of int datatype therefore it gave a garbage value. As far as i know, the range of char datatype is -128 to 127 and for unsigned it is 0 to 256. So when we gave input as 'A' how come it displayed answer as 4225 when it is clearly out of it's range???????????
3:- When we give input(to the second code i.e. one with char datatype) as 200 the answer is 2500 which is the same when we give input as '2'????????
do you think that "switch" read characters so it could read strings? no, it converting the "char" into integer (because that's the behavior of the "char" type). and i just test it, it only applies for 0 - 9 (since 10+ has no code in ascii)
My questions are:
1:- When we are giving input in character form why is the answer in int form????
2:- In the first program when we provided input as 200 , the answer should have been 40000 ; however since it was outside range of int datatype therefore it gave a garbage value. As far as i know, the range of char datatype is -128 to 127 and for unsigned it is 0 to 256. So when we gave input as 'A' how come it displayed answer as 4225 when it is clearly out of it's range???????????
3:- When we give input(to the second code i.e. one with char datatype) as 200 the answer is 2500 which is the same when we give input as '2'????????
First, please don't use macros in C++ if you can avoid it.
Second, please don't use MS-DOS Borland C. It's old and it's bad for your health.
Third, please write according to current C++ standards.
Now the questions. 1: Your computer sees a character and it knows it has to use its ASCII number in math operations. 2: In the first program you input an integer, on your computer integers I think have a range from -32,768 to 32,767. It doesn't matter if you gave it 'A'! It takes the ASCII code of 'A' and stores it in an integer variable. Not out of range. 3: It tried to store 200 in a char but it was out of range!
(I presumed char is implicitly signed meaning it has a range from -127 to 127).
So it stored a value different from 200. Why don't you use cout << vble << SQR(vble) to see more?
Third, please write according to current C++ standards.
I'll elaborate on this, if you don't mind.
When you #include <iostream> , you should #include <iostream> , not #include <iostream.h> . Although even some newer compilers still let you do it, they usually mark it as deprecated.
Second, instead of void main(), you should have instead put int main() (with any applicable arguments). The current version of g++ *will* complain about this. Change!
Do you use getch() for pausing the program?
That's not its purpose. Anyway, the C++ replacement would be cin.ignore().
You should meditate on this for a minute. Your way of thinking has to change; instead of pausing the program you should either start it on a terminal/console/shell and so its output won't go away after it ends, or redirect the program output to a text file that you can re-read anytime.
I think the problem is that some IDE's automaticly close the console if you run the program from it. I know mine does, and it's inpractical to run your program from the console everytime.
For the 25536, it's not just a garbage value.
You are multiplying 200 by 200, which give you 40000 in mathematics. But you must be using a signed 16 bit integer type for the value, which is limited to [-32768;32767].
The binary representation for 40000 is 1001 1100 0100 0000. So it would need 16 bits to represent the integer part.
But as your type is signed, 1 bit is used for the sign, and you only have 15 bits to represent the integer part. So the processor interprets those 16 bits as if it was representing a signed 16 bit number.
-25536 has this same binary representation on 16 bits, that's why you are obtaining this value.
Your compiler must be too old, at the time of MSDOS, int was 16bit size. If you compile it on a more recent one, int will be 32 bits and you won't have the overflow problem with 200