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


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

To use Perl like conditional assignment in C

Started byYa Shou <shouyatf@gmail.com>
First post2011-09-24 04:06 -0700
Last post2011-09-26 01:37 +0100
Articles 15 — 10 participants

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


Contents

  To use Perl like conditional assignment in C Ya Shou <shouyatf@gmail.com> - 2011-09-24 04:06 -0700
    Re: To use Perl like conditional assignment in C Willem <willem@toad.stack.nl> - 2011-09-24 11:13 +0000
    Re: To use Perl like conditional assignment in C pete <pfiland@mindspring.com> - 2011-09-24 07:48 -0400
      Re: To use Perl like conditional assignment in C Uno <merrilljensen@q.com> - 2011-09-24 21:58 -0700
        Re: To use Perl like conditional assignment in C pete <pfiland@mindspring.com> - 2011-09-25 08:44 -0400
          Re: To use Perl like conditional assignment in C Uno <merrilljensen@q.com> - 2011-09-26 21:52 -0700
            Re: To use Perl like conditional assignment in C pete <pfiland@mindspring.com> - 2011-09-27 21:14 -0400
              Re: To use Perl like conditional assignment in C Keith Thompson <kst-u@mib.org> - 2011-09-27 19:56 -0700
        Re: To use Perl like conditional assignment in C Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-09-25 06:53 -0700
        Re: To use Perl like conditional assignment in C "J. J. Farrell" <jjf@bcs.org.uk> - 2011-09-26 04:40 +0100
    Re: To use Perl like conditional assignment in C Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-24 09:04 -0400
      Re: To use Perl like conditional assignment in C Willem <willem@toad.stack.nl> - 2011-09-24 14:06 +0000
      Re: To use Perl like conditional assignment in C Ya Shou <shouyatf@gmail.com> - 2011-09-26 21:10 -0700
      Re: To use Perl like conditional assignment in C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-09-29 22:08 +0000
    Re: To use Perl like conditional assignment in C "BartC" <bc@freeuk.com> - 2011-09-26 01:37 +0100

#10844 — To use Perl like conditional assignment in C

FromYa Shou <shouyatf@gmail.com>
Date2011-09-24 04:06 -0700
SubjectTo use Perl like conditional assignment in C
Message-ID<a2b7378d-3156-4f3c-ad6a-1bf14330d993@5g2000yqo.googlegroups.com>
Hello,


I want to assign an uncertain value to a variable, this variable with
values in the configuration file, reads the configuration file
through a function to return to the read configuration value, if not
configured or the value of non-compliance, will return 0 for failure,
when this, I will to give it a default value assigned.

However, I found it seem impossible to achieve this in C, but much
easier to implement in Perl, see below:

cfgval = loadConfig () || DEFAULT_VAL;

In C '||' operator does not return the value of the expression which
is true, but returns 1 or 0, so that I can not be used in a similar
way. I considered a program in C is:

cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;

And however, this approach to involve a duplication of code, it is
ugly, the other will call loadConfig function twice, inefficient and
likely to cause unnecessary accidents.

Of course, if / else statement can achieve this functionality, but
this method requires an additional variable, and not look simple. As
follows:

int loadval = loadConfig ();
if (loadval! = 0) {
   cfgval = loadval;
} else {
   cfgval = DEFAULT_VAL;
}

Or

int loadval;
cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;


I would like to ask you is, in this case, there is nothing more
beautiful realization?


Thanks.

[toc] | [next] | [standalone]


#10845

