From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: tuhs-bounces@minnie.tuhs.org X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.1 Received: from minnie.tuhs.org (minnie.tuhs.org [45.79.103.53]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 7468b3ec for ; Wed, 22 Aug 2018 14:22:40 +0000 (UTC) Received: by minnie.tuhs.org (Postfix, from userid 112) id BCF91A1A8A; Thu, 23 Aug 2018 00:22:39 +1000 (AEST) Received: from minnie.tuhs.org (localhost [127.0.0.1]) by minnie.tuhs.org (Postfix) with ESMTP id BDA0DA1A85; Thu, 23 Aug 2018 00:22:25 +1000 (AEST) Authentication-Results: minnie.tuhs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=A0GVLEq6; dkim-atps=neutral Received: by minnie.tuhs.org (Postfix, from userid 112) id 5171CA1A85; Thu, 23 Aug 2018 00:22:23 +1000 (AEST) Received: from mail-qt0-f176.google.com (mail-qt0-f176.google.com [209.85.216.176]) by minnie.tuhs.org (Postfix) with ESMTPS id D70D4A1A30 for ; Thu, 23 Aug 2018 00:22:22 +1000 (AEST) Received: by mail-qt0-f176.google.com with SMTP id g44-v6so1537446qtb.12 for ; Wed, 22 Aug 2018 07:22:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=dre/B8hmesMWulilHhNZKlqKyPkyM+wC6Q8o8ixDwEE=; b=A0GVLEq6FqLn/l67K1wBj75KjHtHoqQJteKmC6708m2r/ilDlFd8q1ZM9d01TTvp/0 ykpuPGHGwrIQuoWHGxexNA+zDyareMqObqV2891Q592M9uK3cRB5HxNgMC4HKJdJi7FB bJe+w0ajrIhqOJtevaYLwOoT3Z+dXOfDrQX2wnv3bFzhKpCF2l50v7mye8GRBPASE9GP dUaFXlVrwy6UIiQZ+G9CfiQsrJqOF+RnHXhOS9fVcen+OJKQl4eYXHI+7Ewy2j0UukGH shuSLZENYp8MUIRRxtuAh0ZsUO0ouSgTLCvMdTdkbc7UsVWtbcpXoMHSCRnJ3RWQ5lzB 1Wdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=dre/B8hmesMWulilHhNZKlqKyPkyM+wC6Q8o8ixDwEE=; b=lpkZxadICHfxWhg2uXuRC85zfrOYfLGJcEs+7dMSWnossxdc3cpdwcbWc4EA0xEEZG j5wBvoSVQIcif0shRLVD21D3aYXROwKFOp7WaaxRk5lx2NO7MaWrsxUm5LA/MTtDsob5 3aq/hBoKL3ViUus8p1PDnCy5xRhGzqsIN9pwgfJ0Skel8hyg8yrFSmbDSi5HkoYSbXqj J769Ma+xmAxLgYDyRUEMFC8IXMnJc8XwuTr+hv0CtXN0ofIq1eaZvO0Pyso+VQ+AXnnn WMLcdtu67osckxDGhC83auk6AuV85QcbBqeCU3EOqxJ0UHYx7jncZmd7O1/UvkexLI84 ZvEA== X-Gm-Message-State: AOUpUlExfP/JSUhpibl8enW8jLORLzb6WX0QAM1RPQ22drqPyFfb9ZaT m6fTUAiUIH3bpQ2OGUYc5ysgdW8OhdlI+z7oZzA0UQ== X-Google-Smtp-Source: AA+uWPwqlbRGVDU3Ibd0tDTYHBgav5hayATg9/ySnhjpnIYdd4Md1SN6PgmWjxLr8AoNj4bFhwje7G/LsNDXSZVS6Kw= X-Received: by 2002:ac8:3726:: with SMTP id o35-v6mr23732555qtb.224.1534947741989; Wed, 22 Aug 2018 07:22:21 -0700 (PDT) MIME-Version: 1.0 References: <4019d825.4b05.16561830adb.Coremail.caipenghui_c@163.com> In-Reply-To: <4019d825.4b05.16561830adb.Coremail.caipenghui_c@163.com> From: Dan Cross Date: Wed, 22 Aug 2018 10:21:45 -0400 Message-ID: To: caipenghui_c@163.com Content-Type: multipart/alternative; boundary="000000000000e8ffb4057406deac" Subject: Re: [TUHS] A c program problem X-BeenThere: tuhs@minnie.tuhs.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: The Unix Heritage Society mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: TUHS main list Errors-To: tuhs-bounces@minnie.tuhs.org Sender: "TUHS" --000000000000e8ffb4057406deac Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 =3D sum + 1;` should be `sum =3D sum + i;`, or more idiomatically, `su= m +=3D i;`. Indeed, the whole program could be written: #include int main(void) { int i, sum =3D 0; for (i =3D 1; i <=3D 100; i++) sum +=3D i; printf("%d\n", sum); return 0; } If you're using a C99 or later compiler, you can be slightly more succinct: #include int main(void) { int sum =3D 0; for (int i =3D 1; i <=3D 100; i++) sum +=3D i; printf("%d\n", sum); return 0; } Hope that helps! - Dan C. On Wed, Aug 22, 2018 at 8:17 AM cc 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 > > int main(void) > { > int i, sum =3D 0; > i =3D 1; > while ( i <=3D 100) { > sum =3D sum + 1; > i++; > } > printf("%d\n", sum) > return 0; > } > > > > cc > =E9=82=AE=E7=AE=B1=EF=BC=9Acaipenghui_c@163.com > > > > =E7=AD=BE=E5=90=8D=E7=94=B1 =E7=BD=91=E6=98=93=E9=82=AE=E7=AE=B1=E5=A4=A7= =E5=B8=88 =E5=AE=9A= =E5=88=B6 > --000000000000e8ffb4057406deac Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
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 =3D sum=C2=A0+ 1;` sho= uld be `sum =3D sum=C2=A0+ i;`, or more idiomatically, `sum=C2=A0+=3D i;`.= =C2=A0 Indeed, the whole program could be written:

#include <stdio.h>

int
main(void)=
{
=C2=A0 =C2=A0 =C2=A0 =C2=A0 int i, sum =3D 0;
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 for (i =3D 1; i <=3D 100; i++)
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sum=C2=A0+=3D i;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 printf("%d\n", sum);
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 return 0;
}

If = you're using a C99 or later compiler, you can be slightly more succinct= :

#include <stdio.h>
int
main(void)
{
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 int sum =3D 0;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 for (int i = =3D 1; i <=3D 100; i++)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 sum=C2=A0+=3D i;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 pr= intf("%d\n", sum);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return 0= ;
}

Hope that helps!
=C2=A0 =C2=A0 =C2=A0 =C2=A0- Dan C.

<= br>
On Wed, Aug 22, 2018 at 8:17= AM cc <caipen= ghui_c@163.com> wrote:
=20
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)=
{
=C2=A0 =C2=A0 =C2=A0 =C2=A0int i, sum =3D 0;
=C2=A0 =C2=A0 =C2= =A0 =C2=A0i =3D 1;
=C2=A0 =C2=A0 =C2=A0 =C2=A0while ( i <=3D 100) {=C2=A0 =C2=A0 =C2=A0 =C2=A0sum =3D sum + 1;
=C2=A0 =C2=A0 =C2=A0 =C2= =A0i++;
=C2=A0 =C2=A0 =C2=A0 =C2=A0}
=C2=A0 =C2=A0 =C2=A0 =C2=A0print= f("%d\n", sum)
=C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;
}



--000000000000e8ffb4057406deac--