Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #12449 > unrolled thread

I am learning C: a little problem with a simple source code

Started byzemir <zemir_fp@libero.it>
First post2011-11-01 08:27 -0700
Last post2011-11-02 06:11 -0700
Articles 20 on this page of 146 — 41 participants

Back to article view | Back to comp.lang.c


Contents

  I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 08:27 -0700
    Re: I am learning C: a little problem with a simple source code Mark Bluemel <mark_bluemel@pobox.com> - 2011-11-01 15:49 +0000
    Re: I am learning C: a little problem with a simple source code nroberts <roberts.noah@gmail.com> - 2011-11-01 08:52 -0700
    Re: I am learning C: a little problem with a simple source code jt@toerring.de (Jens Thoms Toerring) - 2011-11-01 16:22 +0000
    Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-01 09:31 -0700
      Re: I am learning C: a little problem with a simple source code Ike Naar <ike@sverige.freeshell.org> - 2011-11-01 16:57 +0000
        Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 11:51 -0700
          Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-01 15:04 -0400
            Re: I am learning C: a little problem with a simple source code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-01 20:21 +0000
              Re: I am learning C: a little problem with a simple source code jacob navia <jacob@spamsink.net> - 2011-11-01 21:41 +0100
          Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-02 08:06 +1300
            Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:17 -0700
              Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-01 16:24 -0400
                Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:27 -0700
                  Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2011-11-01 20:24 -0700
                    Re: I am learning C: a little problem with a simple source code gazelle@shell.xmission.com (Kenny McCormack) - 2011-11-02 10:24 +0000
                      Re: I am learning C: a little problem with a simple source code "BartC" <bc@freeuk.com> - 2011-11-02 11:04 +0000
                        Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-02 07:41 -0400
                        Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-02 18:00 +0000
                        Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2011-11-02 14:06 -0700
                          Re: I am learning C: a little problem with a simple source code John Bode <jfbode1029@gmail.com> - 2011-11-03 15:05 -0700
                          Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 22:55 -0800
                            Re: I am learning C: a little problem with a simple source code jgk@panix.com (Joe keane) - 2012-01-26 15:59 +0000
                              Re: I am learning C: a little problem with a simple source code Fritz Wuehler <fritz@spamexpire-201201.rodent.frell.theremailer.net> - 2012-01-27 20:28 +0100
                                Re: I am learning C: a little problem with a simple source code Markus Wichmann <nullplan@gmx.net> - 2012-01-28 14:11 +0100
                                  Re: I am learning C: a little problem with a simple source code Shao Miller <sha0.miller@gmail.com> - 2012-01-29 09:34 -0500
                                    Re: I am learning C: a little problem with a simple source code Markus Wichmann <nullplan@gmx.net> - 2012-01-30 13:38 +0100
                                      Re: I am learning C: a little problem with a simple source code Shao Miller <sha0.miller@gmail.com> - 2012-01-30 12:10 -0500
                                Re: I am learning C: a little problem with a simple source code Zoltan Kocsi <zoltan@bendor.com.au> - 2012-01-29 08:55 +1100
                                Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 21:48 -0800
                              Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 21:46 -0800
                                Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-01-31 22:30 -0800
                                  Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 10:11 -0800
                                    Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-08 13:51 -0800
                                      Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-09 12:51 -0800
                                        Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-09 13:36 -0800
                                          Re: I am learning C: a little problem with a simple source code jacob navia <jacob@spamsink.net> - 2012-03-09 23:50 +0100
                                          Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-09 15:01 -0800
                                            Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-09 16:21 -0800
                                              Re: I am learning C: a little problem with a simple source code gazelle@shell.xmission.com (Kenny McCormack) - 2012-03-10 00:57 +0000
                                                Re: I am learning C: a little problem with a simple source code jacob navia <jacob@spamsink.net> - 2012-03-10 13:37 +0100
                                                  Re: I am learning C: a little problem with a simple source code gazelle@shell.xmission.com (Kenny McCormack) - 2012-03-10 13:58 +0000
                                              Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-10 12:24 -0800
                                                Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-10 14:16 -0800
                                                  Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-11 12:50 -0700
                                          Re: I am learning C: a little problem with a simple source code Philip Lantz <prl@canterey.us> - 2012-03-11 00:57 -0800
                                            Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-11 03:20 -0700
                                              Re: I am learning C: a little problem with a simple source code Philip Lantz <prl@canterey.us> - 2012-03-12 02:10 -0700
                                                Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-12 10:49 -0700
                                                  Re: I am learning C: a little problem with a simple source code Philip Lantz <prl@canterey.us> - 2012-03-14 01:16 -0700
                                            Re: I am learning C: a little problem with a simple source code Robert Miles <milesrf@Usenet-News.net> - 2012-04-15 06:53 -0500
                                              Re: I am learning C: a little problem with a simple source code Nomen Nescio <nobody@dizum.com> - 2012-04-15 19:45 +0200
                                              Re: I am learning C: a little problem with a simple source code Philip Lantz <prl@canterey.us> - 2012-04-15 15:06 -0700
                                Re: I am learning C: a little problem with a simple source code 88888 Dihedral <dihedral88888@googlemail.com> - 2012-04-16 10:56 -0700
                        Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-03 10:19 +1300
                          Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-02 21:10 +0000
                          Re: I am learning C: a little problem with a simple source code Kaz Kylheku <kaz@kylheku.com> - 2011-11-03 05:13 +0000
                Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:29 -0700
              Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-01 21:44 +0000
                Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 06:33 -0700
                  Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-03 07:05 -0700
                    Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 15:32 -0700
                      Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-04 01:44 -0700
                        Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 07:30 -0700
                          Re: I am learning C: a little problem with a simple source code John Gordon <gordon@panix.com> - 2011-11-04 16:03 +0000
                            Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 09:20 -0700
                              Re: I am learning C: a little problem with a simple source code John Gordon <gordon@panix.com> - 2011-11-04 16:45 +0000
                                Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 11:59 -0700
                                  Re: I am learning C: a little problem with a simple source code John Gordon <gordon@panix.com> - 2011-11-04 19:32 +0000
                                  Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-05 04:07 -0700
                              Re: I am learning C: a little problem with a simple source code John Gordon <gordon@panix.com> - 2011-11-04 16:54 +0000
                                Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 11:58 -0700
                            Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 18:25 +0100
                          Re: I am learning C: a little problem with a simple source code gordonb.e1fpi@burditt.org (Gordon Burditt) - 2011-11-04 13:50 -0500
                            Re: I am learning C: a little problem with a simple source code Pasquale Frega <zemir_fp@libero.it> - 2011-11-04 11:57 -0700
                              Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2011-11-04 14:44 -0700
                            Re: I am learning C: a little problem with a simple source code "osmium" <r124c4u102@comcast.net> - 2011-11-04 14:03 -0500
                            Re: I am learning C: a little problem with a simple source code Kaz Kylheku <kaz@kylheku.com> - 2011-11-04 21:37 +0000
                              Re: I am learning C: a little problem with a simple source code Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-06 12:20 +0200
                      Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-04 08:32 -0700
                        Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 09:04 -0700
                          Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-04 10:28 -0700
                  Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-03 18:35 +0000
                    Re: I am learning C: a little problem with a simple source code Pasquale Frega <zemir_fp@libero.it> - 2011-11-03 14:50 -0700
                      Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2011-11-03 15:59 -0700
                        Re: I am learning C: a little problem with a simple source code Pasquale Frega <zemir_fp@libero.it> - 2011-11-04 12:00 -0700
                        Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 20:58 +0100
                          Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 21:13 +0100
                      Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-04 01:56 -0700
                        Re: I am learning C: a little problem with a simple source code Pasquale Frega <zemir_fp@libero.it> - 2011-11-04 12:12 -0700
                          Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-05 10:09 +1300
                    Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 15:32 -0700
                      Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-03 22:53 +0000
                        Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 17:57 -0700
                          Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-03 21:35 -0500
                            Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-04 03:31 +0000
                          Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-04 02:25 -0700
                    Re: I am learning C: a little problem with a simple source code Kulin Remailer <remailer@reece.net.au> - 2011-11-03 22:47 +0000
                      Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 16:14 -0700
                    Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-03 21:55 -0500
                    Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-04 08:10 -0700
                  Re: I am learning C: a little problem with a simple source code ImpalerCore <jadill33@gmail.com> - 2011-11-04 14:21 -0700
                    Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-04 16:31 -0500
                    Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-05 10:41 +1300
                    Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-04 21:38 +0000
                      Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-04 22:18 -0500
                        Re: I am learning C: a little problem with a simple source code Lew Pitcher <lpitcher@teksavvy.com> - 2011-11-05 11:11 -0400
                          Re: I am learning C: a little problem with a simple source code Lew Pitcher <lpitcher@teksavvy.com> - 2011-11-05 11:13 -0400
                            Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-05 23:35 -0500
                        Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-06 07:40 +0000
                    Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-04 18:15 -0400
                      Re: I am learning C: a little problem with a simple source code ImpalerCore <jadill33@gmail.com> - 2011-11-04 20:00 -0700
                        Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-04 23:19 -0400
                          Re: I am learning C: a little problem with a simple source code ImpalerCore <jadill33@gmail.com> - 2011-11-04 20:33 -0700
                        Re: I am learning C: a little problem with a simple source code Kaz Kylheku <kaz@kylheku.com> - 2011-11-05 03:52 +0000
                          Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-05 17:01 +1300
                          Re: I am learning C: a little problem with a simple source code ImpalerCore <jadill33@gmail.com> - 2011-11-04 21:48 -0700
            Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:19 -0700
              Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-02 09:26 +1300
            Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:23 -0700
              Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-02 09:27 +1300
                Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:31 -0700
          Re: I am learning C: a little problem with a simple source code jt@toerring.de (Jens Thoms Toerring) - 2011-11-01 22:48 +0000
            Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 23:09 -0800
          Re: I am learning C: a little problem with a simple source code gordonb.prbii@burditt.org (Gordon Burditt) - 2011-11-01 21:24 -0500
          Re: I am learning C: a little problem with a simple source code Dann Corbit <dcorbit@connx.com> - 2011-11-02 21:03 -0700
            Re: I am learning C: a little problem with a simple source code Ike Naar <ike@sverige.freeshell.org> - 2011-11-03 06:39 +0000
            Re: I am learning C: a little problem with a simple source code Barry Schwarz <schwarzb@dqel.com> - 2011-11-03 18:33 -0700
              Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-04 03:03 -0700
                Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 11:40 +0100
                Re: I am learning C: a little problem with a simple source code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-04 11:36 +0000
                  Re: I am learning C: a little problem with a simple source code Kaz Kylheku <kaz@kylheku.com> - 2011-11-04 15:58 +0000
            Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 07:24 +0100
              Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 10:28 +0100
                Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 16:02 +0100
                  Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 16:20 +0100
                    Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 16:39 +0100
              Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-06 18:07 +0100
                Re: I am learning C: a little problem with a simple source code Barry Schwarz <schwarzb@dqel.com> - 2011-11-06 09:59 -0800
                  Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-06 19:23 +0100
                  Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-06 15:23 -0500
        Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 11:52 -0700
          Re: I am learning C: a little problem with a simple source code "BartC" <bc@freeuk.com> - 2011-11-01 23:02 +0000
    Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-02 03:53 -0700
      Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-02 08:03 -0400
        Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-02 06:11 -0700

