431,852 Members | 2,152 Online
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

# explanation for this output

 P: n/a How the following code is working. main() { int a = 38, b = 13; unsigned long long c; c = a * (1<
19 Replies

 P: n/a On 20 Jun 2007, deepak

 P: n/a In article <11**********************@x35g2000prf.googlegroups .com>, deepak How the following code is working. >main() { int a = 38, b = 13; unsigned long long c; c = a * (1<The output of this code is not 9961472000 and it is 1371537408. >How this converting in to this number? Is it because of the registersin the processor? The fact that you assign the output to a long long variable does not mean that the expression will be evaluated in long long. Instead, because the variables inolved are all int, the value will be evaluated using int arithmetic, will encounter UB because of the arithmetic overflow of int [on most systems], and whatever comes out will be widened to the long long c. Try c = (long long) a * (1LL<

 P: n/a In article , David Tiktin On 20 Jun 2007, deepak int a = 38, b = 13; > c = a * (1<Try it with 3200LL. No, then a * (1<

 P: n/a On Jun 20, 10:18 am, deepak int main(void) { const unsigned long long a = 38, b = 13; unsigned long long c; c = a * (1ULL<t 9961472000 */ Jun 20 '07 #5

 P: n/a "user923005" int main(void) { const unsigned long long a = 38, b = 13; unsigned long long c; c = a * (1ULL<t 9961472000 */ Jun 20 '07 #6

 P: n/a deepak wrote: How the following code is working. main() { int a = 38, b = 13; unsigned long long c; c = a * (1< int main(void) { int a = 38, b = 13; unsigned long long c; printf("[Output]\n"); c = (long long unsigned) a *(1u << b) * 32000; printf("a converted to long long unsigned, c = %llu\n", c); c = a * (1LLu << b) * 32000; printf("shifting a long long unsigned, c=%llu\n", c); c = a * (1u << b) * 32000LLu; printf("using long long unsigned 32000, c = %llu\n", c); return 0; } [Output] a converted to long long unsigned, c = 9961472000 shifting a long long unsigned, c=9961472000 using long long unsigned 32000, c = 9961472000 Note the needed

 P: n/a deepak wrote: How the following code is working. main() { int a = 38, b = 13; unsigned long long c; c = a * (1<

 P: n/a On Jun 20, 12:28 pm, "Army1987" int main(void) { const unsigned long long a = 38, b = 13; unsigned long long c; c = a * (1ULL< int fclose(FILE *stream); Description 2 A successful call to the fclose function causes the stream pointed to by stream to be flushed and the associated file to be closed. Any unwritten buffered data for the stream are delivered to the host environment to be written to the file; any unread buffered data are discarded. Whether or not the call succeeds, the stream is disassociated from the file and any buffer set by the setbuf or setvbuf function is disassociated from the stream (and deallocated if it was automatically allocated). Returns 3 The fclose function returns zero if the stream was successfully closed, or EOF if any errors were detected." "5.1.2.2.3 Program termination 1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible" "7.20.4.3 The exit function Synopsis 1 #include void exit(int status); Description 2 The exit function causes normal program termination to occur. If more than one call to the exit function is executed by a program, the behavior is undefined. 314 Library §7.20.4.3 ©ISO/IEC ISO/IEC 9899:1999 (E) 3 First, all functions registered by the atexit function are called, in the reverse order of their registration,253) except that a function is called after any previously registered functions that had already been called at the time it was registered. If, during the call to any such function, a call to the longjmp function is made that would terminate the call to the registered function, the behavior is undefined. 4 Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed. 5 Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation- defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined. Returns 6 The exit function cannot return to its caller. 7.20.4.4 The _Exit function Synopsis 1 #include void _Exit(int status); Description 2 The _Exit function causes normal program termination to occur and control to be returned to the host environment. No functions registered by the atexit function or signal handlers registered by the signal function are called. The status returned to the host environment is determined in the same way as for the exit function (7.20.4.3). Whether open streams with unwritten buffered data are flushed, open streams are closed, or temporary files are removed is implementation-defined. Returns 3 The _Exit function cannot return to its caller. 253) Each function is called as many times as it was registered, and in the correct order with respect to other registered functions." I think it's pretty plain. return 0; } /* C:\tmp>t 9961472000 */- Hide quoted text - - Show quoted text - Jun 20 '07 #9

 P: n/a user923005 said: On Jun 20, 12:28 pm, "Army1987" "user923005" >/* After removing the outrageous errors: */ #include int main(void) { const unsigned long long a = 38, b = 13; unsigned long long c; c = a * (1ULL<

 P: n/a On Thu, 21 Jun 2007 00:20:25 +0200, Sjouke Burry deepak wrote: >How the following code is working.main() { int a = 38, b = 13; unsigned long long c; c = a * (1<

 P: n/a On Jun 20, 10:26 pm, David Tiktin wrote: On 20 Jun 2007, deepak Try it with 3200LL. Dave -- D.a.v.i.d T.i.k.t.i.n t.i.k.t.i.n [at] a.d.v.a.n.c.e.d.r.e.l.a.y [dot] c.o.m- Hide quoted text - - Show quoted text - Jun 21 '07 #12

 P: n/a deepak wrote: > How the following code is working. main() { int a = 38, b = 13; unsigned long long c; c = a * (1< cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com Jun 21 '07 #13

 P: n/a Barry Schwarz wrote: >How the following code is working.main() { int a = 38, b = 13; unsigned long long c; c = a * (1< cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com Jun 21 '07 #14

 P: n/a CBFalconer deepak wrote:How the following code is working.main() { int a = 38, b = 13; unsigned long long c; c = a * (1< San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 21 '07 #15

 P: n/a On Jun 20, 10:18 pm, deepak printf("%llu", c); } The output of this code is not 9961472000 and it is 1371537408. How this converting in to this number? Is it because of the registers in the processor? Jun 21 '07 #16

 P: n/a On Wed, 20 Jun 2007 23:25:52 -0700, deepak wrote: >On Jun 20, 10:18 pm, deepak How the following code is working.main() { int a = 38, b = 13; unsigned long long c; c = a * (1< >> printf("%llu", c);}The output of this code is not 9961472000 and it is 1371537408.How this converting in to this number? Is it because of the registersin the processor? Remove del for email Jun 21 '07 #17

 P: n/a deepak How the following code is working.main() { int a = 38, b = 13; unsigned long long c; c = a * (1< San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 21 '07 #18

 P: n/a "user923005" On Jun 20, 12:28 pm, "Army1987" "user923005" int main(void) { const unsigned long long a = 38, b = 13; unsigned long long c; c = a * (1ULL< int fclose(FILE *stream); Description 2 A successful call to the fclose function causes the stream pointed to by stream to be flushed and the associated file to be closed. Any unwritten buffered data for the stream are delivered to the host environment to be written to the file; any unread buffered data are discarded. Whether or not the call succeeds, the stream is disassociated from the file and any buffer set by the setbuf or setvbuf function is disassociated from the stream (and deallocated if it was automatically allocated). Returns 3 The fclose function returns zero if the stream was successfully closed, or EOF if any errors were detected." "5.1.2.2.3 Program termination 1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible" "7.20.4.3 The exit function Synopsis 1 #include void exit(int status); Description 2 The exit function causes normal program termination to occur. If more than one call to the exit function is executed by a program, the behavior is undefined. 314 Library §7.20.4.3 ©ISO/IEC ISO/IEC 9899:1999 (E) 3 First, all functions registered by the atexit function are called, in the reverse order of their registration,253) except that a function is called after any previously registered functions that had already been called at the time it was registered. If, during the call to any such function, a call to the longjmp function is made that would terminate the call to the registered function, the behavior is undefined. 4 Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed. 5 Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation- defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined. Returns 6 The exit function cannot return to its caller. 7.20.4.4 The _Exit function Synopsis 1 #include void _Exit(int status); Description 2 The _Exit function causes normal program termination to occur and control to be returned to the host environment. No functions registered by the atexit function or signal handlers registered by the signal function are called. The status returned to the host environment is determined in the same way as for the exit function (7.20.4.3). Whether open streams with unwritten buffered data are flushed, open streams are closed, or temporary files are removed is implementation-defined. Returns 3 The _Exit function cannot return to its caller. 253) Each function is called as many times as it was registered, and in the correct order with respect to other registered functions." I think it's pretty plain. Where on Earth does it say that return 0; will automatically add a newline to stdout for you? 7.19.2 p2: A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. If you ran that program on one of the Unix-like systems I've used, you would get the prompt on the same line of the output, which does not look very good. return 0; } /* C:\tmp>t 9961472000 */- Hide quoted text - - Show quoted text - Jun 21 '07 #19

 P: n/a Keith Thompson wrote: > .... snip ... > How so? (Note that it's 1ll, equivalent to 1LL, not 111.) 1ll is of type long long, so 1ll< cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com Jun 21 '07 #20

### This discussion thread is closed

Replies have been disabled for this discussion.