FromWillem <willem@toad.stack.nl>
Date2011-09-24 11:13 +0000
Message-ID<slrnj7rerd.2vmr.willem@toad.stack.nl>
In reply to#10844
Ya Shou wrote:
) Hello,
)
)
) I want to assign an uncertain value to a variable, this variable with
) values in the configuration file, reads the configuration file
) through a function to return to the read configuration value, if not
) configured or the value of non-compliance, will return 0 for failure,
) when this, I will to give it a default value assigned.
)
) However, I found it seem impossible to achieve this in C, but much
) easier to implement in Perl, see below:
)
) cfgval = loadConfig () || DEFAULT_VAL;
)
) In C '||' operator does not return the value of the expression which
) is true, but returns 1 or 0, so that I can not be used in a similar
) way. I considered a program in C is:
)
) cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
)
) And however, this approach to involve a duplication of code, it is
) ugly, the other will call loadConfig function twice, inefficient and
) likely to cause unnecessary accidents.
)
) Of course, if / else statement can achieve this functionality, but
) this method requires an additional variable, and not look simple. As
) follows:
)
) int loadval = loadConfig ();
) if (loadval! = 0) {
)    cfgval = loadval;
) } else {
)    cfgval = DEFAULT_VAL;
) }
)
) Or
)
) int loadval;
) cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
)
)
) I would like to ask you is, in this case, there is nothing more
) beautiful realization?

Why can't you assign to cfgval twice?

  if (!(cfgval = loadConfig()))
    cfgval = DEFAULT_VAL;

Or if you want to mimic the Perl style:

  (cfgval = loadConfig()) || (cfgval = DEFAULT_VAL);

But that will be deemed very ugly by most C programmers.

IIRC, some compilers have an extension which allows this:

  cfgval = loadConfig() ?: DEFAULT_VAL;

But that is not standard C, as far as I know.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

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


#10848

Frompete <pfiland@mindspring.com>
Date2011-09-24 07:48 -0400
Message-ID<4E7DC3AA.427D@mindspring.com>
In reply to#10844
Ya Shou wrote:
> 
> Hello,
> 
> I want to assign an uncertain value to a variable, this variable with
> values in the configuration file, reads the configuration file
> through a function to return to the read configuration value, if not
> configured or the value of non-compliance, will return 0 for failure,
> when this, I will to give it a default value assigned.
> 
> However, I found it seem impossible to achieve this in C, but much
> easier to implement in Perl, see below:
> 
> cfgval = loadConfig () || DEFAULT_VAL;
> 
> In C '||' operator does not return the value of the expression which
> is true, but returns 1 or 0, so that I can not be used in a similar
> way. I considered a program in C is:
> 
> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
> 
> And however, this approach to involve a duplication of code, it is
> ugly, the other will call loadConfig function twice, inefficient and
> likely to cause unnecessary accidents.
> 
> Of course, if / else statement can achieve this functionality, but
> this method requires an additional variable, and not look simple. As
> follows:
> 
> int loadval = loadConfig ();
> if (loadval! = 0) {
>    cfgval = loadval;
> } else {
>    cfgval = DEFAULT_VAL;
> }
> 
> Or
> 
> int loadval;
> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
> 
> I would like to ask you is, in this case, there is nothing more
> beautiful realization?

    cfgval = loadConfig ();
    if (cfgval == 0) {
        cfgval = DEFAULT_VAL;
    }

-- 
pete

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


#10902

FromUno <merrilljensen@q.com>
Date2011-09-24 21:58 -0700
Message-ID<9e7n5aF5qvU1@mid.individual.net>
In reply to#10848
On 9/24/2011 4:48 AM, pete wrote:
> Ya Shou wrote:
>>
>> Hello,
>>
>> I want to assign an uncertain value to a variable, this variable with
>> values in the configuration file, reads the configuration file
>> through a function to return to the read configuration value, if not
>> configured or the value of non-compliance, will return 0 for failure,
>> when this, I will to give it a default value assigned.
>>
>> However, I found it seem impossible to achieve this in C, but much
>> easier to implement in Perl, see below:
>>
>> cfgval = loadConfig () || DEFAULT_VAL;
>>
>> In C '||' operator does not return the value of the expression which
>> is true, but returns 1 or 0, so that I can not be used in a similar
>> way. I considered a program in C is:
>>
>> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
>>
>> And however, this approach to involve a duplication of code, it is
>> ugly, the other will call loadConfig function twice, inefficient and
>> likely to cause unnecessary accidents.
>>
>> Of course, if / else statement can achieve this functionality, but
>> this method requires an additional variable, and not look simple. As
>> follows:
>>
>> int loadval = loadConfig ();
>> if (loadval! = 0) {
>>     cfgval = loadval;
>> } else {
>>     cfgval = DEFAULT_VAL;
>> }
>>
>> Or
>>
>> int loadval;
>> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
>>
>> I would like to ask you is, in this case, there is nothing more
>> beautiful realization?
>
>      cfgval = loadConfig ();
>      if (cfgval == 0) {
>          cfgval = DEFAULT_VAL;
>      }
>

