my friend sent me this programme:
#include<iostream>
int main()
{
int a=5;
std ::cout << ++a
<< "\t"
<< ++a
<<
return 0;
}
its output should be "6 7" and this i what i get:
(arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new2.cpp
new2.cpp: In function 'int main()':
new2.cpp:10: warning: operation on 'a' may be undefined
(arnuld@arch ~ )% ./a.out
7 7
(arnuld@arch ~ )%
to not to relay on "undefined behaviuor" i changed this programme:
#include<iostream>
int
main()
{
int a =
5;
int b = +
+a;
int c = +
+a;
std::cout <<
b
<<
"\t"
<<
c
<<
std::endl;
return
0;
}
and now it runs as expected:
(arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new.cpp
(arnuld@arch ~ )% ./a.out
6 7
(arnuld@arch ~ )%
i want to know why the direct use of ++a in std::cout is undefiend
behaviour" ? 8 1814
On Jul 15, 11:10 pm, arnuld <geek.arn...@gmail.comwrote:
my friend sent me this programme:
#include<iostream>
int main()
{
int a=5;
std ::cout << ++a
<< "\t"
<< ++a
<<
return 0;
}
its output should be "6 7" and this i what i get:
(arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new2.cpp
new2.cpp: In function 'int main()':
new2.cpp:10: warning: operation on 'a' may be undefined
(arnuld@arch ~ )% ./a.out
7 7
(arnuld@arch ~ )%
to not to relay on "undefined behaviuor" i changed this programme:
#include<iostream>
int
main()
{
int a =
5;
int b = +
+a;
int c = +
+a;
std::cout <<
b
<<
"\t"
<<
c
<<
std::endl;
return
0;
}
and now it runs as expected:
(arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new.cpp
(arnuld@arch ~ )% ./a.out
6 7
(arnuld@arch ~ )%
i want to know why the direct use of ++a in std::cout is undefiend
behaviour" ?
It isn't. You can use ++a directly in your cout statement. If you take
a closer look, your first post didn't have "<< endl;" or a semicolon
at all which is what caused it.
On Jul 16, 7:10 am, arnuld <geek.arn...@gmail.comwrote:
my friend sent me this programme:
#include<iostream>
int main()
{
int a=5;
std ::cout << ++a
<< "\t"
<< ++a
<<
return 0;
}
its output should be "6 7" and this i what i get:
(arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new2.cpp
new2.cpp: In function 'int main()':
new2.cpp:10: warning: operation on 'a' may be undefined
(arnuld@arch ~ )% ./a.out
7 7
(arnuld@arch ~ )%
to not to relay on "undefined behaviuor" i changed this programme:
#include<iostream>
int
main()
{
int a =
5;
int b = +
+a;
int c = +
+a;
std::cout <<
b
<<
"\t"
<<
c
<<
std::endl;
return
0;
}
and now it runs as expected:
(arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new.cpp
(arnuld@arch ~ )% ./a.out
6 7
(arnuld@arch ~ )%
i want to know why the direct use of ++a in std::cout is undefiend
behaviour" ?
Hi, I think it might have been down to the fact that you did not
include an endl; statement or even just a ;
#include<iostream>
int main()
{
int a = 5;
std ::cout << ++a << "\t;
std ::cout << ++a;
return 0;
}
This returned 6 7.
Hope that helped.
On Jul 16, 11:37 am, szcl...@googlemail.com wrote:
Hi, I think it might have been down to the fact that you did not
include an endl; statement or even just a ;
#include<iostream>
int main()
{
int a = 5;
std ::cout << ++a << "\t;
std ::cout << ++a;
return 0;
}
This returned 6 7.
you introduced an extra statement in the programme for producing the
correct result. why i can not get the same output using one single
statement ?
On 16 Jul, 07:57, arnuld <geek.arn...@gmail.comwrote:
On Jul 16, 11:37 am, szcl...@googlemail.com wrote:
Hi, I think it might have been down to the fact that you did not
include an endl; statement or even just a ;
#include<iostream>
int main()
{
int a = 5;
std ::cout << ++a << "\t;
std ::cout << ++a;
return 0;
}
This returned 6 7.
you introduced an extra statement in the programme for producing the
correct result. why i can not get the same output using one single
statement ?
I think this is because the compiler will increment 'a' twice before
the line is output to the screen, so the output will then be 7 7.
arnuld wrote:
my friend sent me this programme:
#include<iostream>
int main()
{
int a=5;
std ::cout << ++a
<< "\t"
<< ++a
<<
return 0;
}
So, this is only one statement:
std::cout << ++a << "\t" << ++a << std::endl;
therefore a is incremented twice before anything is done (ie. something
is printed to standard output).
That means you got the correct output.
I can not back this up with a chapter number from the standard as I do
not have it, but this is how c++ works with prefix increment operator
>
i want to know why the direct use of ++a in std::cout is undefiend
behaviour" ?
IMO undefined behavior is something different, not this
arnuld wrote:
my friend sent me this programme:
#include<iostream>
int main()
{
int a=5;
std ::cout << ++a
<< "\t"
<< ++a
<<
return 0;
}
its output should be "6 7" and this i what i get:
(arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new2.cpp
new2.cpp: In function 'int main()':
new2.cpp:10: warning: operation on 'a' may be undefined
(arnuld@arch ~ )% ./a.out
7 7
(arnuld@arch ~ )%
Basically the rule in C is that if you have an expression, and somewhere
in that expression, you use an increment operator on a variable, you
can't mention that variable elsewhere in the expression.
If we ignore this, then you got correct values (a is incremented twice,
and then printed)
On Jul 16, 1:31 pm, anon <a...@no.nowrote:
So, this is only one statement:
std::cout << ++a << "\t" << ++a << std::endl;
therefore a is incremented twice before anything is done (ie. something
is printed to standard output).
That means you got the correct output.
and i thought this works like this:
1.) std::cout << ++a << "\t" << ++a << std::endl;
2.) (std::cout << ++a) << "\t" << ++a << std::endl;
3.) (std::cout << ++a) returns std::cout as a result and as a side-
effect it writes the vale of "++a" (== 6) to the the standard output,
which in this case is my terminal. (from C++ Primer 4/e, section
chapter 1, 1.2.2)
4.) (std::cout << "\t") << ++a << std::endl;
same as step 3
5.) (std::cout << ++a) << std::endl;
same as step 3 and this time ++a will be printed (== 7)
6.) std::cout << std::endl;
this will produce a newline and will flush the buffer
where i am wrong ?
I can not back this up with a chapter number from the standard as I do
not have it, but this is how c++ works with prefix increment operator
you can but i trust your information as GCC agrees with you :-)
IMO undefined behavior is something different, not this
ouch!
arnuld wrote:
>On Jul 16, 1:31 pm, anon <a...@no.nowrote:
>So, this is only one statement: std::cout << ++a << "\t" << ++a << std::endl;
>therefore a is incremented twice before anything is done (ie. something is printed to standard output).
>That means you got the correct output.
and i thought this works like this:
1.) std::cout << ++a << "\t" << ++a << std::endl;
2.) (std::cout << ++a) << "\t" << ++a << std::endl;
3.) (std::cout << ++a) returns std::cout as a result and as a side-
effect it writes the vale of "++a" (== 6) to the the standard output,
which in this case is my terminal. (from C++ Primer 4/e, section
chapter 1, 1.2.2)
4.) (std::cout << "\t") << ++a << std::endl;
same as step 3
5.) (std::cout << ++a) << std::endl;
same as step 3 and this time ++a will be printed (== 7)
6.) std::cout << std::endl;
this will produce a newline and will flush the buffer
where i am wrong ?
Steps 3 and 4 are executed simultaneously, that is is incremented twice,
before it is sent to the standard output
>
>I can not back this up with a chapter number from the standard as I do not have it, but this is how c++ works with prefix increment operator
you can but i trust your information as GCC agrees with you :-)
>IMO undefined behavior is something different, not this
ouch!
I tried to cancel this message, but looks like it did not work ;)
And you get that warning only with -Wall. But I would rather change the
code, then remove that parameter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: JKop |
last post by:
Would you classify the following code as "Undefined Behaviour" or as "Non-
portable"?
signed main()
{
signed char chedder = 130;
}
Relevant information:
|
by: Alf P. Steinbach |
last post by:
A few days ago I posted an "Hello, world!" tutorial, discussed in <url:
http://groups.google.no/groups?threadm=41ba4c0a.76869078@news.individual.net>.
As I wrote then:
<quote>
because there...
|
by: sugaray |
last post by:
does the expression int a=printf("%d\n",a); implementation dependent ?
I supposed it would produced the result 2, but i got 4206596 (result
may vary on your machine), i wonder if the value produced...
|
by: Nitin Bhardwaj |
last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a
student and have been doing & studying C programming for the last 4
years).....and also a regular reader of "comp.lang.c"
I...
|
by: Ken Turkowski |
last post by:
The construct
(void*)(((long)ptr + 3) & ~3)
worked well until now to enforce alignment of the pointer to long
boundaries. However, now VC++ warns about it, undoubtedly to help things
work on 64...
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
|
by: markryde |
last post by:
Hello,
I saw in some open source projects a use of "!!" in "C" code;
for example:
in some header file
#define event_pending(v) \
(!!(v)->vcpu_info->evtchn_upcall_pending & \...
|
by: Mr. Ken |
last post by:
Left shift by negative numbers, will I get 1/2?
Thanks.
|
by: Frederick Gotham |
last post by:
I have a general idea of the different kinds of behaviour described by the
C Standard, such as:
(1) Well-defined behaviour:
int a = 2, b = 3;
int c = a + b;
(Jist: The code will work...
|
by: hiteshthappa |
last post by:
hi
Can anyone please help me in finding the total number of words in a
file
I get the newlines, characters and blankspaces correctly but counting
words ia problem.I have tried many ways but it...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |