void fun()
{
// What goes here?
}
void main()
{
int x = 20;
fun();
x = 10;
printf("%d",x); // Should print 20.
}
This was one of my test question. I was wondering if I should use static int
. Can you please help me?
void fun()
{
// What goes here?
}
void main()
{
int x = 20;
fun();
x = 10;
printf("%d",x); // Should print 20.
}
This was one of my test question. I was wondering if I should use static int
. Can you please help me?
I do not condone this practice, and this is a horrible idea. But technically this meets the question's criteria, sort of.
void fun()
{
// Essentially this is a function with an empty body
// And I don't care about () in a macro
// Because this is evil, regardless
#define printf(a, b) (printf)(a, b*2)
}
void main() // I know this is not a valid main() signature
{
int x = 20;
fun();
x = 10;
printf("%d", x);
}
Standard disclaimers apply.
Approach 1: Create a new x
variable in an inner scope.
void fun()
{
#define fun() { int x
#define printf } printf
}
Approach 2: Define a second variable that changes to 10
so that x
can always be 20
.
void fun()
{
#define x x=20,y
}