pete,

you just posted another syntax other than c tonight in clc; watch it, 
there, revolutionary.
-- 
Uno

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


#10916

Frompete <pfiland@mindspring.com>
Date2011-09-25 08:44 -0400
Message-ID<4E7F224B.4BA0@mindspring.com>
In reply to#10902
Uno wrote:
> 
> On 9/24/2011 4:48 AM, pete wrote:
> > Ya Shou wrote:
> >>
> >> Hello,
> >>
> >> I want to assign an uncertain value to a variable, this variable with
> >> values in the configuration file, reads the configuration file
> >> through a function to return to the read configuration value, if not
> >> configured or the value of non-compliance, will return 0 for failure,
> >> when this, I will to give it a default value assigned.
> >>
> >> However, I found it seem impossible to achieve this in C, but much
> >> easier to implement in Perl, see below:
> >>
> >> cfgval = loadConfig () || DEFAULT_VAL;
> >>
> >> In C '||' operator does not return the value of the expression which
> >> is true, but returns 1 or 0, so that I can not be used in a similar
> >> way. I considered a program in C is:
> >>
> >> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
> >>
> >> And however, this approach to involve a duplication of code, it is
> >> ugly, the other will call loadConfig function twice, inefficient and
> >> likely to cause unnecessary accidents.
> >>
> >> Of course, if / else statement can achieve this functionality, but
> >> this method requires an additional variable, and not look simple. As
> >> follows:
> >>
> >> int loadval = loadConfig ();
> >> if (loadval! = 0) {
> >>     cfgval = loadval;
> >> } else {
> >>     cfgval = DEFAULT_VAL;
> >> }
> >>
> >> Or
> >>
> >> int loadval;
> >> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
> >>
> >> I would like to ask you is, in this case, there is nothing more
> >> beautiful realization?
> >
> >      cfgval = loadConfig ();
> >      if (cfgval == 0) {
> >          cfgval = DEFAULT_VAL;
> >      }
> >
> 
> pete,
> 
> you just posted another syntax other than c tonight in clc; watch it,
> there, revolutionary.

I don't think that I did that.

-- 
pete

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


#10992

FromUno <merrilljensen@q.com>
Date2011-09-26 21:52 -0700
Message-ID<9ecvitFmu0U2@mid.individual.net>
In reply to#10916
On 9/25/2011 5:44 AM, pete wrote:
> Uno wrote:
>>
>> On 9/24/2011 4:48 AM, pete wrote:
>>> Ya Shou wrote:
>>>>
>>>> Hello,
>>>>
>>>> I want to assign an uncertain value to a variable, this variable with
>>>> values in the configuration file, reads the configuration file
>>>> through a function to return to the read configuration value, if not
>>>> configured or the value of non-compliance, will return 0 for failure,
>>>> when this, I will to give it a default value assigned.
>>>>
>>>> However, I found it seem impossible to achieve this in C, but much
>>>> easier to implement in Perl, see below:
>>>>
>>>> cfgval = loadConfig () || DEFAULT_VAL;
>>>>
>>>> In C '||' operator does not return the value of the expression which
>>>> is true, but returns 1 or 0, so that I can not be used in a similar
>>>> way. I considered a program in C is:
>>>>
>>>> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
>>>>
>>>> And however, this approach to involve a duplication of code, it is
>>>> ugly, the other will call loadConfig function twice, inefficient and
>>>> likely to cause unnecessary accidents.
>>>>
>>>> Of course, if / else statement can achieve this functionality, but
>>>> this method requires an additional variable, and not look simple. As
>>>> follows:
>>>>
>>>> int loadval = loadConfig ();
>>>> if (loadval! = 0) {
>>>>      cfgval = loadval;
>>>> } else {
>>>>      cfgval = DEFAULT_VAL;
>>>> }
>>>>
>>>> Or
>>>>
>>>> int loadval;
>>>> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
>>>>
>>>> I would like to ask you is, in this case, there is nothing more
>>>> beautiful realization?
>>>
>>>       cfgval = loadConfig ();
>>>       if (cfgval == 0) {
>>>           cfgval = DEFAULT_VAL;
>>>       }
>>>
>>
>> pete,
>>
>> you just posted another syntax other than c tonight in clc; watch it,
>> there, revolutionary.
>
> I don't think that I did that.
>

