Order of evaluation in C [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The code below gives an answer (further below), that I do not understand.
#include <stdio.h>
int fA (int x) {
int w = x;
printf("%d", x);
if (x > 4)
w += fA(x - 2);
if (x > 2)
w += fA(x - 4);
printf("%d", x);
return w;
}
int fB (int x) {
if (x < 1)
return 1;
int w = x;
if (x > 2)
w = w * fB(x - 1);
if (x > 1)
w= w + fA(x - 1);
return w;
}
int main (void) {
printf("\n %d %d \n", fA(6), fB(3));
return 0;
}
it prints
112264004226 12 11
The question is why?
In my opinion it should starts with 6.
Thanks!

There is no guarantee that parameters to a function will be evaluated in any particular order.
So when you call printf with fA(6) and fB(3) as parameters, the compiler is free to call either one before the other.
In this particular case, fB(3) was evaluated first. But if you use a different compiler, it might evaluate fA(6) first.

There's no defined order. This depends on compiler, for example on:
# gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include- dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
this produces
640042261122 12 11
Moreover - the same compiler may decide order to be different for optimization

Is this a puzzle?
I imagine you expect it to start with a 6 because fA(6) comes first. But printf's arguments are being evaluated in reverse order (by my compiler; YMMV), so fB(3) is called first.

Related

Why am I getting an error in this C program? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I am new to C programming but I stumbled on this code
int print(int nb)
{
if (nb < 0)
{
return (0);
}
printf("%d", nb + print(nb - 1));
nb --;
return (nb);
}
int main(void)
{
print(4);
return (0);
}
I ran the code and it gave me an output of 00246
why is that the output that, looking at it logically, the answer is not suppose to start with a 0
print(4) -> print(3) -> print(2) -> print(1) -> print(0) -> print(-1)
print(-1) stops the recursion returning 0, thus a call to printf() is emitted with 0 + 0, which is 0.
print(0) ends with -1 as value, and a call to printf() with 1 + -1 is emitted, which is 0.
etc.

Need to fix a few bugs (preprocessor directives) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
task condition: Using the structure type and preprocessor directives, compile a program for input information about N types of computer equipment, which is known: manufacturer, type (printer, scanner, laptop, mouse, keyboard), color, model), get the price by the formula y = 3x ^ 2 + 4x-2, where x is the number of the option plus N. Sort the prices for computer equipment by the method of "bubbles" in ascending order.
#define N 5
#define M 15
#define PRI(X) 3*X*X+4*X-2
typedef struct Ctechnology
{
char firma[M];
char type[M];
int price[N];
} comp;
int main()
{
comp a;
printf("Firm, type, price - (y=3x^2+4x-2)\n ");
for (int i = 0; i < N; i++)
{
a.price[N] = PRI(((i + 1)+N)); // there is a problem
printf("%d) ", i + 1);
scanf("%s %s", a.firma, a.type);
printf("\n | [%d] | Firm %10s | Type %10s | Price %10d |\n", i + 1, a.firma, a.type, a.price[N]);
}
return 0;
}
Running
#define N 5
#define M 15
#define PRI(X) 3*X*X+4*X-2
price[N] = PRI(((i + 1)+N));
through gcc -E (which runs the preprocessor), we get
price[5] = 3*((i + 1)+5)*((i + 1)+5)+4*((i + 1)+5)-2;
In general, you may want to add parentheses around the Xes in your directive, so they're correctly grouped no matter the input:
#define PRI(X) 3*(X)*(X)+4*(X)-2
->
price[5] = 3*(((i + 1)+5))*(((i + 1)+5))+4*(((i + 1)+5))-2;
Then, of course, there's the matter of price[N] always being an out-of-bounds access.

Can anyone explain and trace the following multiple recursive C program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to understand how recursion works. I seem to not understand how this multiple recursion works. Please help and thanks!
Here is the output
#include<stdio.h>
int R(int x);
int main()
{
R(5);
return 0;
}
int R(int x)
{
if(x > 0){
x--;
R(x);
R(x - 2);
printf("%d ", x);
}
}
R(5) calls R(4) which will display something and then R(2) which will also display something. After that R(5) displays 4. Which is why you see a 4 at the end of the output.
R(4)display - R(2)display - 4
Before the 4 you have the display of R(2). R(2) calls R(1) which displays somenthing and then R(0) which displays nothing. R(2) displays 1. Which is why you have a 1 at the end, just before 4.
R(4)display - R(1)display - R(0)display - 1 4
And so on...
R(4)display - R(1)display - 1 4
R(4)display - 0 1 4
You always process the R()display calls starting from the end (right side first).
As you can see recursivity will inverse the order of the numbers (4, the biggest number, is last). This is because you have the printf after the recursive calls to R.
Take a look at the trace tree I made
R(5) means the call of R function for 5, and as you know the output of it will be 4. The numbers show in which order the functions are executed till the end. The ones that have a tick besides them will make outputs.

Basic C program does not display anything [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
What does my program not output anything. I have tried other similar versions of solutions for project euler problem 1. I don't need the answer I would just like to know why their is no output. After I compile with gcc and execute the file it seems like is freezes with no output. I have to ctrl-z to kill the program.
#include <stdio.h>
/* Project Euler Problem 1 */
int main()
{
int sum = 0;
int i = 0;
while (i <= 1000);
{
if (i % 3 == 0 || i % 5 == 0);
{
sum += i;
}
i++;
}
printf("%d\n", sum);
return 0;
}
You are closing the while without doing any instruction by putting a semi-colon:
while (i <= 1000);
You should drop the semicolon.
The same for the if instruction:
if (i % 3 == 0 || i % 5 == 0);

what is the fault with c here? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Please excuse me for asking this, because I know the code I'm gonna give you is wrong. Being a newbie I am not able to find the fault. Please help me correct the question and give a solution as well. Again I'm sorry to bother with this simple problem. Tomorrow is my exm in C so i'm kinda desperate. :(
Q: What will be the output of the program?
First let me show you how I find the code first:
#include<stdio.h>
int funct l(int n){
if (n>3)
return int funct(n-3)));
}
main() {
int n= 10;
printf("%d", funct l (n));
}
Then I thought i'd correct it. Then I cleaned up the code as far as i can. Then the code came to this:
#include<stdio.h>
int funct(int n){
if (n>3){
return funct(n-3);
}
}
main() {
int n= 10;
printf("%d", funct(n));
}
still it doesn't give proper answer (though I don't know what it'll show). It is either 1 or 2 and process returned 1 (0*1) is showing at the last line.
Please help me out!
Your funct function doesn't always return a value. This means that it could return anything. Try this:
int funct(int n) {
if (n > 3)
return funct(n - 3);
return n;
}
Here is the call stack when n = 10
funct(n = 10)
funct(n = 7)
funct(n = 4)
funct(n = 1)
return 1
return 1
return 1
return 1
Here is the call stack when n = 11
funct(n = 11)
funct(n = 8)
funct(n = 5)
funct(n = 2)
return 2
return 2
return 2
return 2

Resources