How to print a variable's name in C++? [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Programmatic way to get variable name in C?
I have checked some of the blogs before posting this here. I have tried the following snippet...
int a=21;
int main()
{
cout<<#a<<a<<endl;
return 0;
}
I am using g++ compiler on ubuntu 10.04. And I am getting the following error:
sample.cpp:17: error: stray ‘#’ in program.
Please suggest me how to print the variables name .

The # stringifying macro thing only works inside macros.
You could do something like this:
#include <iostream>
#define VNAME(x) #x
#define VDUMP(x) std::cout << #x << " " << x << std::endl
int main()
{
int i = 0;
std::cout << VNAME(i) << " " << i << std::endl;
VDUMP(i);
return 0;
}

The # is for if you are writing a macro.
If your cout line were a macro, it would work the way you expect.
If you're not in a macro, you just type "a".

Related

How to get the namespace name in the c++ code [duplicate]

This question already has answers here:
What's the exact step of macro expanding?
(1 answer)
How do I show the value of a #define at compile-time?
(14 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I'd like to print for a debug reasons the namespace of the variable/function it is defined in. I'm trying to use the define directive:
#include <iostream>
#define NS Static
#define MAKESTRING(x) #x
namespace NS {
int global_x = 8;
void print_global_x() {
//print global_x here:
//std::cout << NS << ": " << global_x << std::endl;
std::cout << MAKESTRING(NS) << "::global_x: " << global_x << std::endl;
}
}
but at the #1 line I'm getting a namespace name is not allowed error while with the second approach (line #2) I'm getting just NS::global_x: 8 instead of Static::global_x: 8
How can I correct the code to get the desired result?

Hashtag symbol in C++ macro expression [duplicate]

This question already has answers here:
What is the # for when formatting using %s
(7 answers)
Closed 2 years ago.
In the macro definition below, what does '#' symbol do? What is this syntax (#x) here?
#define print(x) cout<<(#x)<<" : "<<x<<endl;
# is the stringify operator. It turns the macro argument x into a string literal.
I can see no reason for the extra parens, #define print(x) cout << #x <<" : " << x << endl; would work just as well. Even better would be #define print(x) cout << #x <<" : " << (x) << endl; because the second use of x might require parens to be parsed correctly.

Can't open program because of int main error [duplicate]

This question already has answers here:
Two 'main' functions in C/C++
(13 answers)
Closed 3 years ago.
I know it is duplicated... I didn't understand any of the other threads. Literally just started to learn programming. Currently trying to learn C++ as my first language. Ran into this error, I did google it but I didn't really know what they were talking about. I looked at both of my "int main" things and they are exactly the same. No errors. I guess formatted wrong. Here is the code. I'm currently playing around with the std::count and variables, along with std:cin
#include <iostream>
int main()
{
std::cout << "Hello, how are you doing? I suppose you are only here to read this. Oh well.";
return 0;
}
int main()
{
std::cout << "Please feed me a number: " << "It can be any number."; // Asking human to enter a number.
int x{ }; // get number from keyboard and store it in value x
std::cin >> x; // recieved number and is now entering console
std::cout << "Thank you for feeding me " << x << '\n';
return 0;
}
A C++ program need only one main() function. This latter is called at program startup. Your code should look like this :
#include <iostream>
int main()
{
std::cout << "Hello, how are you doing? I suppose you are only here to read this. Oh well." << std::endl;
std::cout << "Please feed me a number: " << " It can be any number." << std::endl; // Asking human to enter a number.
int x{ }; // get number from keyboard and store it in value x
std::cin >> x; // recieved number and is now entering console
std::cout << "Thank you for feeding me " << x << std::endl;
return 0;
}
Here a link for more reading about the main() function.

How to change value of const variable via its address? [duplicate]

This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
behavior of const_cast in C++ [duplicate]
(3 answers)
Closed 7 years ago.
I'm trying to change value of const variable via its address.
following this code:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int main(void)
{
uint64_t const x = -1;
uint64_t *b = reinterpret_cast<uint64_t*>(0x28ff10);
cout<< x << endl;
cout<< &x << " " << b << " " << *b << endl;
printf("%p\n", &x);
*b = 10;
cout<< &x << " " << x << " " << b << " " << *b << " " << *(reinterpret_cast<uint64_t*>(0x28ff10)) <<endl;
return 0;
}
Compiled with MinGW 4.8.1:
g++ -g main.cpp && ./a.exe
And this is output:
18446744073709551615
0x28ff10 0x28ff10 18446744073709551615
0028FF10
0x28ff10 18446744073709551615 0x28ff10 10 10
Could anyone explain it ?
EDIT:
Figured out! compile still optimized my variable although I compiled it with -O0. Looked at ASM generated, I saw that printf and cout put directly the value instead of the variable symbol.
So, to make my code do right behavior, I have to declared it with volatile static
I'm trying to change value of const variable via its address.
You've already gone wrong by this point.
const is short for "constant".
You cannot mutate a constant.
Sometimes you can get it to sort of look like you did, but doing so has undefined behaviour. You told your compiler that it can make all sorts of assumptions about x (including optimising it out from your binary entirely!) because you promise that you'll never change it. Then you change it.
No dinner for you tonight, says Mr. Compiler!

can anyone please explain me why the two "results" are different? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I have this code:
#include <iostream>
using namespace std;
int & squareRef(int );
int main() {
int number1 = 8;
cout << "In main() &number1: " << &number1 << endl;
int & result = squareRef(number1);
// cout << "In main() &result: " << &result << endl;
cout << result << endl;
cout << result << endl;
cout << number1 << endl;
}
int & squareRef(int rNumber) {
cout << "In squareRef(): " << &rNumber << endl;
rNumber *= rNumber;
return rNumber;
}
The program produces the following output:
In main() &number1: 0x28ff08
In squareRef(): 0x28fef0
64
1875681984
8
Can anyone please explain why the two "results" are different , is that suppose to be same isn't ?
You are invoking undefined behaviour by returning a reference to a local variable:
test.cc:19:7: error: reference to local variable ‘rNumber’ returned [-Werror=return-local-addr]
int & squareRef(int rNumber) {
rNumber is copied on the stack for the call. After the call, the value on the stack is undefined, and might well change due to subsequent calls. The reference you return only points to that location on the stack and does not hold the actual value.
In general, when these things happen, it is very helpful to turn on all warnings your compiler can give you. With gcc, the flags -Wall -Wextra -Werror provide you with a lot of helpful warnings, such as these. In general, code should compile without throwing any warnings (except maybe unused variables/parameters in function stubs, although there are macros to explicitly skip over these).