Because perl is a child of c, or that if you thought what you posted was 
C, it wouldn't be syntactical?
-- 
Uno

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


#11034

Frompete <pfiland@mindspring.com>
Date2011-09-27 21:14 -0400
Message-ID<4E8274E0.7965@mindspring.com>
In reply to#10992
Uno wrote:
> 
> On 9/25/2011 5:44 AM, pete wrote:
> > Uno wrote:
> >>
> >> On 9/24/2011 4:48 AM, pete wrote:
> >>> Ya Shou wrote:
> >>>>
> >>>> Hello,
> >>>>
> >>>> I want to assign an uncertain value to a variable, this variable with
> >>>> values in the configuration file, reads the configuration file
> >>>> through a function to return to the read configuration value, if not
> >>>> configured or the value of non-compliance, will return 0 for failure,
> >>>> when this, I will to give it a default value assigned.
> >>>>
> >>>> However, I found it seem impossible to achieve this in C, but much
> >>>> easier to implement in Perl, see below:
> >>>>
> >>>> cfgval = loadConfig () || DEFAULT_VAL;
> >>>>
> >>>> In C '||' operator does not return the value of the expression which
> >>>> is true, but returns 1 or 0, so that I can not be used in a similar
> >>>> way. I considered a program in C is:
> >>>>
> >>>> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
> >>>>
> >>>> And however, this approach to involve a duplication of code, it is
> >>>> ugly, the other will call loadConfig function twice, inefficient and
> >>>> likely to cause unnecessary accidents.
> >>>>
> >>>> Of course, if / else statement can achieve this functionality, but
> >>>> this method requires an additional variable, and not look simple. As
> >>>> follows:
> >>>>
> >>>> int loadval = loadConfig ();
> >>>> if (loadval! = 0) {
> >>>>      cfgval = loadval;
> >>>> } else {
> >>>>      cfgval = DEFAULT_VAL;
> >>>> }
> >>>>
> >>>> Or
> >>>>
> >>>> int loadval;
> >>>> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
> >>>>
> >>>> I would like to ask you is, in this case, there is nothing more
> >>>> beautiful realization?
> >>>
> >>>       cfgval = loadConfig ();
> >>>       if (cfgval == 0) {
> >>>           cfgval = DEFAULT_VAL;
> >>>       }
> >>>
> >>
> >> pete,
> >>
> >> you just posted another syntax other than c tonight in clc; watch it,
> >> there, revolutionary.
> >
> > I don't think that I did that.
> >
> 
> Because perl is a child of c,
> or that if you thought what you posted was
> C, it wouldn't be syntactical?

I just posted some C code.
If it also happens to be some other code,
I don't know anything about it.

-- 
pete

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


#11036

FromKeith Thompson <kst-u@mib.org>
Date2011-09-27 19:56 -0700
Message-ID<ln4nzx1ika.fsf@nuthaus.mib.org>
In reply to#11034
pete <pfiland@mindspring.com> writes:
> Uno wrote:
>> On 9/25/2011 5:44 AM, pete wrote:
>> > Uno wrote:
>> >> On 9/24/2011 4:48 AM, pete wrote:
[...]
>> >>>       cfgval = loadConfig ();
>> >>>       if (cfgval == 0) {
>> >>>           cfgval = DEFAULT_VAL;
>> >>>       }
>> >>>
>> >>
>> >> you just posted another syntax other than c tonight in clc; watch it,
>> >> there, revolutionary.
>> >
>> > I don't think that I did that.
>> >
>> 
>> Because perl is a child of c,
>> or that if you thought what you posted was
>> C, it wouldn't be syntactical?
>
> I just posted some C code.
> If it also happens to be some other code,
> I don't know anything about it.

