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


Groups > comp.lang.c > #99508

Re: Program to read csv file, store characters and numbers in separate arrays

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: Program to read csv file, store characters and numbers in separate arrays
Date 2016-12-15 15:37 +0000
Organization Fix this later
Message-ID <o2uda0$mjd$1@dont-email.me> (permalink)
References <09988b6c-e11e-4fd6-8731-7992d16c7b94@googlegroups.com> <o2u8a9$3n0$1@dont-email.me> <o2ubtq$hb6$1@dont-email.me>

Show all headers | View raw


On 15/12/16 15:14, David Brown wrote:
> On 15/12/16 15:12, Richard Heathfield wrote:
>> [the code]
>>
>> On 15/12/16 13:05, Alla _ wrote:
>>
>>> (3) I will use a header file, in which I will store preprocessor
>>> commands, like
>>> #define and #include, as well as functions' declarations, etc.;
>>
>> A well-designed header contains *only* the following:
>>
>> 1) inclusion guards;
>> 2) macro definitions (#defines);
>> 3) type definitions (typedefs);
>> 4) function prototypes;
>> 5) comments;
>> 6) #include directives *ONLY* for those headers that are required for
>> the immediate purposes of the header itself.
>>
>> For example, here's a quick header I threw together a year or so ago:
>>
>> /* CISPRNG - Cryptographically In-Secure
>>  * Pseudo-Random Number Generator
>>  *
>>  * Written by Richard Heathfield,
>>  * 20 October 2015.
>>  *
>>  * You will also need cisprng.c and a
>>  * C compiler.
>>  *
>>  * The author places this code into the
>>  * public domain, and so it may be freely
>>  * used for any legal purpose, although
>>  * I would recommend against using it
>>  * in circumstances where you really need
>>  * a cryptographically secure PRNG, because
>>  * it isn't one.
>>  */
>> #ifndef H_CISPRNG_H_
>> #define H_CISPRNG_H_
>>
>> #define CISPRNG_RAND_MAX (0x7FFFFFFFUL)
>>
>> /* Single state versions */
>> /* set the seed */
>> void cisprng_set(unsigned long seed);
>> /* get a PRN */
>> unsigned long cisprng_random(void);
>> /* get a PRN in a range */
>> unsigned long cisprng_range(unsigned long low,
>>                             unsigned long high);
>> /* Custom state versions */
>> unsigned long cisprng_csrandom(unsigned long *pseed);
>> unsigned long cisprng_csrange(unsigned long *pseed,
>>                               unsigned long low,
>>                               unsigned long high);
>> #endif
>> /* end of cisprng.h */
>>
>> Note that it doesn't define any types. But if it did, they would go
>> after the #defines but before the function prototypes. Nor does it
>> include any headers. But *if* I'd used, say, size_t *in the header*, I'd
>> have included <stddef.h> in the header. But I would *not* include
>> <stddef.h> in the header just because the corresponding C file used
>> size_t (which, actually, it doesn't). A module header is not a place to
>> hide #includes.
>>
>
> I hope I am not going to cause too much confusion to a learner here, but
> I have a few comments about your rules for headers.  It may be that you
> agree with me, and just did not write your complete rule set.

I think we might be able to save Alla some confusion here by suggesting 
that she avert her eyes. :-)

> I think it is important to emphasis that you should only have macros,
> types, function prototypes, etc., in a header if these declarations will
> be used by other modules.

Yes, but.

With me so far? :-)

I was going to dig out a quotation from King Lear about not taking 
things far enough, but it turned out not to be quite so apt as I'd 
hoped. Oh well.

Module, right? Well, we have to say what we mean by "module". We might 
reasonably define it as a source and its associated header, in which 
case I think your point is fine. But we might also define it as a set of 
related functions, which might be spread over several sources. If we 
define it that way (and, frankly, I do), then it becomes reasonable to 
think of a module as having two headers: one public, and one private. 
The private header is shared across all the sources that go to make up 
that module, and the public header is shared across other modules.

