Changing the C code for desired Output - c

I have came across this interview question. I know it's tricky but can't think of any approach.
Change the program so that the output of printf is always 20. Only foo() can be changed. main() function can not be changed.
void foo()
{
// Add Here
}
int main()
{
int i = 20;
foo();
i = 100;
printf("%d", i);
//Some other computation. Doesn't have any printf statements.
return 0;
}

We can use Macro Arguments to change the output of printf.
void foo()
{
#define printf(x, y) printf(x, 20);
}
int main()
{
int i = 20;
foo();
i = 100;
printf("%d", i);
return 0;
}
By using this, during printf("%d",i) will get mapped to macro expansion printf("%d",20)

foo() could merely print 20 and call exit().

Related

Auto VS Static Variable Scope in C programming

Helloo!! I am currently learning about variable scope in C programming, auto, static and extern. I am running this code below and I am not sure why it works. I thought auto variables are only defined in the function it is defined in and does not retain its value? How is it that this code is working? Shouldn't the int a,b be static variables instead?
#include <stdio.h>
void sum(void)
{
auto int a,b;
a = 91; b = 7;
printf("%d + %d = %d\n",a,b,a+b);
}
int main()
{
puts("Calling the sum() function:");
sum();
puts("done");
return(0);
}
Your variables are not retaining anything. They're always the same value because you're explicitly assigning them the same values in sum().
a = 91; b = 7;
If you change your code to accept the variables as parameters instead, you'll get the output you would expect.
#include <stdio.h>
void sum(int a, int b)
{
printf("%d + %d = %d\n",a,b,a+b);
}
int main()
{
puts("Calling the sum() function:");
sum(91, 7);
sum(23, 9);
auto int x = 44;
auto int y = 24;
sum(x, y);
puts("done");
return(0);
}
If you try to access x or y from within sum() now, you'll get an error that they aren't in scope.

C - Is passing static variable by reference acceptable practice?

I would like to ask if there could be any problem in passing a static variable to a function by reference, in order to let that function modify the variable.
A stupid example is:
void increment (int *b)
{
*b = *b + 1;
}
void f()
{
static int a = 4;
increment(&a);
printf("%d\n", a);
}
int main()
{
f();
f();
f();
return 0;
}
I executed it and it seems to work fine, printing
5
6
7
but I would like to understand if this is a sort of "bad" programming practice, that is preferable not to do.

Life of a variable defined in a function other than main

Here's a code written in c
#include<stdio.h>
int foo()
{
static int a=0;
a=a+1;
return a;
}
int main()
{
foo();
foo();
printf("%d",foo());
}
I've compiled this code using gcc11 in eclipse IDE and I've got 3 as my output.
Here's what I think should happen which leads me to the output as 1 not 3.
Function call-1: The main function calls the function foo and the
control goes to the function foo then the variable 'a' in foo is
created with an initial value of zero then it is incremented by
one and this incremented value (1) is returned to the main function. At this step the variables created for the function foo should have been destroyed.
Function call-2: Same as Function call-1:
Function call-3: Same as Function call-1:
In the end the value printed by the printf function in main should have been 1.
Why the output of the program is 3?
Try this with more printing.
#include<stdio.h>
int foo()
{
static int a=0;
a=a+1;
printf("a is now: %d",a);
return a;
}
int main()
{
foo();
foo();
printf("%d",foo());
}
You will notice that the variable a is initialised just the once and its value is retained across function call invocations.
See here: http://en.wikipedia.org/wiki/Static_variable
static variables are not destroyed when the function returns. Remove that keyword and it will work as you expected.
Edit:
The static variable is only present in the scope of its own function. You can create other variables with the same name (static or otherwise) in other functions.
void foo()
{
static int a = 0;
a++;
printf("%d\n", a);
}
void bar()
{
int a = 10;
a++;
printf("%d\n", a);
}
void baz()
{
static int a = 100;
a++;
printf("%d\n", a);
}
int main()
{
foo();
bar();
baz();
foo();
bar();
baz();
return 0
}
This will print:
1
11
101
2
11
102
Here, the variable a is a Static Variable.
Although the scope of variable has to be ended after the complete execution of function. But the Static variable has Scope through out the program and they are not deleted throughout the program.
if it was
int a;
in the place of
static int a;
The result would have been different.
Static variable inside a function keeps its value between invocations.
see the below example for difference between static and normal variable.
void sample()
{
int nv = 10;//nv refers normal variable.
static int sv = 10;//sv refers static variable.
nv += 5;
sv += 5;
printf("nv = %d, sv = %d\n", nv, sv);
}
int main()
{
int i;
for (i = 0; i < 5; ++i)
sample();
}
output:
nv = 15, sv = 15
nv = 15, sv = 20
nv = 15, sv = 25
nv = 15, sv = 30
nv = 15, sv = 35