Neither does Uno.  The code fragment you posted is C; it's not Perl.

-- 
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]


#10920

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2011-09-25 06:53 -0700
Message-ID<db89d5da-ccbb-42f8-8d56-07f2d277ff90@j1g2000yqj.googlegroups.com>
In reply to#10902
On Sep 25, 5:58 am, Uno <merrilljen...@q.com> wrote:
> On 9/24/2011 4:48 AM, pete wrote:
>
>
>
>
>
> > Ya Shou wrote:
>
> >> Hello,
>
> >> I want to assign an uncertain value to a variable, this variable with
> >> values in the configuration file, reads the configuration file
> >> through a function to return to the read configuration value, if not
> >> configured or the value of non-compliance, will return 0 for failure,
> >> when this, I will to give it a default value assigned.
>
> >> However, I found it seem impossible to achieve this in C, but much
> >> easier to implement in Perl, see below:
>
> >> cfgval = loadConfig () || DEFAULT_VAL;
>
> >> In C '||' operator does not return the value of the expression which
> >> is true, but returns 1 or 0, so that I can not be used in a similar
> >> way. I considered a program in C is:
>
> >> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
>
> >> And however, this approach to involve a duplication of code, it is
> >> ugly, the other will call loadConfig function twice, inefficient and
> >> likely to cause unnecessary accidents.
>
> >> Of course, if / else statement can achieve this functionality, but
> >> this method requires an additional variable, and not look simple. As
> >> follows:
>
> >> int loadval = loadConfig ();
> >> if (loadval! = 0) {
> >>     cfgval = loadval;
> >> } else {
> >>     cfgval = DEFAULT_VAL;
> >> }
>
> >> Or
>
> >> int loadval;
> >> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
>
> >> I would like to ask you is, in this case, there is nothing more
> >> beautiful realization?
>
> >      cfgval = loadConfig ();
> >      if (cfgval == 0) {
> >          cfgval = DEFAULT_VAL;
> >      }
>
> pete,
>
> you just posted another syntax other than c tonight in clc; watch it,
> there, revolutionary.

in what way is that not C syntax?

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


#10938

From"J. J. Farrell" <jjf@bcs.org.uk>
Date2011-09-26 04:40 +0100
Message-ID<j5os78$fm7$1@dont-email.me>
In reply to#10902
Uno wrote:
> On 9/24/2011 4:48 AM, pete wrote:
>> Ya Shou wrote:
>>>
>>> Hello,
>>>
>>> I want to assign an uncertain value to a variable, this variable with
>>> values in the configuration file, reads the configuration file
>>> through a function to return to the read configuration value, if not
>>> configured or the value of non-compliance, will return 0 for failure,
>>> when this, I will to give it a default value assigned.
>>>
>>> However, I found it seem impossible to achieve this in C, but much
>>> easier to implement in Perl, see below:
>>>
>>> cfgval = loadConfig () || DEFAULT_VAL;
>>>
>>> In C '||' operator does not return the value of the expression which
>>> is true, but returns 1 or 0, so that I can not be used in a similar
>>> way. I considered a program in C is:
>>>
>>> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
>>>
>>> And however, this approach to involve a duplication of code, it is
>>> ugly, the other will call loadConfig function twice, inefficient and
>>> likely to cause unnecessary accidents.
>>>
>>> Of course, if / else statement can achieve this functionality, but
>>> this method requires an additional variable, and not look simple. As
>>> follows:
>>>
>>> int loadval = loadConfig ();
>>> if (loadval! = 0) {
>>>     cfgval = loadval;
>>> } else {
>>>     cfgval = DEFAULT_VAL;
>>> }
>>>
>>> Or
>>>
>>> int loadval;
>>> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
>>>
>>> I would like to ask you is, in this case, there is nothing more
>>> beautiful realization?
>>
>>      cfgval = loadConfig ();
>>      if (cfgval == 0) {
>>          cfgval = DEFAULT_VAL;
>>      }
>>
> 
> pete,
> 
> you just posted another syntax other than c tonight in clc; watch it, 
> there, revolutionary.

