Macros


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'????????
i'll try as far as i know:

char could be converted to integer data, e.g:

1
2
3
switch (vble) {
case 'a' : //...
}


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)

3. error code perhaps?

CMIIW
Yes Probabaly 3rd Question was error code but plz help nd solve the first two..........
They are really confusing me.....
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?
Last edited on
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!

Happy programming. :)

-Albatross
Second, please don't use MS-DOS Borland C. It's old and it's bad for your health.

What else do i use then?????
And where do i source it from????
Dev-C++ (old, 2005) http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe
or
wxDev-C++ (new, 2011) http://sourceforge.net/projects/wxdsgn/files/wxDev-C%2B%2B/Version%207/wxdevcpp_7.3.1_full_setup.exe/download

Be warned. No more #include <conio.h> , no more void main().
You will need to learn the modern way to write programs.
Last edited on
so what should we replace getch() with? i can't find a better function than that...
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.
Last edited on
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.
closed account (z05DSL3A)
That's not its purpose. Anyway, the C++ replacement would be cin.ignore().

and if the stream is not clear? It fails to wait and keeps on going.

Edit: cin.ignore() is NOT a C++ replacement for getch().
Last edited on
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Reminds me of another thread.
closed account (z05DSL3A)
Still if the stream in not empty when it gets to cin.ignore, it will empty it and move on.
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;

int main()
{
    int x(0);
    cout << "Please enter a number: ";
    cin >> x;

    cin.ignore(); //will not stop hear
}

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
Indeed cin.ignore() fails even with those ugly parameters.
I then read about cin.get() and it worked.

Edit: no it didn't. Ooops.
Last edited on
Topic archived. No new replies allowed.