Page 1 of 8  [1] 2 3 4 5 6 7 8  Next page →


#12449 — I am learning C: a little problem with a simple source code

Fromzemir <zemir_fp@libero.it>
Date2011-11-01 08:27 -0700
SubjectI am learning C: a little problem with a simple source code
Message-ID<f98781ff-2685-42af-bc3f-73ab27f898dc@d37g2000prg.googlegroups.com>
Hi, this is my first message on this group............ In these days i
am studing C language and i wrote some codes. Today i am writing a
small programm to calculate the perimeter and the area of a circle;
this is:

#include <stdio.h>;
#include <math.h>;
float area(float r)
{                                                    /*define area
function*/
	return(r*r*3.14);
}
float perimeter(float r)
{                                             /*define perimeter
function*/
		return(2*3.14*r);
}
void main() {
	system("cls");
	float r=0; int c=121 /*define radius and c variable used to
                                       control the behaviour of the
program;
                                       it is set to 121 value (y
char)*/
	printf("This programm calculate Area and Perimenter of a Circle");
	do {
		printf("\n\nPlease insert radius value (in cm): ");
		scanf("%f", &r);  /*input of the radius value*/
		if(r>0) {
			printf("Area is cm2: ");
			printf("%.2f", area(r));                                       /
*call area function*/
			printf("\nPerimeter is cm: ");
			printf("%.2f", perimeter(r));                       	     /*call
perimeter function*/
		} else printf("Value must to be a valid number");
		printf("\nDo you want to continue? (y or elsewhere to exit)");
		c=getch();
	} while(c==121);
	printf("\nEND\n");
}

