Help with accelerated C++

Pages: 12
Hello,

I just read through the first chapter of accelerated c++, and I ran into this exercise.

0-1. What does the following statement do?
3 + 4;

Alright, so I put it into the program like this:

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
    3 + 4;

    system("pause");
    return 0;
}


When I compile & debug it, it doesn't give me anything in the cmd, only "Press a key to continue"

Why?
The program did mental arithmetic. You do not use the result of the expression so it is not clear what you are waiting.:)
Last edited on
The answer is: "Depends on your compiler and optimization options"
I do not remember if standard tells how compiler should handle these issues, so I suppose it can be anything from nothing (3+4 will be replaced by compile-time constant) or putting 3 and 4 into CPU registers, adding these values, putting them into RAM and finally using them somewhere or deallocating said RAM.

Most up-to-date compilers will just optimize out this line.
Last edited on
So the answer is that it is just blank because I didn't give it an int or?

It should have been int 3; & int; 4 before it would work?
if you make this line something like int foo = 3 + 4; most compilers will optimize it and it will behave like int foo(7);
And if you don't use foo variable after, it could be completely optimized out.

EDIT: sample code
1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
    int x = 3;
    int y = 4;
    int z = x + y;
    std::cout << z;
}
when compiled w/ -O3 (actually even -O1 — but not -O — will suffice) left out all variables, transforming code to the equivalent of std::cout << 7;
Last edited on
I don't want it to be int's.
It is a question in Accelerated, asking exactly this:

0-1. What does the following statement do?
3 + 4;

Ok, it will add 3 and 4 together and return 7 as result.
Last edited on
No, it doesn't write anything at all.
@Dario z
I don't want it to be int's.
It is a question in Accelerated, asking exactly this:

0-1. What does the following statement do?
3 + 4;


Checks whether the computer knows the add table.:)
I use this:

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
    3 + 4;

    system("pause");
    return 0;
}


CMD window pops up with no answer, is that the answer to the question, nothing happens?
If is adding 3 and 4 together and return result. However you are not using this result anywhere so it completely logical that you didn't see anything.
Ok, it will add 3 and 4 together and return 7 as result.
No, it doesn't write anything at all.

The first answer is half-correct. The expression 3 + 4 will evaluate to 7, assuming it's executed as written and not optimised away. It doesn't return anything, as such, because it's not a function or a method, but I assume that's what vlad meant.

But that expression doesn't actually do anything. It certainly doesn't write anything. So the second answer is also correct.
Well, I simplified it. 3 + 4 is an expression which evaluates to 7. You can actually see the result using std::cout << 3 + 4;
If you take a look at the answers it tells you:
This statement is an expression that yields the result 7 and has no side effects.
http://www.parkscomputing.com/accelerated-cpp-solutions/00-gettingstarted/
Answer: It is an expression, result is 7; the statement has no side effects.
http://acceleratedcplusplus.wordpress.com/

EDIT: Read chapter 0.7, second paragraph.
Last edited on
But it doesn't come up, so answer is that it doesn't give any side effects, so it doesn't print out.
But it doesn't come up, so answer is that it doesn't give any side effects, so it doesn't print out.

That's another way of saying the same thing. "No side effects" includes the fact that it doesn't output anything to the console window.
Sweet, thanks for helping everyone :)
Why would you expect it to output anything at all if you did not even write "std::cout" anywhere? In the future remember: the program does exactly what you tell it to, nothing less and nothing more. If it is not doing something it is 100% of the time because you did not tell it to in that case.

In this case, you told it to add, but you never told it to display the result of adding, so it did not display anything.
He didn't write anything - he's asking about an example shown in a book.
Last edited on
I think the OPer explained that several times already.
Well yeah, but in your response, you said "you did not even write...", "you never told it to...", etc, as if it was his code. I thought you may have missed the context of the OP's question.
Pages: 12