Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > program output?

Reply
Thread Tools

program output?

 
 
bnp
Guest
Posts: n/a
 
      05-24-2004
Hi,

I took a test on C.
there was an objective question for program output type.
following is the program:

main()
{
char ch;
int i =2;
for (ch=0;ch<128;ch++)
i+=2;
printf("%d",i);
}
i wrote the answer 256 will be printed.
But our instructor told me that it will be infinite loop.
Is that right, I am not sure of the answer.

Regards,

Bhavik
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      05-24-2004
On 24 May 2004 04:20:00 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (bnp) wrote:

>Hi,
>
>I took a test on C.
>there was an objective question for program output type.
>following is the program:
>
>main()
>{
>char ch;
>int i =2;
>for (ch=0;ch<128;ch++)
>i+=2;
>printf("%d",i);
>}
>i wrote the answer 256 will be printed.
>But our instructor told me that it will be infinite loop.
>Is that right, I am not sure of the answer.


Kinda tricky.
First of all, the default "signedness" of the char type is
platform-dependent. If it is 8 bits and "signed", as is the default on
typical PC-based platforms, then this does indeed go into an infinite loop.
In that case, the range of values for an 8-bit char is -128 to +127. It
will always be less than +128.

If the default for chars is unsigned, then the program output is 258 [I
didn't figure it out, I just ran it ]
-leor

>
>Regards,
>
>Bhavik


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
 
 
 
Pieter Droogendijk
Guest
Posts: n/a
 
      05-24-2004
On Mon, 24 May 2004 04:20:00 -0700, bnp wrote:

> Hi,
>
> I took a test on C.
> there was an objective question for program output type.
> following is the program:
>
> main()


int main () or int main (void) is preferred.

> {


Proper indentation is a Good Thing(tm), even for trivial examples such as
this.

> char ch;
> int i =2;
> for (ch=0;ch<128;ch++)
> i+=2;
> printf("%d",i);
> }
> i wrote the answer 256 will be printed.
> But our instructor told me that it will be infinite loop.
> Is that right, I am not sure of the answer.


Your instructor may have been right. It all depends on CHAR_MAX, which may
be 127 if char is signed. If this is the case, ch will never be >= 128,
and the loop will never end. If CHAR_MAX is greater than 127 (because it's
unsigned, or because char has more than 8 value bits), then it may print
258 (and will for sure if you print a newline or flush stdout).

--
int main(void){int putchar(int),i=0;unsigned long t=500555079,n[]={t
,159418370,88921539,286883974,80500161,0};while(n[i])putchar(*(!(t&1
)+!(t||!++i)+"# \n")),(t&&1+(t>>=i-~-i))||(t=n[i]^n[i-1]);return 0;}

 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      05-24-2004

On Mon, 24 May 2004, bnp wrote:
>
> I took a test on C.
> there was an objective question for program output type.
> following is the program:
>
>


Good style dictates the inclusion of <stdio.h> (see errors regarding
'printf' below).

> main()


_int_ main() required for C99; _int_ main(_void_) preferred in both
C89 and C99. (Where _this_ indicates emphasis, not literal underscores.)

> {
> char ch;


Lack of indentation is a bad thing and should be avoided, especially
on Usenet and on school tests. Also, everywhere else.

> int i =2;
> for (ch=0;ch<128;ch++)


Non-strict-conformance: program behavior depends on signedness of
plain char.
Possible undefined behavior: If char is signed and has eight bits,
signed integer overflow will occur.

> i+=2;
> printf("%d",i);


Possible linker error: 'printf' undefined.
Undefined behavior: variadic function 'printf' called without
prototype in scope.
Constraint violation (is that correct?): program output ends
without terminating newline '\n'.

> }
> i wrote the answer 256 will be printed.
> But our instructor told me that it will be infinite loop.
> Is that right, I am not sure of the answer.


Yes, you are both "right." Only the program is wrong.

-Arthur
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-24-2004
Pieter Droogendijk wrote:
> On Mon, 24 May 2004 04:20:00 -0700, bnp wrote:
>
>> I took a test on C.
>> there was an objective question for program output type.
>> following is the program:
>>
>> main()

>
> int main () or int main (void) is preferred.
>
>> {

>
> Proper indentation is a Good Thing(tm), even for trivial examples
> such as this.
>
>> char ch;
>> int i =2;
>> for (ch=0;ch<128;ch++)
>> i+=2;
>> printf("%d",i);
>> }
>> i wrote the answer 256 will be printed.
>> But our instructor told me that it will be infinite loop.
>> Is that right, I am not sure of the answer.

>
> Your instructor may have been right. It all depends on CHAR_MAX,
> which may be 127 if char is signed. If this is the case, ch will
> never be >= 128, and the loop will never end. If CHAR_MAX is
> greater than 127 (because it's unsigned, or because char has
> more than 8 value bits), then it may print 258 (and will for
> sure if you print a newline or flush stdout).


No, in that (signed) case you will overflow ch, and behaviour
becomes undefined. So it may well print "the instructor is full
of it" or do anything else.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


 
Reply With Quote
 
buda
Guest
Posts: n/a
 
      05-24-2004
So overflow is undefined? I thought that the "overflowed bit" gets discarded
and you end up with the "next" number, which in this case might be -128 (if
char is 8 bits signed, then gets to 127 which is 0111 1111, thus adding one
in binary will make it 1000 0000 which translates into -12. I actually
expect this to be "not defined" by the standard, but this is the behavior on
most (if not all) machines I have seen.

"CBFalconer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Pieter Droogendijk wrote:
> > On Mon, 24 May 2004 04:20:00 -0700, bnp wrote:
> >
> >> I took a test on C.
> >> there was an objective question for program output type.
> >> following is the program:
> >>
> >> main()

> >
> > int main () or int main (void) is preferred.
> >
> >> {

> >
> > Proper indentation is a Good Thing(tm), even for trivial examples
> > such as this.
> >
> >> char ch;
> >> int i =2;
> >> for (ch=0;ch<128;ch++)
> >> i+=2;
> >> printf("%d",i);
> >> }
> >> i wrote the answer 256 will be printed.
> >> But our instructor told me that it will be infinite loop.
> >> Is that right, I am not sure of the answer.

> >
> > Your instructor may have been right. It all depends on CHAR_MAX,
> > which may be 127 if char is signed. If this is the case, ch will
> > never be >= 128, and the loop will never end. If CHAR_MAX is
> > greater than 127 (because it's unsigned, or because char has
> > more than 8 value bits), then it may print 258 (and will for
> > sure if you print a newline or flush stdout).

>
> No, in that (signed) case you will overflow ch, and behaviour
> becomes undefined. So it may well print "the instructor is full
> of it" or do anything else.
>
> --
> A: Because it fouls the order in which people normally read text.
> Q: Why is top-posting such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?
>
>



 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      05-24-2004
In <(E-Mail Removed)> Leor Zolman <(E-Mail Removed)> writes:

>On 24 May 2004 04:20:00 -0700, (E-Mail Removed) (bnp) wrote:
>
>>I took a test on C.
>>there was an objective question for program output type.
>>following is the program:
>>
>>main()
>>{
>>char ch;
>>int i =2;
>>for (ch=0;ch<128;ch++)
>>i+=2;
>>printf("%d",i);
>>}
>>i wrote the answer 256 will be printed.
>>But our instructor told me that it will be infinite loop.
>>Is that right, I am not sure of the answer.

>
>Kinda tricky.


Not really: the code invokes undefined behaviour at least once, by calling
printf without a proper declaration in scope, so it is not even worth
analysing it: anything can legally happen.

>First of all, the default "signedness" of the char type is
>platform-dependent. If it is 8 bits and "signed", as is the default on
>typical PC-based platforms, then this does indeed go into an infinite loop.


Can we have a chapter and verse for that? I naively thought that signed
overflow results in undefined behaviour...

>In that case, the range of values for an 8-bit char is -128 to +127.


Can we have a chapter and verse for that? I naively thought that the
standard only guaranteed -127 to 127 and that sign-magnitude and one's
complement implementations are perfectly OK...

>It will always be less than +128.


True, but by the time it makes the transition from 127 to whatever value
(if any), anything can happen... including getting out of the loop.

>If the default for chars is unsigned, then the program output is 258 [I
>didn't figure it out, I just ran it ]


Maybe, maybe not. As the last line of output is not newline terminated,
there is no guarantee that the program will produce any output at all
(it's an implementation-defined feature).

Whoever wrote this test expecting another answer than "undefined
behaviour" should better start learning C...

Here's a properly rewritten version of this test:

Assuming 8-bit bytes, what is the output of the following program?

#include <stdio.h>

int main()
{
unsigned char ch;
unsigned int i = 2;

for (ch = 0; ch < 256; ch++) i += 2;
printf("%u\n", i);
return 0;
}

Note that if i is plain int, signed overflow will occur inside the
infinite loop, and all the bets are off, again.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Ralmin
Guest
Posts: n/a
 
      05-24-2004
"buda" <(E-Mail Removed)> wrote:
> So overflow is undefined? I thought that the "overflowed bit" gets
> discarded and you end up with the "next" number, which in this case
> might be -128 (if char is 8 bits signed, then gets to 127 which is
> 0111 1111, thus adding one in binary will make it 1000 0000 which
> translates into -12. I actually expect this to be "not defined"
> by the standard, but this is the behavior on most (if not all)
> machines I have seen.


Yes, signed integer overflow is undefined behaviour in C. Some machines trap
on overflow, others discard the overflow like you describe. In that case it
depends on whether the signed integer is using 2's complement, 1's
complement or the signed magnitude system.

In 8-bit 2's complement, 1000 0000 is -128
In 8-bit 1's complement, 1000 0000 is -127
In 8-bit signed magnitude, 1000 0000 is negative zero, which might be a
valid result, or might itself be a trap representation.

There might even be more wacky forms of undefined behaviour that occur on
overflow.

--
Simon.


 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      05-24-2004
"Arthur J. O'Dwyer" <(E-Mail Removed)> writes:

> On Mon, 24 May 2004, bnp wrote:
>
>> printf("%d",i);

>
> Constraint violation (is that correct?): program output ends
> without terminating newline '\n'.


Implementation-defined and possibly undefined behavior. See 7.19.2#2:
"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. [...]"

Martin


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
 
Reply With Quote
 
buda
Guest
Posts: n/a
 
      05-24-2004
"Ralmin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) s.com...
> Yes, signed integer overflow is undefined behaviour in C. Some machines

trap
> on overflow, others discard the overflow like you describe. In that case

it
> depends on whether the signed integer is using 2's complement, 1's
> complement or the signed magnitude system.
>
> In 8-bit 2's complement, 1000 0000 is -128
> In 8-bit 1's complement, 1000 0000 is -127
> In 8-bit signed magnitude, 1000 0000 is negative zero, which might be a
> valid result, or might itself be a trap representation.
>
> There might even be more wacky forms of undefined behaviour that occur on
> overflow.
>
> --
> Simon.
>


Thanks for the answer.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to javac a java program w/ another java program which is w/o a main method cjeffwang@yahoo.com Java 1 10-31-2005 04:25 AM
System program/ Application program ?? Parvsandhu Java 2 07-11-2005 09:08 AM
how to convert a java program to an exe program ola Java 3 02-16-2004 09:42 AM
Calling Java program in another Java program Rey Java 4 12-12-2003 10:18 PM
passing data between Java program and C program--help pipi Java 1 07-21-2003 05:02 AM



Advertisments