I use Tcc compiler on Windosw XP. The program works fine, i wrote it
to get positive value number else it print a warning message. However
also in the case i make a mistake and write a string or a char it
display a warning message but if i try to remake the calculate it does
not work as well as i should want
Why?.
I have tried to resete r value to zero:
else {
       r=0;
       printf("Value must to be a valid number");
 }
but it is the same.
I hope in your help.
Thanks!

[toc] | [next] | [standalone]


#12453

FromMark Bluemel <mark_bluemel@pobox.com>
Date2011-11-01 15:49 +0000
Message-ID<j8p4ki$rpe$2@dont-email.me>
In reply to#12449
On 11/01/2011 03:27 PM, zemir wrote:
> Hi, this is my first message on this group............ In these days i
> am studing C language and i wrote some codes. Today i am writing a
> small programm to calculate the perimeter and the area of a circle;
> this is:
>
> #include<stdio.h>;
> #include<math.h>;
> float area(float r)
> {                                                    /*define area
> function*/
> 	return(r*r*3.14);
> }
> float perimeter(float r)
> {                                             /*define perimeter
> function*/
> 		return(2*3.14*r);
> }
> void main() {
> 	system("cls");
> 	float r=0; int c=121 /*define radius and c variable used to
>                                         control the behaviour of the
> program;
>                                         it is set to 121 value (y
> char)*/
> 	printf("This programm calculate Area and Perimenter of a Circle");
> 	do {
> 		printf("\n\nPlease insert radius value (in cm): ");
> 		scanf("%f",&r);  /*input of the radius value*/
> 		if(r>0) {
> 			printf("Area is cm2: ");
> 			printf("%.2f", area(r));                                       /
> *call area function*/
> 			printf("\nPerimeter is cm: ");
> 			printf("%.2f", perimeter(r));                       	     /*call
> perimeter function*/
> 		} else printf("Value must to be a valid number");
> 		printf("\nDo you want to continue? (y or elsewhere to exit)");
> 		c=getch();
> 	} while(c==121);
> 	printf("\nEND\n");
> }
>
> I use Tcc compiler on Windosw XP. The program works fine, i wrote it
> to get positive value number else it print a warning message. However
> also in the case i make a mistake and write a string or a char it
> display a warning message but if i try to remake the calculate it does
> not work as well as i should want
> Why?.
> I have tried to resete r value to zero:
> else {
>         r=0;
>         printf("Value must to be a valid number");
>   }
> but it is the same.
> I hope in your help.
> Thanks!

Using scanf for interactive input is (almost?) always a mistake...