So, for example, we might have an abstract data type that is defined in 
mylittlemoduleinternal.h and shared across mylittlemoduleA.c, 
mylittlemoduleB.c, and mylittlemoduleC.c. And then we have a bunch of 
prototypes listed in mylittlemodule.h, for sharing with other modules.

Would you agree with that?

> Anything that is used only within the
> implementation C file, should be in that file alone - and not the
> header.  So if you have a function "cisprng_calculate" that is not
> exported, it should not be declared in the header file, and it should be
> declared "static" in the C file.

But if it is needed by other sources within the same module, that gives 
us a problem. One possible solution is to share a function pointer in 
the private header --- but it's messy.

> Regarding macros, I would always limit the use of macros to when you
> actually need them to be macros.  If usage of CISPRNG_RAND_MAX permits,
> I would prefer:
>
> static const unsigned long int cisprng_rand_max = 0x7ffffffful;

Yeah, but this is C90, and it's an array size. :-)

Okay, obviously it isn't. But I'm sure you take my point. That one could 
indeed have been a const, but that isn't always possible.

> The rules of "const" objects in C mean that you often /do/ need to make
> such things #define'd values rather than static consts.

Yes.

> Assuming you are using C99 or C11 rather than C90 (I know some people
> still use C90),

You hear that, folks? David thinks I'm *some people*! Fame at last!

> then "static inline" functions are often a better
> alternative to function-like macros, and can go in headers as well.
>
> I would also put more emphasis on the comments.  Ideally, the comments
> in a header should tell the reader all they need to know about the
> functions and how to use them.

Up to a point, Lord Copper.

> It makes it easier for people using the
> code - they don't have to look elsewhere for documentation.  Of course,
> there are limits - comments should not be so extensive that it is hard
> to see the code!

And that's why we have external docs.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 05:05 -0800
  Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 13:48 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 08:22 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 16:28 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 09:25 -0800
          Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 19:31 +0000
            Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:19 -0800
              Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 10:01 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 02:43 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:03 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:08 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays jameskuyper@verizon.net - 2016-12-18 03:32 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Barry Schwarz <schwarzb@dqel.com> - 2016-12-16 17:09 -0800
            Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:25 -0800
              Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 10:11 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 02:45 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-16 09:54 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 17:56 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-16 18:02 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:06 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:28 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 12:02 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 12:41 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays BartC <bc@freeuk.com> - 2016-12-16 13:40 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 14:03 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays BartC <bc@freeuk.com> - 2016-12-16 13:03 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 15:04 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays BartC <bc@freeuk.com> - 2016-12-16 15:26 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 17:01 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 04:18 -0800
  Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 14:12 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-15 16:14 +0100
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 15:37 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-15 23:20 +0100
          Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 23:27 +0000
            Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-16 12:10 +0100
              Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:43 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-16 14:01 +0100
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 13:12 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-16 14:57 +0100
      Re: Program to read csv file, store characters and numbers in separate arrays BartC <bc@freeuk.com> - 2016-12-15 16:30 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 16:36 +0000
          Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-15 23:29 +0100
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 08:26 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 16:33 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 08:32 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 16:37 +0000
      Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-15 12:17 -0800
        Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:33 -0800
    Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-15 12:23 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 20:39 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-15 21:13 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 00:39 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 08:54 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:09 -0800
          Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 09:32 +0000
            Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:51 -0800
              Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 10:08 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 11:09 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:16 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:20 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-16 09:48 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-16 17:55 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays raltbos@xs4all.nl (Richard Bos) - 2016-12-20 11:53 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:33 +0000
            Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-16 09:46 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays "James R. Kuyper" <jameskuyper@verizon.net> - 2017-01-03 15:38 -0500
  Re: Program to read csv file, store characters and numbers in separate arrays Barry Schwarz <schwarzb@dqel.com> - 2016-12-15 12:36 -0800
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:44 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 10:04 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:14 -0800
          Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:46 +0000

csiph-web