From: Dan Cross <crossd@gmail.com>
To: caipenghui_c@163.com
Cc: TUHS main list <tuhs@minnie.tuhs.org>
Subject: Re: [TUHS] A c program problem
Date: Wed, 22 Aug 2018 10:21:45 -0400 [thread overview]
Message-ID: <CAEoi9W4wUu6ZKUUVU=i5-ZnGjWAJuP3df7Hrc6v1yKOkf4BmUw@mail.gmail.com> (raw)
In-Reply-To: <4019d825.4b05.16561830adb.Coremail.caipenghui_c@163.com>
[-- Attachment #1: Type: text/plain, Size: 1749 bytes --]
Note, TUHS probably isn't the *best* forum to ask for help with basic C
programs. :-)
That said, I suspect you mis-transcribed the program and that the line,
`sum = sum + 1;` should be `sum = sum + i;`, or more idiomatically, `sum +=
i;`. Indeed, the whole program could be written:
#include <stdio.h>
int
main(void)
{
int i, sum = 0;
for (i = 1; i <= 100; i++)
sum += i;
printf("%d\n", sum);
return 0;
}
If you're using a C99 or later compiler, you can be slightly more succinct:
#include <stdio.h>
int
main(void)
{
int sum = 0;
for (int i = 1; i <= 100; i++)
sum += i;
printf("%d\n", sum);
return 0;
}
Hope that helps!
- Dan C.
On Wed, Aug 22, 2018 at 8:17 AM cc <caipenghui_c@163.com> wrote:
> Hello everyone
>
> I had a problem compiling a piece of c code from the book. The result of
> running the book is 5050, but the compiler is 100. I don't know which is
> right, please help me to see which is wrong. Thank you very much!
>
> #include <stdio.h>
>
> int main(void)
> {
> int i, sum = 0;
> i = 1;
> while ( i <= 100) {
> sum = sum + 1;
> i++;
> }
> printf("%d\n", sum)
> return 0;
> }
>
>
>
> cc
> 邮箱:caipenghui_c@163.com
>
> <https://maas.mail.163.com/dashi-web-extend/html/proSignature.html?ftlId=1&name=cc&uid=caipenghui_c%40163.com&iconUrl=https%3A%2F%2Fmail-online.nosdn.127.net%2Fwzpmmc%2Fad31362036872a624ad455630205a987.jpg&items=%5B%22%E9%82%AE%E7%AE%B1%EF%BC%9Acaipenghui_c%40163.com%22%5D>
>
> 签名由 网易邮箱大师 <https://mail.163.com/dashi/dlpro.html?from=mail88> 定制
>
[-- Attachment #2: Type: text/html, Size: 4661 bytes --]
next prev parent reply other threads:[~2018-08-22 14:22 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-22 12:01 cc
2018-08-22 12:24 ` Donald ODona
2018-08-22 12:54 ` Mark Longridge
2018-08-22 14:21 ` Dan Cross [this message]
2018-08-22 14:26 ` cc
2018-08-22 14:29 ` cc
2018-08-22 14:32 ` Ralph Corderoy
2018-08-22 14:34 ` cc
2018-08-22 15:40 ` Toby Thain
2018-08-22 15:46 ` Caipenghui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAEoi9W4wUu6ZKUUVU=i5-ZnGjWAJuP3df7Hrc6v1yKOkf4BmUw@mail.gmail.com' \
--to=crossd@gmail.com \
--cc=caipenghui_c@163.com \
--cc=tuhs@minnie.tuhs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).