Where? When?

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


#10856

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-09-24 09:04 -0400
Message-ID<j5kkg3$io8$1@dont-email.me>
In reply to#10844
On 9/24/2011 7:06 AM, Ya Shou wrote:
> Hello,
>
>
> I want to assign an uncertain value to a variable, this variable with
> values in the configuration file, reads the configuration file
> through a function to return to the read configuration value, if not
> configured or the value of non-compliance, will return 0 for failure,
> when this, I will to give it a default value assigned.
> [...]

     Others have suggested ways to write this.  But is it a good
idea?  It leaves you with no way for the configuration file to
supply zero as an actual value, which may be satisfactory now
but could become troublesome later.

     "In-band" signalling has a long history, and you'll find it
used a lot in the C library and in C programs.  The malloc()
function returns NULL to indicate a special case, and this works
because NULL is easily distinguishable from any valid pointer.
strchr(), getenv(), and many others use NULL in a similar way.
But with functions returning other types it's not always so easy
to choose an "impossible" value.  time() and several other time-
related functions return -1 when something's wrong, but on most
systems the value -1 could easily be understood as a perfectly
valid time, often 1969-12-31 23:59:59.  fgetc() returns the value
EOF, which is supposed to be different from all actual character
values -- but this makes trouble on systems with wide characters
and narrow integers, or on ones' complement or signed magnitude
systems where `char' is signed.  strtol() uses three different
"impossible" values, *all* of them perfectly legitimate!

     In short, using a special value to mean "absent" or "invalid"
may seem convenient, but can be less convenient than it appears
at first.  Instead of

	cfgval = loadConfig();
	if (cfgval == SPECIAL_VALUE)
	    cfgval = DEFAULT_VALUE;

consider something more like

	if (!loadConfig(&cfgval))
	    cfgval = DEFAULT_VALUE;

where loadConfig() tries to load the value and store it at the
indicated place, and returns 1 or 0 to indicate success or failure.
Or, if you prefer, use

	cfgval = loadConfig(DEFAULT_VALUE);

where loadConfig() loads and returns the value from the configuration
file, or returns its own argument if unable to load anything.  You
could even put DEFAULT_VALUE into loadConfig() itself, producing

	cfgval = loadConfig();

where loadConfig() loads and returns the file's value if it can, or
returns DEFAULT_VALUE if unable.

     But think long and hard and critically before using SPECIAL_VALUE.
It can become a snare and a delusion.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

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


#10862

FromWillem <willem@toad.stack.nl>
Date2011-09-24 14:06 +0000
Message-ID<slrnj7rour.gnu.willem@toad.stack.nl>
In reply to#10856
Eric Sosman wrote:
) Or, if you prefer, use
)
) 	cfgval = loadConfig(DEFAULT_VALUE);
)
) where loadConfig() loads and returns the value from the configuration
) file, or returns its own argument if unable to load anything.  You
) could even put DEFAULT_VALUE into loadConfig() itself, producing
)
) 	cfgval = loadConfig();
)
) where loadConfig() loads and returns the file's value if it can, or
) returns DEFAULT_VALUE if unable.

I prefer the first, because of the more real-world generic version:
  cfgval = loadConfig('foo_num', FOO_DEFAULT_VALUE);

Which I've used plenty of times.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

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


#10994

FromYa Shou <shouyatf@gmail.com>
Date2011-09-26 21:10 -0700
Message-ID<80c3d242-acbf-4edd-89b0-192c677049ec@w15g2000vbe.googlegroups.com>
In reply to#10856
On Sep 24, 9:04 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> On 9/24/2011 7:06 AM, Ya Shou wrote:
>
> > Hello,
>
> > I want to assign an uncertain value to a variable, this variable with
> > values in the configuration file, reads the configuration file
> > through a function to return to the read configuration value, if not
> > configured or the value of non-compliance, will return 0 for failure,
> > when this, I will to give it a default value assigned.
> > [...]
>
>      Others have suggested ways to write this.  But is it a good
> idea?  It leaves you with no way for the configuration file to
> supply zero as an actual value, which may be satisfactory now
> but could become troublesome later.
>
>      "In-band" signalling has a long history, and you'll find it
> used a lot in the C library and in C programs.  The malloc()
> function returns NULL to indicate a special case, and this works
> because NULL is easily distinguishable from any valid pointer.
> strchr(), getenv(), and many others use NULL in a similar way.
> But with functions returning other types it's not always so easy
> to choose an "impossible" value.  time() and several other time-
> related functions return -1 when something's wrong, but on most
> systems the value -1 could easily be understood as a perfectly
> valid time, often 1969-12-31 23:59:59.  fgetc() returns the value
> EOF, which is supposed to be different from all actual character
> values -- but this makes trouble on systems with wide characters
> and narrow integers, or on ones' complement or signed magnitude
> systems where `char' is signed.  strtol() uses three different
> "impossible" values, *all* of them perfectly legitimate!
>
>      In short, using a special value to mean "absent" or "invalid"
> may seem convenient, but can be less convenient than it appears
> at first.  Instead of
>
>         cfgval = loadConfig();
>         if (cfgval == SPECIAL_VALUE)
>             cfgval = DEFAULT_VALUE;

