Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #74791 > unrolled thread
| Started by | jimzhou001@gmail.com |
|---|---|
| First post | 2015-11-03 17:54 -0800 |
| Last post | 2015-11-06 20:07 -0500 |
| Articles | 16 on this page of 36 — 9 participants |
Back to article view | Back to comp.lang.c
Funny C Program jimzhou001@gmail.com - 2015-11-03 17:54 -0800
Re: Funny C Program bartekltg <bartekltg@gmail.com> - 2015-11-04 05:28 +0100
Re: Funny C Program Richard Heathfield <rjh@cpax.org.uk> - 2015-11-04 07:12 +0000
Re: Funny C Program bartekltg <bartekltg@gmail.com> - 2015-11-04 13:25 +0100
Re: Funny C Program bartekltg <bartekltg@gmail.com> - 2015-11-04 13:52 +0100
Re: Funny C Program jimzhou001@gmail.com - 2015-11-04 06:36 -0800
Re: Funny C Program jimzhou001@gmail.com - 2015-11-04 02:28 -0800
Re: Funny C Program bartekltg <bartekltg@gmail.com> - 2015-11-04 13:25 +0100
Re: Funny C Program Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-04 19:07 -0800
Re: Funny C Program bartekltg <bartekltg@gmail.com> - 2015-11-05 09:48 +0100
Re: Funny C Program Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-05 06:08 -0800
Re: Funny C Program Richard Heathfield <rjh@cpax.org.uk> - 2015-11-04 07:04 +0000
Re: Funny C Program jimzhou001@gmail.com - 2015-11-04 02:33 -0800
Re: Funny C Program Richard Heathfield <rjh@cpax.org.uk> - 2015-11-04 10:57 +0000
Re: Funny C Program jimzhou001@gmail.com - 2015-11-04 03:46 -0800
Re: Funny C Program luser droog <luser.droog@gmail.com> - 2015-11-04 10:46 -0800
Re: Funny C Program jimzhou001@gmail.com - 2015-11-04 15:14 -0800
Re: Funny C Program brian.oakley@stonybrook.edu - 2015-11-04 12:37 -0800
Re: Funny C Program jimzhou001@gmail.com - 2015-11-04 15:30 -0800
Funny C Program jimzhou001@gmail.com - 2015-11-06 16:40 -0800
Re: Funny C Program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-07 01:04 +0000
Re: Funny C Program Keith Thompson <kst-u@mib.org> - 2015-11-06 17:12 -0800
Re: Funny C Program jimzhou001@gmail.com - 2015-11-06 18:47 -0800
Re: Funny C Program James Kuyper <jameskuyper@verizon.net> - 2015-11-07 11:24 -0500
Re: Funny C Program jimzhou001@gmail.com - 2015-11-07 16:21 -0800
Re: Funny C Program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-08 00:46 +0000
Re: Funny C Program James Kuyper <jameskuyper@verizon.net> - 2015-11-07 19:56 -0500
Re: Funny C Program jimzhou001@gmail.com - 2015-11-07 19:08 -0800
Re: Funny C Program James Kuyper <jameskuyper@verizon.net> - 2015-11-08 10:58 -0500
Re: Funny C Program James Kuyper <jameskuyper@verizon.net> - 2015-11-12 13:31 -0500
Re: Funny C Program Keith Thompson <kst-u@mib.org> - 2015-11-12 11:59 -0800
Re: Funny C Program jimzhou001@gmail.com - 2015-11-17 23:00 -0800
Re: Funny C Program jimzhou001@gmail.com - 2015-11-18 00:12 -0800
Re: Funny C Program Keith Thompson <kst-u@mib.org> - 2015-11-18 09:01 -0800
Re: Funny C Program jimzhou001@gmail.com - 2015-11-17 23:00 -0800
Re: Funny C Program James Kuyper <jameskuyper@verizon.net> - 2015-11-06 20:07 -0500
Page 2 of 2 — ← Prev page 1 [2]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-11-07 01:04 +0000 |
| Message-ID | <87ziyqtybf.fsf@bsb.me.uk> |
| In reply to | #75200 |
jimzhou001@gmail.com writes:
> Actually I have another question,"Is 'int monk[101]={0};' really
> legal?
Yes, it's fine.
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-11-06 17:12 -0800 |
| Message-ID | <lnr3k21ujq.fsf@kst-u.example.com> |
| In reply to | #75201 |
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> jimzhou001@gmail.com writes:
>> Actually I have another question,"Is 'int monk[101]={0};' really
>> legal?
>
> Yes, it's fine.
>
> <snip>
To expand on that: If an initializer doesn't specify values for all
elements of the object being initialized, the unspecified elements are
initialized to zero. (That's 0 for integers, 0.0 for floating-point,
NULL for pointers.)
{0} is a valid initializer for any type, and will recursively set every
element to zero. (It would have been nice to be able to write {} and
let *all* the elements be set implicitly, but the language doesn't allow
it.)
Some compilers, including older versions of gcc, might warn about
missing initializers.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | jimzhou001@gmail.com |
|---|---|
| Date | 2015-11-06 18:47 -0800 |
| Message-ID | <4f00f36b-8b20-49e1-ad96-0a782787ac05@googlegroups.com> |
| In reply to | #75203 |
Oh,I asked this question because I got into trouble not initializing.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-11-07 11:24 -0500 |
| Message-ID | <563E25B0.8080504@verizon.net> |
| In reply to | #75207 |
On 11/06/2015 09:47 PM, jimzhou001@gmail.com wrote: > Oh,I asked this question because I got into trouble not initializing. If you provide even a single initializer for a array or struct, all of the other elements of the array, or the other members of the struct, get zero-initialized. However, if you provide no initializers, then unless the object has static storage duration, all of the elements of the array or the members of the struct have indeterminate values. It is never a good idea to make any use of an indeterminate value - in particular, an object whose value is indeterminate is allowed to be in a trap representation - attempting to read the value of an object containing a trap representation has undefined behavior.
[toc] | [prev] | [next] | [standalone]
| From | jimzhou001@gmail.com |
|---|---|
| Date | 2015-11-07 16:21 -0800 |
| Message-ID | <68f7c46c-0248-4dcd-8073-46a938d5e427@googlegroups.com> |
| In reply to | #75267 |
I'm sorry.Maybe I didn't say clearly.I met the barrier when trying to initialize the array with "int monk[101]={0}".You can see more details at http://jimzhou001.blogspot.de/2015/11/funny-c-programcomparison-between.html?m=1
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-11-08 00:46 +0000 |
| Message-ID | <87wptts4gf.fsf@bsb.me.uk> |
| In reply to | #75317 |
jimzhou001@gmail.com writes:
> I'm sorry.Maybe I didn't say clearly.I met the barrier when trying to
> initialize the array with "int monk[101]={0}".You can see more details
> at
> http://jimzhou001.blogspot.de/2015/11/funny-c-programcomparison-between.html?m=1
You should post your code here. I doubt many people want to read a blog
just to answer a question here.
If int monk[101]={0}; does not work, you need to say why you think it
does not work. Maybe you are doing something else in the program that
is messing things up.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-11-07 19:56 -0500 |
| Message-ID | <563E9DA5.70603@verizon.net> |
| In reply to | #75317 |
On 11/07/2015 07:21 PM, jimzhou001@gmail.com wrote:
> I'm sorry.Maybe I didn't say clearly.I met the barrier when trying to
> initialize the array with "int monk[101]={0}".You can see more
> details at
> http://jimzhou001.blogspot.de/2015/11/funny-c-programcomparison-between.html?m=1
Yes - saying " I got into trouble not initializing." is not a good way
of explaining that you got into trouble while initializing.
Unless I'm missing something (which is unfortunately entirely possible),
the only possible problem with your code is that it doesn't terminate
the output with a new-line character (7.21.2p2), but it's unusual for
anything to go wrong for that reason, and quite bizarre that it would go
wrong in this fashion.
Could you identify precisely which compiler you used, and what options
you selected when using it? That might give a clue.
[toc] | [prev] | [next] | [standalone]
| From | jimzhou001@gmail.com |
|---|---|
| Date | 2015-11-07 19:08 -0800 |
| Message-ID | <d5f3dfa0-e91e-43bd-8489-cecea8218c70@googlegroups.com> |
| In reply to | #75325 |
Thank you.I uses the compiler C4droid v4.99 on my Android phone.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-11-08 10:58 -0500 |
| Message-ID | <563F7132.4010804@verizon.net> |
| In reply to | #75325 |
I'm sending this message to n0n3m4@gmail.com, listed as the contact
address for questions about C4droid, and also to the comp.lang.c usenet
newsgroup. Unless n0n3m4 has a usenet connection, his response will
probably not go to the group - but if he gives me permission, I will
repost it there.
A message was posted on comp.lang.c about the legality/meaning of some
code that produced unexpected results when compiled using C4droid. You
can find the full discussion at
<https://groups.google.com/forum/#!topic/comp.lang.c/DABEmqReVJQ>, but
it's not until the following message that the connection to C4droid was
revealed - it's mentioned on the web page referred to in the link.
On 11/07/2015 07:56 PM, James Kuyper wrote:
> On 11/07/2015 07:21 PM, jimzhou001@gmail.com wrote:
>> I'm sorry.Maybe I didn't say clearly.I met the barrier when trying to
>> initialize the array with "int monk[101]={0}".You can see more
>> details at
>> http://jimzhou001.blogspot.de/2015/11/funny-c-programcomparison-between.html?m=1
>
> Yes - saying " I got into trouble not initializing." is not a good way
> of explaining that you got into trouble while initializing.
>
> Unless I'm missing something (which is unfortunately entirely possible),
> the only possible problem with your code is that it doesn't terminate
> the output with a new-line character (7.21.2p2), but it's unusual for
> anything to go wrong for that reason, and quite bizarre that it would go
> wrong in this fashion.
In case you're unwilling to trust a link to an unknown web page, here's
the relevant details from the page it refers to. jimzhou001 passed the
following code to C4droid:
include <stdio.h>
int main(void)
{
int i,array[101]={0};
for(i=1;i<101;i++)
printf(" %d",array[i]);
return 0;
}
(I added some white space not present in the original program, to aid
readability)
As I understand it, the output from that program should consist of 100
repeats of " 0". jimzhou001 reports the output from that program using a
screen shot, so I can't cut and paste it here, but the output contains
100 numbers, each preceded by a single space, with the first 60 numbers
and last 4 numbers all 0. There's a long string of non-zero values
starting at the 61st position with "1 -1092757696 -1092757628 78752 19 ...".
Can you confirm and explain the behavior of this program when compiled
by C4droid?
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-11-12 13:31 -0500 |
| Message-ID | <n22lpe$749$1@dont-email.me> |
| In reply to | #75342 |
On 11/08/2015 10:58 AM, James Kuyper wrote:
> I'm sending this message to n0n3m4@gmail.com, listed as the contact
> address for questions about C4droid, and also to the comp.lang.c usenet
> newsgroup. Unless n0n3m4 has a usenet connection, his response will
> probably not go to the group - but if he gives me permission, I will
> repost it there.
>
> A message was posted on comp.lang.c about the legality/meaning of some
> code that produced unexpected results when compiled using C4droid. You
> can find the full discussion at
> <https://groups.google.com/forum/#!topic/comp.lang.c/DABEmqReVJQ>, but
> it's not until the following message that the connection to C4droid was
> revealed - it's mentioned on the web page referred to in the link.
>
> On 11/07/2015 07:56 PM, James Kuyper wrote:
>> On 11/07/2015 07:21 PM, jimzhou001@gmail.com wrote:
>>> I'm sorry.Maybe I didn't say clearly.I met the barrier when trying to
>>> initialize the array with "int monk[101]={0}".You can see more
>>> details at
>>> http://jimzhou001.blogspot.de/2015/11/funny-c-programcomparison-between.html?m=1
>>
>> Yes - saying " I got into trouble not initializing." is not a good way
>> of explaining that you got into trouble while initializing.
>>
>> Unless I'm missing something (which is unfortunately entirely possible),
>> the only possible problem with your code is that it doesn't terminate
>> the output with a new-line character (7.21.2p2), but it's unusual for
>> anything to go wrong for that reason, and quite bizarre that it would go
>> wrong in this fashion.
>
> In case you're unwilling to trust a link to an unknown web page, here's
> the relevant details from the page it refers to. jimzhou001 passed the
> following code to C4droid:
>
> include <stdio.h>
> int main(void)
> {
> int i,array[101]={0};
> for(i=1;i<101;i++)
> printf(" %d",array[i]);
> return 0;
> }
>
> (I added some white space not present in the original program, to aid
> readability)
>
> As I understand it, the output from that program should consist of 100
> repeats of " 0". jimzhou001 reports the output from that program using a
> screen shot, so I can't cut and paste it here, but the output contains
> 100 numbers, each preceded by a single space, with the first 60 numbers
> and last 4 numbers all 0. There's a long string of non-zero values
> starting at the 61st position with "1 -1092757696 -1092757628 78752 19 ...".
>
> Can you confirm and explain the behavior of this program when compiled
> by C4droid?
I got a response from Roman Lebedev (n0n3m4@gmail.com). Since he did not
give me explicit permission to repost his response, I won't, but he
didn't say anything particularly private. He basically passed
responsibility for this problem over to the TCC compiler used by
C4droid. He said nothing to suggest that he'd performed any kind of
detailed investigation to find out what was going wrong.
Does anyone have a copy of TCC that is not part of C4droid, that they
can test this code with? If so, and if you can duplicate the problem, I
suppose a bug report can be passed on to the people responsible for TCC.
--
James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-11-12 11:59 -0800 |
| Message-ID | <ln37wbq98h.fsf@kst-u.example.com> |
| In reply to | #75858 |
James Kuyper <jameskuyper@verizon.net> writes:
> On 11/08/2015 10:58 AM, James Kuyper wrote:
[...]
>>> Unless I'm missing something (which is unfortunately entirely possible),
>>> the only possible problem with your code is that it doesn't terminate
>>> the output with a new-line character (7.21.2p2), but it's unusual for
>>> anything to go wrong for that reason, and quite bizarre that it would go
>>> wrong in this fashion.
>>
>> In case you're unwilling to trust a link to an unknown web page, here's
>> the relevant details from the page it refers to. jimzhou001 passed the
>> following code to C4droid:
>>
>> include <stdio.h>
>> int main(void)
>> {
>> int i,array[101]={0};
>> for(i=1;i<101;i++)
>> printf(" %d",array[i]);
>> return 0;
>> }
>>
>> (I added some white space not present in the original program, to aid
>> readability)
>>
>> As I understand it, the output from that program should consist of 100
>> repeats of " 0". jimzhou001 reports the output from that program using a
>> screen shot, so I can't cut and paste it here, but the output contains
>> 100 numbers, each preceded by a single space, with the first 60 numbers
>> and last 4 numbers all 0. There's a long string of non-zero values
>> starting at the 61st position with "1 -1092757696 -1092757628 78752 19 ...".
>>
>> Can you confirm and explain the behavior of this program when compiled
>> by C4droid?
>
> I got a response from Roman Lebedev (n0n3m4@gmail.com). Since he did not
> give me explicit permission to repost his response, I won't, but he
> didn't say anything particularly private. He basically passed
> responsibility for this problem over to the TCC compiler used by
> C4droid. He said nothing to suggest that he'd performed any kind of
> detailed investigation to find out what was going wrong.
>
> Does anyone have a copy of TCC that is not part of C4droid, that they
> can test this code with? If so, and if you can duplicate the problem, I
> suppose a bug report can be passed on to the people responsible for TCC.
I just compiled and ran that program using tcc version 0.9.25 on Linux
Mint 17.2, x86_64. It produced the correct output, 100 occurrences of
" 0".
It could still be a bug that depends on the target system, on the
compiler version, or just on bad luck (if the {0} initializer doesn't
work correctly, the contents of `array` might or might not be all zeros
just by chance). I don't see an option to generate an assembly listing,
but by fiddling around with a few things it does seem likely that the
compiler is generating code to zero the array.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | jimzhou001@gmail.com |
|---|---|
| Date | 2015-11-17 23:00 -0800 |
| Message-ID | <ee0ab6c6-9c07-4346-8907-cefed14ffc48@googlegroups.com> |
| In reply to | #75870 |
I actually used tcc compiler before.When I used gcc,the final problem disappeared.But there were some other terrible experiences for me about the following C++ program.
//
#include <iostream>
int main(void)
{
using namespace std;
int inchheight,foot,inch;
const int ConversionFactor=12;
cout<<"Input your height in inches(only integer allowed):__\b\b";
cin>>inchheight;
foot = inchheight / ConversionFactor;
inch = inchheight % ConversionFactor;
cout<<"You are "<<foot<<" feet(foot) "<<inch <<" inch(es) high."<<endl;
return 0;
}
I run it successfully a few days ago,but failed today!According to the errors showed on the screen,which I am unable to show here,the compiler seems not to recognize "iostream" or "cout".I even want to change a compiler.Which one do you recommend on Android phone?
[toc] | [prev] | [next] | [standalone]
| From | jimzhou001@gmail.com |
|---|---|
| Date | 2015-11-18 00:12 -0800 |
| Message-ID | <33f58a20-83b8-4c71-b2a9-7879d6df52d2@googlegroups.com> |
| In reply to | #76365 |
The errors screenshots are here:http://jimzhou001.blogspot.co.uk/2015/11/whats-wrong-with-c4droid.html
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-11-18 09:01 -0800 |
| Message-ID | <ln1tbngs20.fsf@kst-u.example.com> |
| In reply to | #76365 |
jimzhou001@gmail.com writes:
> I actually used tcc compiler before.When I used gcc,the final problem
> disappeared.But there were some other terrible experiences for me
> about the following C++ program.
>
> //
> #include <iostream>
> int main(void)
> {
> using namespace std;
[snip]
You need to use a C++ compiler -- and if you have questions about the
code, you need to ask them in a C++ newsgroup.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | jimzhou001@gmail.com |
|---|---|
| Date | 2015-11-17 23:00 -0800 |
| Message-ID | <a6444d50-e38d-4973-9cdf-588270005751@googlegroups.com> |
| In reply to | #75870 |
I actually used tcc compiler before.When I used gcc,the final problem disappeared.But there were some other terrible experiences for me about the following C++ program.
//
#include <iostream>
int main(void)
{
using namespace std;
int inchheight,foot,inch;
const int ConversionFactor=12;
cout<<"Input your height in inches(only integer allowed):__\b\b";
cin>>inchheight;
foot = inchheight / ConversionFactor;
inch = inchheight % ConversionFactor;
cout<<"You are "<<foot<<" feet(foot) "<<inch <<" inch(es) high."<<endl;
return 0;
}
I run it successfully a few days ago,but failed today!According to the errors showed on the screen,which I am unable to show here,the compiler seems not to recognize "iostream" or "cout".I even want to change a compiler.Which one do you recommend on Android phone?
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-11-06 20:07 -0500 |
| Message-ID | <563D4EE4.1030209@verizon.net> |
| In reply to | #75200 |
On 11/06/2015 07:40 PM, jimzhou001@gmail.com wrote:
> Actually I have another question,"Is 'int monk[101]={0};' really legal?Or is it necessary for us to initialize the array by a loop?"I did meet the problem.Anyone can explain it?Thanks.If interested,follow the link for this question:http://jimzhou001.blogspot.de/2015/11/funny-c-programzhishen-lu-eating_78.html
No, a loop is not necessary. For any object of aggregate type (array or
struct - 6.2.5p21), you can provide an initializer that explicitly
initializes only part of the object (6.7.9p21). If you do so, then all
of the parts not explicitly initialized will be handled the same as
objects with static storage duration that have no explicit initializer:
they are set to zero, if they have arithmetic type, or to a null
pointer, if they have a pointer type (6.7.9p10). This also applies if
you initialize a character array with a string literal that is shorter
than the array:
char long_array[80] = "short string";
long_array[i] == '\0' for all values of 'i' from 12 to 79.
In C90, the only way to provide an incomplete initializer was to provide
an initializer for the first N elements of an array, or the first N
members of a struct. In C99, designated initializers were added
(6.7.9p6,7), which allow sparse initialization. The declarations:
int monk[101]={[50]=-1;};
struct tm then = {.tm_year = 50;};
are essentially equivalent to:
int monk[101] = {0};
monk[50] = -1;
struct tm middle = {0};
middle.tm_year = 50;
with one key exception: if either 'monk' or 'middle' had been declared
'const', the assignment statements above would be constraint violations,
but the corresponding designated initializers are not.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c
csiph-web