Posted: . At: 12:39 PM. This was 10 months ago. Post ID: 18259
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to print the name of a variable in C. This could be quite useful.


Printing the name of a variable in C could be a very useful trick. This

example.c
1
2
3
4
5
6
7
8
9
#include <stdio.h>
 
#define printme(x) { printf("The value of %s: %d\n", #x, x); }
 
int main(void) {
	#define A (3 - 1)
	volatile int y = A;
	printme(y);
}

This partly obfuscated example shows how this is done.

The printf statement gets the name of a variable and then the value.

Just like this.

printf("The value of %s: %d\n", #x, x);

Use the hash character and then a variable name in the printf statement and then the variable name will be printed instead of the value. This is an easy but fun trick in C programming.

Obfuscation of code is a technique that makes source code harder to understand or reverse-engineer while maintaining its original functionality. There are many ways to obfuscate the simple line int y = 2; in C.

#define A (3 - 1)
volatile int y = A;

In this example, we’re using two techniques.

First, we use the preprocessor directive #define to replace A with (3 – 1). This could potentially make it less obvious what value is being assigned if you were doing something more complex than just subtracting one from three.

Secondly, we declare variable y as volatile, which tells the compiler not to optimize anything related to this variable and keep all operations on it intact within generated executable making reversing the process slightly more challenging.

Please note that these changes won’t truly prevent someone determined enough from figuring out what your code does. Realistically speaking for such a simple assignment operation there isn’t much room for effective obfuscation without resorting to overly complicated constructs would likely raise suspicion anyway If you have genuine considerations about protecting proprietary algorithms consider implementing critical parts as server-side APIs or utilizing native language features like private methods classes etc., where applicable.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.