Read the C FAQ at <http://c-faq.com> Section 12...

[toc] | [prev] | [next] | [standalone]


#12454

Fromnroberts <roberts.noah@gmail.com>
Date2011-11-01 08:52 -0700
Message-ID<d8cad0d0-27cb-44c0-a235-0519d17e7b79@n13g2000vbv.googlegroups.com>
In reply to#12449
On Nov 1, 8:27 am, zemir <zemir...@libero.it> wrote:

>         printf("This programm calculate Area and Perimenter of a Circle");
>         do {
>                 printf("\n\nPlease insert radius value (in cm): ");
>                 scanf("%f", &r);  /*input of the radius value*/

scanf leaves the input on the stream if it can't read it into the type
it expects.  Try reading the entire line with fgets and then using
sscanf on the string buffer you read into.

>  However
> also in the case i make a mistake and write a string or a char it
> display a warning message but if i try to remake the calculate it does
> not work as well as i should want
> Why?.
> I have tried to resete r value to zero:
> else {
>        r=0;
>        printf("Value must to be a valid number");
>  }
> but it is the same.
> I hope in your help.
> Thanks!

[toc] | [prev] | [next] | [standalone]


#12456

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-11-01 16:22 +0000
Message-ID<9hakm7FrvuU1@mid.uni-berlin.de>
In reply to#12449
zemir <zemir_fp@libero.it> wrote:
> Hi, this is my first message on this group............ In these days i
> am studing C language and i wrote some codes. Today i am writing a
> small programm to calculate the perimeter and the area of a circle;
> this is:

> #include <stdio.h>;
> #include <math.h>;
> float area(float r)
> {
>     return(r*r*3.14);
> }
> float perimeter(float r)
> {
>     return(2*3.14*r);
> }

I would perfer to have a global variable like this

const double Pi = 3.14159265358979323846;

or a define 3.14159265358979323846

#define PI 

Also, why not use double instead of float? They need a bit
more memory but tend to be faster and then you got a lot more
precision.

> void main() {

The correct form of main() is either

int main( void )

or

int main( int argc, char ** arhv )

>         system("cls");

Don't do that, first of all "cls" is not a command on all systems
out there and second it annoys most users when a program clears the
screen without a very good reason.

>         float r=0; int c=121

Why the initialization of 'c'? And when you insist in it why
not initioalize it to something readable (and more portable),
i.e. with

    c = 'y';


>         printf("This programm calculate Area and Perimenter of a Circle");
>         do {
>                 printf("\n\nPlease insert radius value (in cm): ");

Please note: unless you have a '\n' at the end of the output
line you better make sure the string has made it to the user.
It may not have done so and may still be in some internal
buffers. This, in this case, call

     flush( stdout );

after that line.

>                 scanf("%f", &r);  /*input of the radius value*/

scanf() can fail to read a number, as you found out, e.g. when
the input didn't consist of just a number. In that case scanf()
leaves the input buffer unmodified and it returns 0 (the return
value tells you how many items it could read. Thus it would be
prudent to check the return value also and if it isn't 1 (indi-
cating that it did read something that can be interpreted as a
number) you have to clean out the input buffer - otherwise the
next time round it will try to read again what it just rejected.
The simplest way to clear the input buffer is to call getc()
until it returns the end-of-line character '\n' (or EOF in
case the user closed the input stream).

>                 if(r>0) {
>                         printf("Area is cm2: ");
>                         printf("%.2f", area(r));                                       /
> *call area function*/
>                         printf("\nPerimeter is cm: ");
>                         printf("%.2f", perimeter(r));                                /*call
> perimeter function*/
>                 } else printf("Value must to be a valid number");
>                 printf("\nDo you want to continue? (y or elsewhere to exit)");
>                 c=getch();
>         } while(c==121);
>         printf("\nEND\n");
> }

> I use Tcc compiler on Windosw XP. The program works fine, i wrote it
> to get positive value number else it print a warning message. However
> also in the case i make a mistake and write a string or a char it
> display a warning message but if i try to remake the calculate it does
> not work as well as i should want
> Why?.

See above.

> I have tried to resete r value to zero:
> else {
>        r=0;

That won't help since the problem are the data remaining in
the input buffer. So do something like this

     if ( scanf( "%f", &r ) != 1 )
     {
         int in;

         while ( ( in = getc( ) ) != '\n' && in != EOF )
             /* empty */ ;

         if ( in == EOF )
             return 0;
     }

to clean out the input buffer if the user passed you something
that's not a number. Note that 'in' is an int, not a char. That
is what 'getc() returns (a char can not be tested for EOF, which
indicates in your case that the input has been closed - if you
would read from a file that would tell you that you reached the
end of the file). For that reason it might be prudent to also
make 'c' an int instead of a char.

                               Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

[toc] | [prev] | [next] | [standalone]


#12460

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2011-11-01 09:31 -0700
Message-ID<27db9617-8ff5-455a-8a90-ed40c1e4b7c2@p20g2000prm.googlegroups.com>
In reply to#12449
On Nov 1, 5:27 pm, zemir <zemir...@libero.it> wrote:
>
> I use Tcc compiler on Windosw XP. The program works fine, i wrote it
> to get positive value number else it print a warning message. However
> also in the case i make a mistake and write a string or a char it
> display a warning message but if i try to remake the calculate it does
> not work as well as i should want
> Why?.
>
scanf() returns the number of arguments successfully converted.

do this

while(scanf("%f\n", &r) != 1)
{
  printf("Can't read that number, try again\n');
}
--
Lots of C programming resources
http://www.malcolmmclean.site11.com/www


[toc] | [prev] | [next] | [standalone]


#12461

FromIke Naar <ike@sverige.freeshell.org>
Date2011-11-01 16:57 +0000
Message-ID<slrn3vfsjb097k.765.ike@sverige.freeshell.org>
In reply to#12460
On 2011-11-01, Malcolm McLean <malcolm.mclean5@btinternet.com> wrote:
> On Nov 1, 5:27?pm, zemir <zemir...@libero.it> wrote:
>>
>> I use Tcc compiler on Windosw XP. The program works fine, i wrote it
>> to get positive value number else it print a warning message. However
>> also in the case i make a mistake and write a string or a char it
>> display a warning message but if i try to remake the calculate it does
>> not work as well as i should want
>> Why?.
>>
> scanf() returns the number of arguments successfully converted.
>
> do this
>
> while(scanf("%f\n", &r) != 1)
> {
>   printf("Can't read that number, try again\n');
> }

Apart from the typo in the closing quote of the printf
argument, this will loop forever if the input is not a
valid float, e.g. if the stream contains "pqr".
You probably want to flush the erroneous input before
retrying the scanf operation.

[toc] | [prev] | [next] | [standalone]


#12467

Fromzemir <zemir_fp@libero.it>
Date2011-11-01 11:51 -0700
Message-ID<3cd13578-0463-46aa-9dae-9a548afb5117@l19g2000yqc.googlegroups.com>
In reply to#12461
Hi all and thanks for your suggestions, but i have solved. I think my
solution it is not good solution and more of you will be not very
happy but it seems to work fine, very fine. Please read:

#include <stdio.h>;
#include <math.h>;
const float Pi = 3.14159265358979323846;
float area(float r)
{                                                  /*define area
function*/
	return(r*r*Pi);
}
float perimeter(float r) {                                         /
*define perimeter function*/
		return(2*Pi*r);
}
void main() {
	system("cls");
	float r=0; int c;/*define radius and c variable used to control the
behaviour of program*/
	printf("This programm calculate Area and Perimenter of a Circle");
	printf("\n\nPlease insert radius value (in cm): "); /*input of the
radius value*/
	scanf("%f", &r);
if(r>0) {
	printf("Area is cm2: ");
	printf("%.2f", area(r));                                /*call area
function*/
	printf("\nPerimeter is cm: ");
	printf("%.2f", perimeter(r));                       	/*call perimeter
function*/
	} else {
		printf("Value must to be a valid number\n");
		printf("Press anykey to continue");
		c=getch();
		system("circle");
		return 0;
	}
	printf("\nDo you want to continue? (y or elsewhere to exit)");
	c=getch();
	if(c==121) {
		system("circle");
		return 0;
	}
}

Then i have another question about variable c: the y value of int c, i
think, it is ascii code so it is the same in all the system?

[toc] | [prev] | [next] | [standalone]


#12470

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-11-01 15:04 -0400
Message-ID<4EB042BB.4000906@verizon.net>
In reply to#12467
On 11/01/2011 02:51 PM, zemir wrote:
> Hi all and thanks for your suggestions, but i have solved. I think my
> solution it is not good solution and more of you will be not very
> happy but it seems to work fine, very fine. Please read:
> 
> #include <stdio.h>;
> #include <math.h>;

On most machines, storing Pi with this many digits in a float is a waste
of digits. You should use 'double', or even better, 'long double'.

> const float Pi = 3.14159265358979323846;
> float area(float r)
> {                                                  /*define area
> function*/
> 	return(r*r*Pi);
> }
> float perimeter(float r) {                                         /
> *define perimeter function*/
> 		return(2*Pi*r);
> }
> void main() {
> 	system("cls");
> 	float r=0; int c;/*define radius and c variable used to control the
> behaviour of program*/
> 	printf("This programm calculate Area and Perimenter of a Circle");
> 	printf("\n\nPlease insert radius value (in cm): "); /*input of the
> radius value*/
> 	scanf("%f", &r);

You haven't bothered to check whether or not scanf() succeeded. You do
realize that it can fail, don't you? What do you want your program to do
when that it is the case?

> if(r>0) {
> 	printf("Area is cm2: ");
> 	printf("%.2f", area(r));                                /*call area
> function*/
> 	printf("\nPerimeter is cm: ");
> 	printf("%.2f", perimeter(r));                       	/*call perimeter
> function*/
> 	} else {
> 		printf("Value must to be a valid number\n");
> 		printf("Press anykey to continue");
> 		c=getch();

You also haven't bothered to check for the possibility that getch()
might have failed.

> 		system("circle");
> 		return 0;
> 	}
> 	printf("\nDo you want to continue? (y or elsewhere to exit)");
> 	c=getch();
> 	if(c==121) {
> 		system("circle");

You also haven't bothered to check whether any of your system() calls
failed. Whether or not that's reasonable depends upon whether or not
"cls" or "circle" are capable of failing - which is outside the scope of
the C standard.

> 		return 0;
> 	}
> }
> 
> Then i have another question about variable c: the y value of int c, i
> think, it is ascii code so it is the same in all the system?

No, 'y' is a integer constant with a value that depends upon which
encoding is being used by the C implementation. It will be the ASCII
code value only if the implementation use ASCII or an ASCII-compatible
encoding such as Unicode (which is most modern systems, but it's nowhere
near to being all systems).

The thing is, 'y' will be the correct value to use, even on systems
where it's not the ASCII code for 'y', because on such systems, when the
user types 'y' on his keyboard, getch() should return exactly the same
non-ASCII value that is represented by  'y'.

[toc] | [prev] | [next] | [standalone]


#12475

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2011-11-01 20:21 +0000
Message-ID<0.2a6f6d23d2abfb2f60e6.20111101202118GMT.87bosvfvb5.fsf@bsb.me.uk>
In reply to#12470
James Kuyper <jameskuyper@verizon.net> writes:
> On 11/01/2011 02:51 PM, zemir wrote:
<snip>
>> 		c=getch();
>
> You also haven't bothered to check for the possibility that getch()
> might have failed.

If it's the getch I remember, it can't fail -- by which I mean there is
no error return that can tell you anything.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#12485

Fromjacob navia <jacob@spamsink.net>
Date2011-11-01 21:41 +0100
Message-ID<j8plh0$lkq$1@speranza.aioe.org>
In reply to#12475
Le 01/11/11 21:21, Ben Bacarisse a écrit :
> James Kuyper<jameskuyper@verizon.net>  writes:
>> On 11/01/2011 02:51 PM, zemir wrote:
> <snip>
>>> 		c=getch();
>>
>> You also haven't bothered to check for the possibility that getch()
>> might have failed.
>
> If it's the getch I remember, it can't fail -- by which I mean there is
> no error return that can tell you anything.
>
> <snip>

In a macintosh man getch gives

       All routines return the integer ERR upon failure and an integer 
value other than  ERR  (OK  in  the  case  of
        ungetch()) upon successful completion.

POSIX specifies:

Upon successful completion getch(), mvgetch(), mvwgetch() and wgetch() 
return the single-byte character, KEY_ value, or ERR. When in the 
nodelay mode and no data is available, ERR is returned.

getch is not specified by ISO C

[toc] | [prev] | [next] | [standalone]


#12471

FromIan Collins <ian-news@hotmail.com>
Date2011-11-02 08:06 +1300
Message-ID<9hau9pFelvU2@mid.individual.net>
In reply to#12467
On 11/ 2/11 07:51 AM, zemir wrote:
> Hi all and thanks for your suggestions, but i have solved. I think my
> solution it is not good solution and more of you will be not very
> happy but it seems to work fine, very fine. Please read:
>
> #include<stdio.h>;
> #include<math.h>;
> const float Pi = 3.14159265358979323846;

Why don't you use doubles?

> float area(float r)
> {                                                  /*define area
> function*/
> 	return(r*r*Pi);
> }
> float perimeter(float r) {                                         /
> *define perimeter function*/
> 		return(2*Pi*r);
> }
> void main() {

This should be int main().  On modern compilers, this won't compile 
because you are returning values.

> 	system("cls");

This won't do anything on a system without a "cls" command.

> 	float r=0; int c;/*define radius and c variable used to control the
> behaviour of program*/
> 	printf("This programm calculate Area and Perimenter of a Circle");
> 	printf("\n\nPlease insert radius value (in cm): "); /*input of the
> radius value*/
> 	scanf("%f",&r);
> if(r>0) {

It would be better to test the return of scanf rather than r.

> 	printf("Area is cm2: ");
> 	printf("%.2f", area(r));                                /*call area
> function*/
> 	printf("\nPerimeter is cm: ");
> 	printf("%.2f", perimeter(r));                       	/*call perimeter
> function*/
> 	} else {
> 		printf("Value must to be a valid number\n");
> 		printf("Press anykey to continue");
> 		c=getch();

getch() is non-standard.

> 		system("circle");

Are you trying to get the program to invoke its self?  Not a good idea!

> 		return 0;
> 	}
> 	printf("\nDo you want to continue? (y or elsewhere to exit)");
> 	c=getch();
> 	if(c==121) {

Why 121?  Why don't you use a character ('y')?

> 		system("circle");
> 		return 0;
> 	}
> }
>
> Then i have another question about variable c: the y value of int c, i
> think, it is ascii code so it is the same in all the system?

As long as the system uses ASCII!  Never use character values, use the 
character.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#12473

Fromzemir <zemir_fp@libero.it>
Date2011-11-01 13:17 -0700
Message-ID<9bd83721-7b79-46ec-8a3e-9d967bb8000e@f11g2000vbm.googlegroups.com>
In reply to#12471
I have tried to resolve with other ways as well as i understand but i
was not able to resolve it; only this way works such as i want. I have
changed cost Pi from float to bouble and i have changed c control
value from '121' to 'y'. Now all works fine. I think this binary can
run well on any other windows pc as well as run on my pc. The only
problem is if anyone want to change the name of binary but i think
this is not a common choice.

On Nov 1, 8:06 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> > void main() {
>
> This should be int main().  On modern compilers, this won't compile
> because you are returning values.
Yes, of course........... int main() is more right but my choice is
such common that it is not more a error......

[toc] | [prev] | [next] | [standalone]


#12477

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-11-01 16:24 -0400
Message-ID<4EB05569.8090009@verizon.net>
In reply to#12473
On 11/01/2011 04:17 PM, zemir wrote:
...
> On Nov 1, 8:06 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>>> void main() {
>>
>> This should be int main().  On modern compilers, this won't compile
>> because you are returning values.
> Yes, of course........... int main() is more right but my choice is
> such common that it is not more a error......

Just because it's a popular error doesn't make it cease to be an error.
Get it right now, while you're still learning. The fewer bad habits you
pick up now, while you're still new to the language, the less you'll
have to unlearn before you can be employable.

[toc] | [prev] | [next] | [standalone]


#12480

Fromzemir <zemir_fp@libero.it>
Date2011-11-01 13:27 -0700
Message-ID<1c44f3e8-d55a-40ea-b89d-9772b6cdf183@gy7g2000vbb.googlegroups.com>
In reply to#12477
On Nov 1, 9:24 pm, James Kuyper <jameskuy...@verizon.net> wrote:
> On 11/01/2011 04:17 PM, zemir wrote:
> ...
>
> > On Nov 1, 8:06 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> >>> void main() {
>
> >> This should be int main().  On modern compilers, this won't compile
> >> because you are returning values.
> > Yes, of course........... int main() is more right but my choice is
> > such common that it is not more a error......
>
> Just because it's a popular error doesn't make it cease to be an error.
> Get it right now, while you're still learning. The fewer bad habits you
> pick up now, while you're still new to the language, the less you'll
> have to unlearn before you can be employable.

Ok, from now and in the future i will use always "int main()"
I am sure thai it is the right choice.

[toc] | [prev] | [next] | [standalone]


#12506

FromKeith Thompson <kst-u@mib.org>
Date2011-11-01 20:24 -0700
Message-ID<lnhb2ni4v5.fsf@nuthaus.mib.org>
In reply to#12480
zemir <zemir_fp@libero.it> writes:
> On Nov 1, 9:24 pm, James Kuyper <jameskuy...@verizon.net> wrote:
>> On 11/01/2011 04:17 PM, zemir wrote:
>> ...
>>
>> > On Nov 1, 8:06 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> >>> void main() {
>>
>> >> This should be int main().  On modern compilers, this won't compile
>> >> because you are returning values.
>> > Yes, of course........... int main() is more right but my choice is
>> > such common that it is not more a error......
>>
>> Just because it's a popular error doesn't make it cease to be an error.
>> Get it right now, while you're still learning. The fewer bad habits you
>> pick up now, while you're still new to the language, the less you'll
>> have to unlearn before you can be employable.
>
> Ok, from now and in the future i will use always "int main()"
> I am sure thai it is the right choice.

For C, "int main(void)" is correct.  (For C++, it's "int main()".)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#12515

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2011-11-02 10:24 +0000
Message-ID<j8r5ph$5pg$3@news.xmission.com>
In reply to#12506
In article <lnhb2ni4v5.fsf@nuthaus.mib.org>,
Keith Thompson  <kst-u@mib.org> wrote:
>zemir <zemir_fp@libero.it> writes:
>> On Nov 1, 9:24 pm, James Kuyper <jameskuy...@verizon.net> wrote:
>>> On 11/01/2011 04:17 PM, zemir wrote:
>>> ...
>>>
>>> > On Nov 1, 8:06 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>>> >>> void main() {
>>>
>>> >> This should be int main().  On modern compilers, this won't compile
>>> >> because you are returning values.
>>> > Yes, of course........... int main() is more right but my choice is
>>> > such common that it is not more a error......
>>>
>>> Just because it's a popular error doesn't make it cease to be an error.
>>> Get it right now, while you're still learning. The fewer bad habits you
>>> pick up now, while you're still new to the language, the less you'll
>>> have to unlearn before you can be employable.
>>
>> Ok, from now and in the future i will use always "int main()"
>> I am sure thai it is the right choice.
>
>For C, "int main(void)" is correct.  (For C++, it's "int main()".)

As CLC returns to its roots...

(What about casting the return value of malloc()?)

-- 
"The anti-regulation business ethos is based on the charmingly naive notion
that people will not do unspeakable things for money." - Dana Carpender

Quoted by Paul Ciszek (pciszek at panix dot com).  But what I want to know
is why is this diet/low-carb food author doing making pithy political/economic
statements?

Nevertheless, the above quote is dead-on, because, the thing is - business
in one breath tells us they don't need to be regulated (which is to say:
that they can morally self-regulate), then in the next breath tells us that
corporations are amoral entities which have no obligations to anyone except
their officers and shareholders, then in the next breath they tell us they
don't need to be regulated (that they can morally self-regulate) ...

[toc] | [prev] | [next] | [standalone]


#12519

From"BartC" <bc@freeuk.com>
Date2011-11-02 11:04 +0000
Message-ID<j8r85n$rvp$1@dont-email.me>
In reply to#12515
"Kenny McCormack" <gazelle@shell.xmission.com> wrote in message
news:j8r5ph$5pg$3@news.xmission.com...
> In article <lnhb2ni4v5.fsf@nuthaus.mib.org>,
> Keith Thompson  <kst-u@mib.org> wrote:
>>zemir <zemir_fp@libero.it> writes:

>>> Ok, from now and in the future i will use always "int main()"
>>> I am sure thai it is the right choice.
>>
>>For C, "int main(void)" is correct.  (For C++, it's "int main()".)
>
> As CLC returns to its roots...

zemir is right. All these mis-declarations of 'main' are so widespread,
wouldn't it have been easier to have accepted them into the standard, than
have to be point them out every time? Especially as the compilers used don't 
seem to be complaining.

> (What about casting the return value of malloc()?)

(That's also been mentioned so many times that I've no idea now whether you
are supposed to cast or not. (And no I'm not interested in knowing; the way
I do it seems to work so I don't care.))

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#12527

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-11-02 07:41 -0400
Message-ID<j8ra8q$89n$1@dont-email.me>
In reply to#12519
On 11/02/2011 07:04 AM, BartC wrote:
...
> zemir is right. All these mis-declarations of 'main' are so widespread,
> wouldn't it have been easier to have accepted them into the standard, than
> have to be point them out every time? Especially as the compilers used don't 
> seem to be complaining.

I'm not happy with that idea, but I could live with it. However, in this
particular case his definition of the body of main() attempts to return
a value. Even if support for void main() were made mandatory, I think
that returning a value from main() when it has been declared as not
returning a value should still be a constraint violation.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#12542

FromSeebs <usenet-nospam@seebs.net>
Date2011-11-02 18:00 +0000
Message-ID<slrnjb30qj.1jaj.usenet-nospam@guild.seebs.net>
In reply to#12519
On 2011-11-02, BartC <bc@freeuk.com> wrote:
> zemir is right. All these mis-declarations of 'main' are so widespread,
> wouldn't it have been easier to have accepted them into the standard, than
> have to be point them out every time? Especially as the compilers used don't 
> seem to be complaining.

It might or might not have been easier, but it would have been a pretty
significant burden on some implementors, for no real benefit.  At least
some compilers complain...

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#12550

FromKeith Thompson <kst-u@mib.org>
Date2011-11-02 14:06 -0700
Message-ID<ln1utqi694.fsf@nuthaus.mib.org>
In reply to#12519
"BartC" <bc@freeuk.com> writes:
[...]
> zemir is right. All these mis-declarations of 'main' are so widespread,
> wouldn't it have been easier to have accepted them into the standard, than
> have to be point them out every time? Especially as the compilers used don't 
> seem to be complaining.

It would have been slightly easier for programmers (what's so
bloody hard about writing it correctly in the first place?),
slightly more difficult for most implementers, and potentially
substantially more difficult for some implementers.  Consider a
system where functions returning void and functions returning int
use significantly different calling conventions.  If compilers were
required to accept "void main(void)", they'd have to do some kind
of fixup to make it look like "int main(void)" to the linker.

My own preference would have been to make defining "main" incorrectly
a constraint violation, not undefined behavior.  For example:

    Permit "int main(void)", "int main()", or "int main(int
    argc, char *argv[])", or equivalent (with "or equivalent"
    being defined sufficiently clearly).  In addition, permit any
    implementation-defined form; if a compiler wants to support
    "void main(void)", it can do so.  A definition of "main" that
    doesn't match either one of the three explicitly permitted
    forms or a form defined by the implementation violates
    a constraint, requiring a diagnostic.  (N.B.: This is a
    hypothetical suggestion, not a description of the language as
    it's actually defined.)

(Aside: I've argued that "int main()" is not equivalent to "int
main(void)", which is why I list it separately.)

Programs using "void main(void)" (as well as books that claim it's
valid), would have been weeded out over the years as they failed
to compile.

If the committee had chosen to require "void main(void)" to be
accepted as well, it wouldn't particularly bother me.  I've never
argued that there's anything *intrinsically* wrong with it, just
that the standard doesn't support it.

But the fact remains that the language is defined the way it is,
and conforming to that definition shouldn't be at all difficult.
Failing to do so can *sometimes* indicate a lack of attention to
detail that can and should reduce the reader's trust in the rest
of the code (Schildt's books are a classic example).

IMHO, the blame lies not with the authors of the Standard, but with
the authors of books and tutorials (most of whom had access to the
Standard and therefore should have known better) who decided, for
no apparent reason, to tell their readers that "void main(void)"
is perfectly valid.

>> (What about casting the return value of malloc()?)
>
> (That's also been mentioned so many times that I've no idea now whether you
> are supposed to cast or not. (And no I'm not interested in knowing; the way
> I do it seems to work so I don't care.))

If you don't care, why mention it?  And since the vast majority of the
posts discussing it have been consistent, how can you be unaware of the
answer?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


Page 1 of 8  [1] 2 3 4 5 6 7 8  Next page →

Back to top | Article view | comp.lang.c


csiph-web