HDTenEightyP/NetNews huggingface dataset:
From: ugo@oliver.sublink.org (Ugo Cei)
Newsgroups: comp.lang.c
Subject: Re: The Correct Way To Write C if-Statements
Date: 21 Nov 92 13:32:59 GMT
Organization: Oliver System - Pavia ITALY

I am surprised that nobody has yet suggested this style:

	(cond) && expression;

or	(cond) && (expression, 1); in case "expression" has void type.

e.g.:       (1 == 1) && puts("1 == 1");
instead of  if (1 == 1) puts("1 == 1");

If you want to reverse the condition, change "&&" to "||".

					Ugo

P.S.: should I put a smiley in ? BTW, this is a common Perl idiom.

-- 
Ugo Cei --- Via Colombo 7 - 27100 Pavia - ITALY   |
         -- Sublink:  ugo@oliver.sublink.org      | "Real programs dump core"
Newsgroups: comp.lang.c
From: sjs@netcom.com (Stephen Schow)
Subject: Re: Printing out trees
Organization: Netcom - Online Communication Services  (408 241-9760 guest) 
Date: Mon, 16 Nov 1992 19:59:47 GMT

I used the following code to display the structure of a binary tree for
a CS class in college.  It works great, but I wish I had a way to show
the lines that connect the nodes (with characters that is).  Any suggestions?

Here's my code...

InGraph(tree,level)
struct node *tree;
int level;
{
     if (tree != NULL)
          {
          InGraph(tree->left,level+1);
          PrintLevel(tree,level);
          InGraph(tree->right,level+1);
          }
}


PrintLevel(tree,level)
struct node *tree;
int level;
{
     int i;


     for(i=1;i<=level*8;i++)
          fprintf(stdout," ");
     fprintf(stdout,"(%d)%s\n",level,tree->info);
}

-- 
------------------------------------------------------------------
Steve Schow    | But you don't have to use the claw, if you
sjs@netcom.com | pick the pear with the big paw paw......
               | Have I given you a clue......?
               |                       - Baloo the Bear
------------------------------------------------------------------
Newsgroups: comp.lang.c,comp.std.c
From: pdh@netcom.com (Phil Howard )
Subject: Re: Printing out trees
Organization: Netcom - Online Communication Services  (408 241-9760 guest) 
Date: Mon, 16 Nov 1992 08:10:21 GMT

Richard Dale Romero <rickr+@CMU.EDU> writes:

>No, that's not really what I meant--I meant in a formatted way
>so that I could tell what the child-parent relations were...

What I have done (while testing an implementation of my variation of
the AVL balanced binary tree) is to print deeper nodes more indented.

I don't have that particular code handly at the moment, but it was
essentially much the same as the previously posted code excepted that
an addition argument was passed along AND INCREMENTED AT EACH LEVEL.
That value controlled a loop to putc() that number of blanks at the
front of each line.

BIG trees are going to be hard to visualize graphically no matter what
you do.


>Regardless it will print out a node such that its direct children
>are on the line directly below it, *every* item in the left
>sub-tree appears to the left of the current node, and *every* item
>in the right sub-tree appears to the right of the current node.

Mine printed the left children first, the parent in between, and the
right children last.  Since I was dealing with the sorted order of
the tree I needed to look at it that way.

Either way, the leaf nodes will always be adjacent to their parents,
but intermediate nodes won't be because the leaf nodes are in the way,
except for the initial descent in your logic.  That is if I understood
you correctly.
-- 
/***********************************************************************\
| Phil Howard  ---  KA9WGN  ---  pdh@netcom.com   |   "The problem with |
| depending on government is that you cannot depend on it" - Tony Brown |
\***********************************************************************/
From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
Newsgroups: comp.lang.c
Subject: Re: converting unsigned int to signed int
Date: 20 Nov 92 10:41:09 GMT
Organization: McGill Research Centre for Intelligent Machines

In article <19226@ucdavis.ucdavis.edu>, fzjaffe@othello.ucdavis.edu (Rory Jaffe) writes:

