linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Pedro de Medeiros" <pedrovmm@gmail.com>
To: Luong Ngo <luong.ngo@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Using operator ! twice
Date: Sat, 30 Jun 2007 00:24:51 -0300	[thread overview]
Message-ID: <38511efb0706292024q6463200s11d4dca1754e93ca@mail.gmail.com> (raw)
In-Reply-To: <1b2aacd80706291736s18563893q83a19d0ae5662514@mail.gmail.com>

On 6/29/07, Luong Ngo <luong.ngo@gmail.com> wrote:
> Hello,
>
> I came across several times codes that use ! operator on an integer
> variable. I am curious what is the purpose of using it twice.
> For example:
>
> int flag;
>                if( !! flag) {
>                    //do something
>                }
>
>
> The first ! operator will make !flag to be true if flag is 0, and if
> we not again it become false, which is just exactly the same if we
> just left it as
>    if(flag), since 0 is the same as false. Similar logic for non-0
> value of flag. Then why do we need to use ! operator twice to get back
> the same value as if we don't use at all? this seems to be the same
> for me if we do this in math:  -(-( -8)) = -8;


The reason for using double !s is to evaluate something as a
boolean value. I don't know why use it alone in "if ( !! num )",
but consider this code instead:

char *options[] = { "off" , "on" };

printf("Option is %s.\n", options[!!num]);

Since !!num always evaluates to 0 or 1, you don't have to
test it.


Cheers,
Pedro.

-- 
Pedro de Medeiros - Ciência da Computação - Universidade de Brasília
Home Page: http://www.nonseq.net - Linux User No.: 234250
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2007-06-30  3:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1b2aacd80706291734y55b6e654i956767ae2a6f5e46@mail.gmail.com>
2007-06-30  0:36 ` Using operator ! twice Luong Ngo
2007-06-30  3:24   ` Pedro de Medeiros [this message]
2007-07-01  1:08   ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=38511efb0706292024q6463200s11d4dca1754e93ca@mail.gmail.com \
    --to=pedrovmm@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=luong.ngo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).