To use Perl like conditional assignment in C

Discussion in 'C Programming' started by Ya Shou, Sep 24, 2011.

  1. Ya Shou

    Ya Shou Guest

    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.
    Ya Shou, Sep 24, 2011
    #1
    1. Advertising

  2. Ya Shou

    Willem Guest

    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
    Willem, Sep 24, 2011
    #2
    1. Advertising

  3. Ya Shou

    Eric Sosman Guest

    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
    d
    Eric Sosman, Sep 24, 2011
    #3
  4. Ya Shou

    Willem Guest

    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
    Willem, Sep 24, 2011
    #4
  5. Ya Shou

    Uno Guest

    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
    Uno, Sep 25, 2011
    #5
  6. On Sep 25, 5:58 am, 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.


    in what way is that not C syntax?
    Nick Keighley, Sep 25, 2011
    #6
  7. Ya Shou

    BartC Guest

    "Ya Shou" <> wrote in message
    news:...

    > 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
    BartC, Sep 26, 2011
    #7
  8. 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?
    J. J. Farrell, Sep 26, 2011
    #8
  9. Ya Shou

    Ya Shou Guest

    On Sep 24, 9:04 pm, Eric Sosman <> 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
    >
    Ya Shou, Sep 27, 2011
    #9
  10. Ya Shou

    Uno Guest

    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
    Uno, Sep 27, 2011
    #10
  11. pete <> 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) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 28, 2011
    #11
  12. Ya Shou

    Jorgen Grahn Guest

    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 .
    Jorgen Grahn, Sep 29, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. itsme
    Replies:
    1
    Views:
    1,617
    Ralf Hildebrandt
    Jul 23, 2003
  2. Anand P Paralkar
    Replies:
    2
    Views:
    9,357
    Srinivasan Venkataramanan
    Aug 4, 2003
  3. Johnsy Joseph

    Conditional assignment to signals

    Johnsy Joseph, Sep 21, 2004, in forum: VHDL
    Replies:
    8
    Views:
    4,858
    Raghavendra
    Sep 24, 2004
  4. Replies:
    2
    Views:
    2,766
  5. Alec S.
    Replies:
    10
    Views:
    10,091
    Alec S.
    Apr 16, 2005
Loading...

Share This Page