> What is a safe, portable method of converting from signed int to
> unsigned?

A cast.

> I want the most negative signed value to convert to 0, and the most
> positive signed value to (max signed int) + abs(max negative signed
> int).

This may or may not be possible.  There is no guarantee that signed and
unsigned ints have the same "range", the same number of distinct
possible values.

However, given your description, why not just subtract INT_MIN?  That
will come about as close as anything can.  To be fully portable, you
have to split it up into various cases based on the value of the
original integer.  As a sketch, probably full of errors of detail:

	#include <limits.h>
	signed int si;
	unsigned int ui;

	if (si <= INT_MAX+INT_MIN)
	 { ui = si - INT_MIN;
	 }
#if INT_MAX+INT_MIN < -1
	/* this can happen only when more than half the possible int
	   values are negative - not possible with a two's complement
	   binary representation, for example */
	else if (si < 0)
	 { /* some code I don't feel like working out */
	 }
#endif
#if UINT_MAX < INT_MAX
	/* I'm not sure whether this is allowed; my reference is at
	   work and I'm at home */
	else if (s > UINT_MAX)
	 { /* more messy code */
	 }
#endif
	else
	 { ui = (unsigned int)si - (unsigned int)INT_MIN;
	 }

					der Mouse

			    mouse@larry.mcrcim.mcgill.edu
From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
Newsgroups: comp.lang.c
Subject: Re: Allowed Fortran and C optimizations
Date: 21 Nov 92 06:22:12 GMT
Organization: Comp Sci, RMIT, Melbourne, Australia

In article <1992Nov16.153307.4632@alchemy.chem.utoronto.ca>, mroussel@alchemy.chem.utoronto.ca (Marc Roussel) writes:
>      A few days ago, I suggested that I might post some example code
> showing that better performance can sometimes be obtained from Fortran
> than C unless one is willing to hand-tune the C code.

So much depends on the machine and the compilers.  On the machine that
I'm posting from, I have had cases where f2c+gcc produced faster code
than f77.

> Compiling this in ANSI mode with the -O flag we get 9.93 MFLOPS, i.e.
> about half the Fortran performance.  Interestingly, we get 9.93 MFLOPS
> without switching to array syntax, i.e. with just the la and lcnt hacks.

This should not have been a surprise.  Changing from
	foo(a) double a*;
to	foo(a) double a[];
changes NOTHING.  In both Classic C and ANSI C the two are identical
in every respect.

>      Now the +Om1 flag does what I want it to, but I wouldn't want to
> compile a library or program written in C by someone else with it:  Since C does
> not force you to keep your function parameters distinct, I couldn't
> count on the program executing correctly.

This is a well understood point, and Jim Giles has explained it in detail
many times.  The argument founders on one vital point:
	Fortran does not force you to keep your function parameters
	distinct _either_.
The basic point here is that
	The Fortran compiler goes ahead and ASSUMES that the arguments
	are distinct,
	the C compiler doesn't make that assumption unless you TELL it to.
It is true, and it is important, that the Fortran standard forbids aliased
arguments, and the C standard doesn't.  But if you want to "count on [a]
program executing correctly", you want a development system which will
enforce whatever assumptions the compiler is relying on.  I have seen a
great deal of Fortran code which blithely ignored the standard's restriction.
Tools like the PORT checker or ToolPack are all but essential.
From: andrew@iwr.ru.ac.za (Andrew Murdoch)
Newsgroups: comp.lang.c
Subject: Re: The Correct Way To Write C if-Statements
Date: 18 Nov 92 08:56:40 GMT
Organization: Rhodes University, Grahamstown, South Africa

