Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: why "if (0 && foo)"?

  1. #1
    Join Date
    Aug 2005
    Location
    Fargo, ND, USA
    Beans
    1,499
    Distro
    Kubuntu 10.04 Lucid Lynx

    why "if (0 && foo)"?

    I was reading some C code today, and I saw something like this:
    Code:
    if (0 && foo)
        bar(foo);
    What is the point of the zero? "foo" wasn't anything like a macro, if it didn't exist it would have been a compiler error (to be more precise, it looked more like foo->count, where count is an int). I would be inclined to think the compiler would optimize out the "0 &&". I looked around on the web a bit for an explanation, and found plenty of examples but no rational. I am thoroughly confused.
    Help yourself: Search the community docs or try other resources.
    Quote Originally Posted by Henry Spencer
    Those who do not understand Unix are condemned to reinvent it, poorly.
    Let science use your computer when you aren't: Folding@Home.

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: why "if (0 && foo)"?

    Quote Originally Posted by jpkotta View Post
    I was reading some C code today, and I saw something like this:
    Code:
    if (0 && foo)
        bar(foo);
    What is the point of the zero? "foo" wasn't anything like a macro, if it didn't exist it would have been a compiler error (to be more precise, it looked more like foo->count, where count is an int). I would be inclined to think the compiler would optimize out the "0 &&". I looked around on the web a bit for an explanation, and found plenty of examples but no rational. I am thoroughly confused.
    Some more of the C code would help.

    I don't know why the 0 would be there, and a little more on what foo is is needed.

  3. #3
    Join Date
    Aug 2005
    Location
    Fargo, ND, USA
    Beans
    1,499
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: why "if (0 && foo)"?

    I am looking at ipkg, the Itsy Package Manager. It's dpkg+apt for embedded systems. You can get the full source from http://www.handhelds.org/pub/packages/ipkg/.

    Code:
    typedef struct pkg pkg_t;
    struct pkg
    {
         char *name;
         unsigned long epoch;
         char *version;
         char *revision;
         char *familiar_revision;
         pkg_src_t *src;
         pkg_dest_t *dest;
         char *architecture;
         char *section;
         char *maintainer;
         char *description;
         pkg_state_want_t state_want;
         pkg_state_flag_t state_flag;
         pkg_state_status_t state_status;
         char **depends_str;
         int depends_count;
         char **pre_depends_str;
         int pre_depends_count;
         char **recommends_str;
         int recommends_count;
         char **suggests_str;
         int suggests_count;
         compound_depend_t * depends;
    
         /* Abhaya: new conflicts */
         char **conflicts_str;
         compound_depend_t * conflicts;
         int conflicts_count;
    	
         char **replaces_str;
         int replaces_count;
         abstract_pkg_t ** replaces;
    
         char **provides_str;
         int provides_count;
         abstract_pkg_t ** provides;
    
         abstract_pkg_t *parent;
    
         pkg_t *old_pkg; /* during upgrade, points from installee to previously installed */
    
         char *filename;
         char *local_filename;
         char *url;
         char *tmp_unpack_dir;
         char *md5sum;
         char *size;
         char *installed_size;
         char *priority;
         char *source;
         conffile_list_t conffiles;
         time_t installed_time;
         /* As pointer for lazy evaluation */
         str_list_t *installed_files;
         /* XXX: CLEANUP: I'd like to perhaps come up with a better
    	mechanism to avoid the problem here, (which is that the
    	installed_files list was being freed from an inner loop while
    	still being used within an outer loop. */
         int installed_files_ref_cnt;
         int essential;
         int arch_priority;
    /* Adding this flag, to "force" ipkg to choose a "provided_by_hand" package, if there are multiple choice */
         int provided_by_hand;
    
         /* Check whether this pkg is being removed or installed */
         int is_processing;
         
    };
    Code:
    int buildDepends(hash_table_t * hash, pkg_t * pkg)
    {
         int count;
         register int i;
         compound_depend_t * depends;
    
         if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
    	  return 0;
    
         if (0 && pkg->pre_depends_count)
    	  fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n", 
    		  pkg->name, pkg->pre_depends_count, pkg->depends_count);
         depends = pkg->depends = malloc(sizeof(compound_depend_t) * count);
         if (depends == NULL) {
            fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
            return  -1;
         }
    
    ...
    Help yourself: Search the community docs or try other resources.
    Quote Originally Posted by Henry Spencer
    Those who do not understand Unix are condemned to reinvent it, poorly.
    Let science use your computer when you aren't: Folding@Home.

  4. #4
    Join Date
    May 2005
    Location
    Dayton, NV
    Beans
    72

    Re: why "if (0 && foo)"?

    I think it is to get the side effects of foo

  5. #5
    Join Date
    Dec 2005
    Location
    Lund, Skåne
    Beans
    580

    Re: why "if (0 && foo)"?

    The short-circuiting rules of C would make sure foo is never evalutated, and the if clause is never entered since the expression is never true. This must be a "lazy"/temporary comment-out of the following if-block, akin to the thing more often seen in preprocessor:

    Code:
    #if 0
    things here effectively commented out
    ..
    #endif

    I write Kupfer, a smart, quick launcher.
    Recommended apps and tools: Vim!

  6. #6
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: why "if (0 && foo)"?

    I think it is to get the side effects of foo
    no, because foo is never called. the only thing i can think of is that it is to disable that line (but then why didn't the author use comments?) anyway maybe if you asked the author he wouldn't know himself, we all find weirdness in our own code sometimes

  7. #7
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: why "if (0 && foo)"?

    As engla said, sometimes programmers do that during debugging as a "switch" to turn a certain block of code off. Typically they use macros because conditions will still be processed (unless the compiler optimizes it out). But it's likely to just be a toggle switch for that functionality.

  8. #8
    Join Date
    Jul 2005
    Location
    Northern CA
    Beans
    657
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: why "if (0 && foo)"?

    Quote Originally Posted by Auria View Post
    ...anyway maybe if you asked the author he wouldn't know himself, we all find weirdness in our own code sometimes
    This assumes that the author can remember why he/she did it.

    Yet another reason to comment one's code.
    Intel i7-920; Nvidia GT 220, 1GB; MSI X58 Pro-E; 6GB DDR; 64-bit mode.

  9. #9
    Join Date
    Aug 2005
    Location
    Fargo, ND, USA
    Beans
    1,499
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: why "if (0 && foo)"?

    OK, that makes sense. It's probably an error if predepends_count is zero, and the only thing inside the if() is a debug print. They probably guarantee that it is always nonzero (I remember seeing elsewhere in the code something about a package always depending on itself), thus the check is not useful in normal operation and only for testing. There are similar checks for a number of other counts in pkg. Thanks for the insight.

    And an #ifdef TESTING would have been better.
    Help yourself: Search the community docs or try other resources.
    Quote Originally Posted by Henry Spencer
    Those who do not understand Unix are condemned to reinvent it, poorly.
    Let science use your computer when you aren't: Folding@Home.

  10. #10
    Join Date
    Jul 2005
    Location
    Northern CA
    Beans
    657
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: why "if (0 && foo)"?

    This could also be a typo. Perhaps the author meant to write
    Code:
    if (0 == pkg->pre_depends_count)
    I taught CS for over twenty years. I refused to grade programming assignments that did not have comments. That came from working in industry for several years before going into teaching. I had to read other people's code (and my own code a few weeks after I wrote it).
    Intel i7-920; Nvidia GT 220, 1GB; MSI X58 Pro-E; 6GB DDR; 64-bit mode.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •