Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this C++ code output the result?

Tags:

c++

This is the C++ code:

#include<iostream>
using namespace std;


int a=8;

int fun(int &a)
{
    a=a*a;
    return a;
}

int main()
{

    cout << a << endl \
        << fun(a) << endl \
        << a << endl;
        return 0;
}

why does it output:

64 64 8

the << operator's associativity is left to right, so why not output 8 64 64?

Does it have the relation to the sequence point and the effect side?

like image 405
Tanky Woo Avatar asked Mar 25 '12 01:03

Tanky Woo


People also ask

What is the output for C code?

Output is dependent on the compiler. For 32 bit compiler it would be fffffffe and for 16 bit it would be fffe.

What does '?' Mean in C programming?

Most likely the '?' is the ternary operator. Its grammar is: RESULT = (COND) ? ( STATEMEN IF TRUE) : (STATEMENT IF FALSE) It is a nice shorthand for the typical if-else statement: if (COND) { RESULT = (STATEMENT IF TRUE); } else { RESULT = (STATEMENT IF FALSE);

What is input and output statement in C?

In every C program, three basic functions take place – accepting of data as input, the processing of data, and the generation of output. The acceptance of data refers to input and the presentation of data refers to the output. The C program accepts input from the keyboard and displays output on the screen.

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.

What is output in C programming?

C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement. When we say Output, it means to display some data on screen, printer, or in any file. C programming provides a set of built-in functions to output the data on the computer screen as well as to save it in text or binary files.

What is the use of main () function in C programming?

All valid C program must contain the main() function. The code execution begins from the start of main() function. The printf() is a library function to send formatted output to the screen. The printf() function is declared in "stdio.h" header file.

What is the difference between printf () and main () in C?

All valid C programs must contain the main () function. The code execution begins from the start of the main () function. The printf () is a library function to send formatted output to the screen. The function prints the string inside quotations.

What does int puts do in C++?

The int puts (const char *s) function writes the string 's' and 'a' trailing newline to stdout. NOTE: Though it has been deprecated to use gets () function, Instead of using gets, you want to use fgets ().


1 Answers

Associativity and evaluation order are not the same thing. The expression a << b << c is equivalent to (a << b) << c due to left-to-right associativity, but when it comes to evaluation order, the compiler is free to evaluate c first then a << b and, likewise, it can evaluate b before it evaluates a. In fact, it can even evaluate the terms in the order bca if it wants, and it just might if it concludes that such an order will maximise performance by minimising pipeline stalls, cache misses, etc.

like image 52
Marcelo Cantos Avatar answered Oct 06 '22 19:10

Marcelo Cantos