In article <605@ulogic.UUCP> hartman@ulogic.UUCP (Richard M. Hartman) writes:
>From: hartman@ulogic.UUCP (Richard M. Hartman)
>Subject: Re: The Correct Way To Write C if-Statements
>Date: 14 Nov 92 23:48:17 GMT
>In article <9130056@hpavla.lf.hp.com> gajdos@hpavla.lf.hp.com (Larry Gajdos) writes:
>>In comp.lang.c, booloo@framsparc.ocf.llnl.gov (Mark Boolootian) writes:
>>
>>> Braces
>>
>>> Braces belong on children's teeth and around the body of every 
>>> single if-statement.  There are no exceptions.  Every single if-body 
>>> is to be surrounded by braces.  The essence of the argument in favor 
>>> of braces is this: all if-statements should look the same, 
    ...  stuff deleted

>>Just for the sake of argument (-; doesn't this logic then imply that,
>>since some if statements have else clauses, all if statements should have
>>else clauses?
>
>No, but it might imply that all tests should be constructed to 
>insist on TRUE conditions and not allow the negation of the test e.g.:
    ... stuff deleted
This seems totally illogical to me.  Ho far do you want to extend this 
concept?  One might then say that since if statements are just C statements,
all C statements should look the same, and then not use certain features 
offered by the language.  Some if statements have 'else's and some don't, 
and some if statements have braces, and some don't.
From: kdq@quest.UUCP (Kevin D. Quitt)
Newsgroups: comp.lang.c
Subject: Re: Is this ANSI?
Date: Sun, 22 Nov 92 12:30:30 PST
Reply-To: srhqla!quest!kdq
Organization: Job quest  (805) 251-8210,  So Cal: (800) 400-8210

>>>struct FM
>>>{
>>>	short	data_len;
>>>	char	data[];        /*  This line is the kicker  */
>>>};

    My apologies for asking a question and not being around for the
discussion; my upstream site dropped two days of news and mail on the
floor.  There is a legitimate reason for using the data field of the
above struct: it's a handle for the data that follows the header.

    The actual header consists of a number of items, including a data
count which can be 0 or more.  The data is arriving via a tcp socket,
so short of outrageous timeouts, we need some way to know when
everything has arrived.  The header structure is fixed size, and the
data that follow is one of several different structures, or free form
data (for being transmitted to other processes).

    The data field of FM lets me write code like:

    struct s1 *s1p	= FM.data;

instead of

    struct s1 *s1p	= (struct s1 *)((char *)&FM + sizeof(struct FM));

    From the one article I did see, it appears that ANSI allows this
usage of null arrays, but does not require it.  Too bad.  Well, I'll
know for sure Tuesday, when I get my copy of the (old) ANSI document;
at $184 including shipping and tax, it's cheap at half the price.


 _
Kevin D. Quitt      96.37% of all statistics are made up.     srhqla!quest!kdq
Newsgroups: comp.lang.c
From: pdh@netcom.com (Phil Howard )
Subject: Re: Can you program in C?
Organization: Netcom - Online Communication Services  (408 241-9760 guest) 
Date: Thu, 19 Nov 1992 21:45:09 GMT

2644danticoj@vms.csd.mu.edu writes:

>My name is Cetera and I am new to this network. I am anxiously awaiting
>graduating Marquette this December, but there is an obstacle in my way. I must
>first pass this math class in which I must program in C. I don't know this
>language, and now must write several programs before the end of the year. The
>course teaches data structures, which I have no problems understanding.
>However, all homework is programmed in C and because of an adminstrative
>loop-hole, I was enrolled in the class with no knowledge of the needed
>programming language. So, can you help me write these programs, thereby
>allowing me to graduate and go on to serve my country as an officer on a
>nuclear submarine? Please???? My future depends on it. By the way, I will be
>using a UNIX machine with a STUDSYS compiler. Help!!!!!!!!!!!!!!!!!!!!!

Maybe someone MIGHT **help** but people will NOT do your programming FOR YOU.
Some of the other replies should make this very obvious.

Buy a good book on C programming and study it and learn it.  I first learned
C in just one week from the original K&R, I would not be trustable with a
nuclear sub, so you should be able to do it in just a couple nights.

I am serious!  Study it!

There are a lot of "dusty corners" and "pitfalls" in C.  As you encounter
these things trying to learn it, first make an exhaustive effort to find
out just what is happeing (print out the values of variables and stuff).
Then if you remain puzzled, go ahead and ask specific questions on the net
and include the full code (of a small program) that is puzzling.

Be sure the FAQ does not address the issue.  You should get and print a
copy of the C language FAQ, too, to have alongside your C book, to help
you through the tricky parts.


>Problem statement
>*****************

The rest of your posting is irrelevant.

When you are in over your head, you have 3 choices:

1.  Stay there and die.
2.  Swim to the surface to breath and live.
3.  Depend on someone else to save you.

Remember, that if you choose number 3, you are depending on someone else
who has chosen number 2.
-- 
/***********************************************************************\
| Phil Howard  ---  KA9WGN  ---  pdh@netcom.com   |   "The problem with |
| depending on government is that you cannot depend on it" - Tony Brown |
\***********************************************************************/
Xref: sparky comp.lang.c:16608 comp.software-eng:4320
From: tpd@icf.hrb.com (THOMAS P. DONNELLY)
Newsgroups: comp.lang.c,comp.software-eng
Subject: Re: Will we keep ignoring this productivity issue?
Date: 16 Nov 92 13:05:46 EST
Organization: HRB Systems, Inc.

In article <1992Nov13.211018.24360@novell.com>, Duane Murphy <damurphy@wc.novell.com> writes:
> In article Ralph Johnson writes:

   ... stuff deleted ...
> 
> Throughout my experience I have repeatedly noticed parallels between the 
> two areas.  This is not unusual, they are both engineering problem 
> solving based studies.  However, I seem to draw more from my training in 
> hardware as I write software.  Hardware design is much older than 
> software design (and to a limited extent easier), but the way it is 
> taught is far more structured than software.
> 
> I recall my first software class.  Here is Fortran syntax; write a
> program.
> Not much training.  Not much experience. Compare that to the first _TWO_
> years of hardware.

I got my degrees 15 years ago, and it was better than that then.  We even
had courses concerned with top-down structured design, large-scale, 
team-based development, and so on.

As for the rest: Hear, hear!  I have a foot in both camps, too, with 
similar (non-commercial) experiences.  Hardware is easier because we 
have made it that way.  It is built much more with levels of implementa-
tion, the levels are better separated, and designs are more modular 
within any one level.  Most of the time.  e.g. chip design is completely
independent of the design that uses the chips.  Boards, likewise, so long
as each level conforms to its interface specifications.


> I believe that we are not teaching programmers analysis before design.  
> We teach them syntax and pretend that this is analysis.  Analysis is real 
> and important.  How can you design a program that you cannot read!
> 
> Just one man's humble opinion,
> Duane

True, as far as it goes.  We need to do a better job of teaching design.
However, I'm unsure that teaching design analysis is a useful precursor
to teaching design.  Perhaps I do not understand what you mean by
analysis.
Organization: University of Illinois at Chicago
Date: Thursday, 19 Nov 1992 00:06:22 CST
From: <U17868@uicvm.uic.edu>
Newsgroups: comp.lang.c
Subject: fwrite & fread

I am using write & fread to read and write to a file. The structure that I
am writing has 194 bytes. After executing fwrite I check the value, in vari
able a which is 1 (correct), because I am writing one structure. Then I check
the values of returned by fclose and ferror and they are also correct i-e 0.
But when I quit from the program without writing more structures or without
running any other functions and look for the size of my file it shows that it
has 414 bytes. Actually, it should have 194 bytes. I have tried with different
file modes, but that doesn't solve the problem. I have also tried with a file
initially empty and with a file having some records. Therefore, I tried clearin
g the buffer, but that also doesn't help. For those of you who might be interes
ted in the code, I have it below. Don't pay attention towards syntax errors.
I have also included a part of my header file which contains the structure.
Thanks in advance,
Sikander Waheed    email: u17868@uicvm.cc.uic.edu

#include <stdio.h>

#include <stdlib.h>
typedef struct activity{
	int  id;
	int  mm;
	int  dd;
	int  yy;
	char day[10];
	int  hour;
	int  min;
	int  end_hour;
	int  end_min;
	char location[25];
        char with_who[20];
	char phone[11];
	char subj[30];
	char note[300];
        int  flag;
};

void add()
{
int a,mm,dd,yy,hour,min,st_hour, end_min = 0 , t2_hour, t2_min, i = 0, ok;
char ch;
FILE *file_pointer;
struct activity temp;
 ok = busy(mm, dd, yy, hour, t2_hour);
 if(ok == 1)
   {
     printf("Schedule can be added\n");
     file_pointer = fopen("activity.dat","ab+");
     setvbuf(file_pointer, NULL, 195, _IOFBF);
     temp.id = 1234;
     temp.mm = mm;
     temp.dd = dd;
     temp.yy = yy;
     temp.hour = hour;
     temp.min = min;
     temp.end_hour = st_hour;
     temp.end_min = end_min;
     temp.flag = 1;
     printf("Enter the day of the activity ");
     gets(temp.day);
     printf("Enter the location of the activity ");
     gets(temp.location);
     printf("Enter the name of the person ");
     gets(temp.with_who);
     printf("Enter the phone number of the person ");
     gets(temp.phone);
     printf("Enter the subject of the activity ");
     gets(temp.subj);
     printf("Enter the note about the activity ");
     gets(temp.note);
	  temp.note[i] = '\0';

	 a = fwrite(&temp, sizeof(temp), 1, file_pointer);
	 a = ferror(file_pointer);
	 a = fclose(file_pointer);
	 a = ferror(file_pointer);
	 setvbuf(file_pointer, NULL , 194, _IOFBF);
	 ch = getchar();
     } /* end if */
    return;
}
From: folsom@bigmax.UUCP (Al Folsom)
Newsgroups: comp.lang.c
Subject: Re: Pascal as C prelude?
Summary: watch it!
Date: 23 Nov 92 14:54:40 GMT
Organization: Fischer & Porter Co., Warminster, PA.

In article <1992Nov21.181810.29808@csservices.Princeton.EDU>, blume@kastle.Princeton.EDU (Matthias Blume) writes:
> 
> |> #define SILLY_MODE
> |> #ifdef SILLY_MODE
> |> 
> |> There's just one drawback, when you do this: you'll end up writing
> |> ugly formatted code like:
> |> 
> |> if (condition)
> |> 	{
> |> 	statement1;
> |> 	statement2;
> |> 	}
> |> 
> |> instead of the proper:
> |> 
> |> if (condition) {
> |> 	statement1;
> |> 	statement2;
> |> }
> |> 
> |> #endif
> 
> # define SERIOUS_MODE
> # ifdef SERIOUS_MODE

All this discussion about formatting, and I kept wondering why
no one ever mentioned the one true style, with the braces on lines
by themselves, indented to the same level as enclosed statements.
Now I know -- I was actually writing Pascal back in 1979, three
years before I thought I learned that language.  What a genius
I am!  But I'm in good company, Tom Plum must also be a closet
Pascal programmer, since the first edition of "C Programming
Guidelines" (1984) shows that style.  Wait a minute, he taught
a few courses at my company back in 79 & 80, maybe I infected
HIM!  Oh, the shame of it all...

As far as the original topic goes, I teach a course in "C" at 
a local community college.  Most of my students are not full 
time students, but rather people trying to learn a new job skill.
I don't have the luxury of multiple semester courses, or leaving
out the "advanced" portions of the language, because they're 
going to go use it like right now.  I invariably find that 
students with Pascal experience pick up C with only a little 
difficulty.  Fortran programmers have a little more difficulty,
and people with only COBOL backgrounds are doomed.  Yes, Pascal
is a good intro, more for the benefits of a background in the 
principles of structured programming, than for a direct translation
of knowledge from one language to the other.

Al Folsom

+-------------------------------------------------------------------+
| Al Folsom                 | Object Oriented Programming?  We've   |
| Fischer & Porter Co.      | been doing that for years...          |
| ...uunet!bigmax!folsom    |                                       |
| also folsom@decus.org     | When the customer objects to the way  |
| KY3T@WA3TSW (Ham)         | it works, we go program some more!    |
+-------------------------------------------------------------------+

Newsgroups: comp.lang.c
From: hjv@phil.ruu.nl (Hendrik Jan Veenstra)
Subject: The Correct Way To Write C if-Statements
Organization: Department of Philosophy, University of Utrecht, The Netherlands
Date: Sat, 21 Nov 1992 18:31:05 GMT



sca@gese.ge14.mdadv.gv.at (Petzi Schweda) writes:


>In article <aaronk.193.722052372@county.lmt.mn.org>, aaronk@county.lmt.mn.org (Aaron S. Kurland) writes:
>!> What do you think of this style?
>|> 
>|> if (cond)
>|> {
>|>    statement1;
>|> }
>|> else
>|> {
>|>    statement2;
>|> }
>|> 

>think you'll end up a little bit messy when using this one inside blocks:

>for (...)
>    {
>    if  (cond)
>    {
>        state;
>    }
>    else
>    {
>        state2;
>    }
>    }

Expanding the first suggestion logically would IMO render

   for (...)
   {
      if (...)
      {
         something;
      }
      else
      {
         something_else;
      }
   }

Also my preferred way of coding BTW...


--
Hendrik Jan Veenstra    hjv@phil.ruu.nl  *  How can I save my dreams
Dept. of Philosophy                      *  in the nightmare, in the flood
University of Utrecht                    *  But I try.
The Netherlands                          *
Newsgroups: comp.lang.c
From: adamstr@cc.uvcc.edu
Subject: Question on data storage
Organization: Utah Valley Community College
Date: 16 Nov 92 15:05:35 -0700

I am somewhat new to the world of C. I am trying to find a way to store data in
a non-readable format. I had initially thought that the "wb" and "rb" options
for the "mode" portion of fopen would do the trick, but that doesn't seem to do
what i want. (this is for DOS by the way...) I used to work for a company that
used C on a Unix platform, and the database files for that particular
application were not readable, either by "typeing" the file or thru the use of
a text editor. Is this sort of thing OS dependant?? Is there a header file
defined that will help?  Any suggestions will greatly appreciated. Thanks!

Please e-mail to    adamstr@cc.uvcc.edu

Newsgroups: comp.lang.c
From: dfuller@portal.hq.videocart.com (Dave Fuller)
Subject: Re: C source code for async/sync communcation using RS-232
Organization: VideOcart Inc.
X-Newsreader: Tin 1.1 PL3
Date: Thu, 19 Nov 1992 22:45:33 GMT

sumargoh@serss0.fiu.edu (H. Sumargo) writes:
: I am currently trying to write a communication project for a class.  I am looking for
: the source code to send and receive data through RS-232.  If you can give some pointers
: as to where I can get the souce code, please email your response to sumargoh@fiu.edu.
: I prefer a C source code; however, IBM pC assembly source code is also welcome.
: 
: Thank you very much.

 This is pretty large. Why ask people for it ? I doubt you'll get it. The     
sending of the data is probably the toughest part of your project.
Do it yourself. Since any comm program that allows user input works on
an interrupt basis, you need to write an interrupt handler, and store
input into a ring buffer in memory where it can be fetched from later
without losing info. Also, you need all of the bitmasks for controlling 
the 8250 UART. I have an excellent set of comm software that will run
in the background, works on its own custom error-correcting protocol. It
also works like zmodem with variable packet sizes depending on the           
line quality during transmission. It also works at the highest speed i've       bothered to test it at (19200) without data loss. But, since it took me
4 months of my free time to do this (AS A SCHOOL PROJECT) you aren't
seeing line 1 of it. Do yourself a favor, research and implement on your 
own. This area of work is rather fascinating.

Dave Fuller
dfuller@portal.hq.videocart.com


Newsgroups: comp.lang.c