This is a better way to do that, but twice assignment maybe
superfluous.

>
> consider something more like
>
>         if (!loadConfig(&cfgval))
>             cfgval = DEFAULT_VALUE;
>

I think that is a perfect solution. beautiful and so efficient.

> where loadConfig() tries to load the value and store it at the
> indicated place, and returns 1 or 0 to indicate success or failure.
> Or, if you prefer, use
>
>         cfgval = loadConfig(DEFAULT_VALUE);
>
> where loadConfig() loads and returns the value from the configuration
> file, or returns its own argument if unable to load anything.  You
> could even put DEFAULT_VALUE into loadConfig() itself, producing
>
>         cfgval = loadConfig();
>
> where loadConfig() loads and returns the file's value if it can, or
> returns DEFAULT_VALUE if unable.
>
>      But think long and hard and critically before using SPECIAL_VALUE.
> It can become a snare and a delusion.
>
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid

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


#11090

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-09-29 22:08 +0000
Message-ID<slrnj89r30.1bk.grahn+nntp@frailea.sa.invalid>
In reply to#10856
On Sat, 2011-09-24, Eric Sosman wrote:
...
>      In short, using a special value to mean "absent" or "invalid"
> may seem convenient, but can be less convenient than it appears
> at first.  Instead of
>
> 	cfgval = loadConfig();
> 	if (cfgval == SPECIAL_VALUE)
> 	    cfgval = DEFAULT_VALUE;
>
> consider something more like
>
> 	if (!loadConfig(&cfgval))
> 	    cfgval = DEFAULT_VALUE;

I'd like to recommend

 	int cfgval = loadConfig(&error);
 	if (error)
 	    cfgval = DEFAULT_VALUE;

which lets you initialize 'cfgval' at its declaration (and in may
cases make it const).

>      But think long and hard and critically before using SPECIAL_VALUE.
> It can become a snare and a delusion.

Agreed.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

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


#10937

From"BartC" <bc@freeuk.com>
Date2011-09-26 01:37 +0100
Message-ID<j5ohha$uod$1@dont-email.me>
In reply to#10844
"Ya Shou" <shouyatf@gmail.com> wrote in message 
news:a2b7378d-3156-4f3c-ad6a-1bf14330d993@5g2000yqo.googlegroups.com...

> However, I found it seem impossible to achieve this in C, but much
> easier to implement in Perl, see below:
>
> cfgval = loadConfig () || DEFAULT_VAL;

> I would like to ask you is, in this case, there is nothing more
> beautiful realization?

Can you change loadConfig()? If so, just do:

 cfgval = loadConfig(DEFAULT_VAL);

If you can't change it, use a wrapper:

 cfgval = your_loadConfig(DEFAULT_VAL);

or just:

 cfgval = your_loadConfig();

-- 
Bartc 

[toc] | [prev] | [standalone]


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


csiph-web