Postfix decrement

I have a very simple question.
in this piece of code when will the value of n be decremented?
#include<stdio.h>
void func(int n)
{
//text//
}
int main()
{
int n=10;
func(n--);
return 0;
}
now when func() is called is the value of n decremented when control comes back to main() or is it decremented at that time only but n=10 is passed to func().
Please explain, also if there is a way to check the value then that will be really helpful.
When a function is called, all it's arguments are evaluated (in an implementation-defined order) before the function can start - it's a sequence point. So, after all the arguments are evaluated the function can finally begin.
What this means is that n-- is evaluated and yields the value 10 for the function. At the moment the function has begun n is already 9 but the n parameter of the function hold the value 10.
A simple way to check this:
void func(int n, int *np)
{
printf("Outside: %d\n", *np);
}
int main(void)
{
/* ... */
func(n--, &n);
}
The decrement will happen before the call to func, however func will be passed a copy of the old value still.
Consider the following modification to your program which illustrates this:
#include <stdio.h>
static int n;
void func(int m)
{
printf("%d,%d\n", n, m);
}
int main()
{
n = 10;
func(n--);
return 0;
}
Prints:
9,10
I think your question is better expressed by this code:
#include <stdio.h>
static int global_n;
void func(int n)
{
printf("n = %d, global_n = %d\n",
n, global_n);
}
int main()
{
global_n = 10;
func(global_n--);
return 0;
}
This demonstrates that the function is passed the old value, but the decrement happens before the call.
n = 10, global_n = 9

C Recursion program won't compile w/ GCC

#include <stdio.h>
int main (void)
{
int n, x;
int factorial (int n)
{
if (x<=0)
{
printf("x equals: ");
return 1;
}
else
{
return n * factorial (n-1);
}
f(x)=f(x-1)+2; //says error is here
}
return 0;
}
I've tried some things and can't get it to work. I could just be overtired and looking past the smallest thing but help would be much appreciated! Thanks :)
You cannot declare a function definition inside of main() or any other function ... function definitions have to be stand-alone and cannot have embedded function definitions inside of them.
Also I'm not sure what you're doing on the line that you've marked as an error since f() is not a defined function, so you can't call it. Furthermore, it would need to return some type of l-value, such as a pointer to a static variable declared inside the function, or a pointer passed by reference to the function and even then the syntax is not right since there would be a required dereference ... so basically you can't do what you're doing on that line.
To get something that compiles, try
#include <stdio.h>
int factorial (int n)
{
if (n <= 0)
{
return 1;
}
else
{
return n * factorial (n-1);
}
}
int main (void)
{
int x;
x = factorial(5);
printf("Factorial of 5 is equal to %d", x);
return 0;
}
Use indentation to see possible problems with scope:
#include <stdio.h>
int main (void)
{
int n, x;
int factorial (int n)
{
if (x<=0)
{
printf("x equals: ");
return 1;
}
else
{
return n * factorial (n-1);
}
f(x)=f(x-1)+2; //says error is here
}
return 0;
}
As far as I can remember, C doesn't have closures.
A function cannot be defined inside another function. However gcc allows it as an extension. You have defined a function named factorial but are trying to use f which hasn't been declared anywhere.

Resources