preprocessing statements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    #16
    Re: preprocessing statements


    "Walter Roberson" <roberson@ibd.n rc-cnrc.gc.cawrote in message
    news:g1hn6u$nhj $1@canopus.cc.u manitoba.ca...
    In this thread you did not ask about #if vs #ifdef . They do not mean
    the same thing. For example,
    >
    #define FOO 0
    #ifdef FOO
    /* this section *will* be compiled, because FOO -is- defined. */
    #endif
    #if FOO
    /* this section will *not* be compiled, because FOO's value is 0,
    and #if 0 is false */
    #endif
    #ifdef if
    /* this section will *not* be compiled, because the macro if is -not-
    defined. */
    #endif
    #if !if
    /* this section *will* be compiled. In a #if line, after all known
    macros are expanded, all remaining identifiers have the value 0
    substituted, without any consideration as to whether the identifiers
    might be C keywords or library functions. With no macro named if,
    the line would be equivilent to #if !0 which is #if 1 which is true.
    */
    #endif
    Priceless.

    Bill


    Comment

    • Keith Thompson

      #17
      Re: preprocessing statements

      "Bill Cunningham" <nospam@nspam.c omwrites:
      I have been thinking about hiding headers from my compiler's
      preprocessor and allowing others to be shown. I want to use the most used
      and add others is necessary. Would this be how it is properly done. I want
      to ask ahead of time because what I do might work but it might not be the
      "correct" or standard way.
      >
      #include <stdio.h>
      #include <stdlib.h>
      #ifdef LINUX
      #include <unistd.h>
      #include <sys/types>
      #include <sys/stats.h>
      #include <fctnl.h>
      #endif
      >
      Is the #endif needed at the end of this case ?
      One more thing that you didn't ask about (but probably should have):

      How do you expect the symbol LINUX to be defined? Since that
      identifier must be available for use in programs, an implementation is
      not allowed to pre-define it. There may be other symbols that are
      predefined (if you're trying to determine whether you're on a Linux
      system); which symbols those might be is a question for another forum.

      If you're defining it yourself, that's fine, but you didn't show that.

      Also, you appear to be trying to write code that will use things
      declared in those implementation-defined headers if you're on a Linux
      system, but will still compile and work properly otherwise. Such
      things are doable, but the nature of the questions you've been asking
      here suggest to me that this is a more advanced topic than you're
      ready for.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Keith Thompson

        #18
        Re: preprocessing statements

        "Bill Cunningham" <nospam@nspam.c omwrites:
        "santosh" <santosh.k83@gm ail.comwrote in message
        news:g1hl26$rje $1@registered.m otzarella.org.. .
        >
        >>>Seriously though, no preprocessing directives are perfectly topical.
        >>>vippstar was talking about the various system specific headers that
        >>>you had included in your code. Those _are_ OT and I believe better
        >>>addressed in comp.unix.progr ammer. But your actual question can be
        >>>answered here and Walter Roberson did so. Also your method to
        >>>conditionall y compile the include directives is perfectly fine.
        >>>That's how it's done.
        [snip]
        >
        So then is it ok to talk about and use examples of #if and #ifdef and so
        on. Or is preprocessing directives OT ?
        Preprocessing directives are topical. In my opinion, the person who
        complained that your orginal post was off-topic overreacted.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • D. Power

          #19
          Re: preprocessing statements

          In article <g1hn6u$nhj$1@c anopus.cc.umani toba.ca>,
          roberson@ibd.nr c-cnrc.gc.ca (Walter Roberson) wrote:

          <snip>
          #ifdef if
          /* this section will *not* be compiled, because the macro if is -not-
          defined. */
          #endif
          This seems to be an odd use of a keyword, but it seems to work.

          #include <stdio.h>

          #define if

          int main (void)
          {
          #ifdef if
          puts ("It seems to be ok.");
          #else
          puts ("No go.");
          #endif

          return 0;
          }

          Compiles with gcc with the -ansi -pedantic -W -Wall -Wextra switches
          with no complaints; it just doesn't look right.

          <snip>
          --
          D. Power
          "I hold it to be the inalienable right of anybody to go to hell in his
          own way."
          -Robert Frost

          Comment

          • Keith Thompson

            #20
            Re: preprocessing statements

            "D. Power" <powerd@pcisys. net.invalidwrit es:
            In article <g1hn6u$nhj$1@c anopus.cc.umani toba.ca>,
            roberson@ibd.nr c-cnrc.gc.ca (Walter Roberson) wrote:
            <snip>
            >#ifdef if
            > /* this section will *not* be compiled, because the macro if is -not-
            > defined. */
            >#endif
            >
            This seems to be an odd use of a keyword, but it seems to work.
            >
            #include <stdio.h>
            >
            #define if
            >
            int main (void)
            {
            #ifdef if
            puts ("It seems to be ok.");
            #else
            puts ("No go.");
            #endif
            >
            return 0;
            }
            >
            Compiles with gcc with the -ansi -pedantic -W -Wall -Wextra switches
            with no complaints; it just doesn't look right.
            "if" isn't a keyword as far as the preprocessor is concerned; it's
            just another identifier. Though I agree that the use of "if" in this
            context is ugly.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            • Walter Roberson

              #21
              Re: preprocessing statements

              In article <lnd4n7qx3n.fsf @nuthaus.mib.or g>,
              Keith Thompson <kst-u@mib.orgwrote:
              >"D. Power" <powerd@pcisys. net.invalidwrit es:
              >In article <g1hn6u$nhj$1@c anopus.cc.umani toba.ca>,
              > roberson@ibd.nr c-cnrc.gc.ca (Walter Roberson) wrote:
              >>#ifdef if
              >> /* this section will *not* be compiled, because the macro if is -not-
              >> defined. */
              >>#endif
              >This seems to be an odd use of a keyword, but it seems to work.
              >"if" isn't a keyword as far as the preprocessor is concerned; it's
              >just another identifier. Though I agree that the use of "if" in this
              >context is ugly.
              Keith is correct on both counts -- the preprocesor doesn't know
              anything about "keywords" or library functions, or about any of
              the types other than the basic arithmetic types of various
              sizes (and, I suppose, size_t).

              And yes, it is ugly -- its name was chosen specifically to illustrate
              the point that *everything* that looks like an identifier (but which
              is not a macro), will have 0 substituted in an #if preprocessor statement.

              Further example:

              #if the + world + is *round* why + do + we + ~ fall + off + while + we + stand + on + our + heads + at + night ? Author : unknown
              /* something */
              #endif


              If I haven't overlooked anything, that should be compilable even if
              nothing is #define'd, even though it appears to use the C keywords
              if and do and while .
              --
              "When a scientist is ahead of his times, it is often through
              misunderstandin g of current, rather than intuition of future truth.
              In science there is never any error so gross that it won't one day,
              from some perspective, appear prophetic." -- Jean Rostand

              Comment

              • Bill Cunningham

                #22
                Re: preprocessing statements


                "Keith Thompson" <kst-u@mib.orgwrote in message
                news:lnprr7r0xp .fsf@nuthaus.mi b.org...
                "Bill Cunningham" <nospam@nspam.c omwrites:
                > I have been thinking about hiding headers from my compiler's
                >preprocessor and allowing others to be shown. I want to use the most used
                >and add others is necessary. Would this be how it is properly done. I
                >want
                >to ask ahead of time because what I do might work but it might not be the
                >"correct" or standard way.
                >>
                >#include <stdio.h>
                >#include <stdlib.h>
                >#ifdef LINUX
                >#include <unistd.h>
                >#include <sys/types>
                >#include <sys/stats.h>
                >#include <fctnl.h>
                >#endif
                >>
                > Is the #endif needed at the end of this case ?
                >
                One more thing that you didn't ask about (but probably should have):
                >
                How do you expect the symbol LINUX to be defined? Since that
                identifier must be available for use in programs, an implementation is
                not allowed to pre-define it. There may be other symbols that are
                predefined (if you're trying to determine whether you're on a Linux
                system); which symbols those might be is a question for another forum.
                >
                If you're defining it yourself, that's fine, but you didn't show that.
                >
                Also, you appear to be trying to write code that will use things
                declared in those implementation-defined headers if you're on a Linux
                system, but will still compile and work properly otherwise. Such
                things are doable, but the nature of the questions you've been asking
                here suggest to me that this is a more advanced topic than you're
                ready for.
                >
                Perhaps so but the code above didn't work. This code I wrote did. The
                question is did I do it correctly.

                #include <stdio.h>
                #include <stdlib.h>
                #ifdef linux
                #ifndef linux
                #undef linux
                #include <unistd.h>
                #include <sys/types.h>
                #include <sys/stats.h>
                #include <fctnl.h>
                #endif
                #endif

                I compiled a simple program asking sizeof to show through printf the
                size of a long double. I haven't defined linux to use those sys call headers
                yet though so that part of the header is untested. But the directives
                blocked out the mentioned OT headers to compile a simple C program. I
                must've did something right.

                Bill


                Comment

                • Keith Thompson

                  #23
                  Re: preprocessing statements

                  "Bill Cunningham" <nospam@nspam.c omwrites:
                  "Keith Thompson" <kst-u@mib.orgwrote in message
                  news:lnprr7r0xp .fsf@nuthaus.mi b.org...
                  >"Bill Cunningham" <nospam@nspam.c omwrites:
                  >> I have been thinking about hiding headers from my compiler's
                  >>preprocesso r and allowing others to be shown. I want to use the most used
                  >>and add others is necessary. Would this be how it is properly done. I
                  >>want
                  >>to ask ahead of time because what I do might work but it might not be the
                  >>"correct" or standard way.
                  >>>
                  >>#include <stdio.h>
                  >>#include <stdlib.h>
                  >>#ifdef LINUX
                  >>#include <unistd.h>
                  >>#include <sys/types>
                  >>#include <sys/stats.h>
                  >>#include <fctnl.h>
                  >>#endif
                  >>>
                  >> Is the #endif needed at the end of this case ?
                  >>
                  >One more thing that you didn't ask about (but probably should have):
                  >>
                  >How do you expect the symbol LINUX to be defined? Since that
                  >identifier must be available for use in programs, an implementation is
                  >not allowed to pre-define it. There may be other symbols that are
                  >predefined (if you're trying to determine whether you're on a Linux
                  >system); which symbols those might be is a question for another forum.
                  >>
                  >If you're defining it yourself, that's fine, but you didn't show that.
                  >>
                  >Also, you appear to be trying to write code that will use things
                  >declared in those implementation-defined headers if you're on a Linux
                  >system, but will still compile and work properly otherwise. Such
                  >things are doable, but the nature of the questions you've been asking
                  >here suggest to me that this is a more advanced topic than you're
                  >ready for.
                  >>
                  Perhaps so but the code above didn't work. This code I wrote did. The
                  question is did I do it correctly.
                  >
                  #include <stdio.h>
                  #include <stdlib.h>
                  #ifdef linux
                  #ifndef linux
                  #undef linux
                  #include <unistd.h>
                  #include <sys/types.h>
                  #include <sys/stats.h>
                  #include <fctnl.h>
                  #endif
                  #endif
                  >
                  I compiled a simple program asking sizeof to show through printf the
                  size of a long double. I haven't defined linux to use those sys call headers
                  yet though so that part of the header is untested. But the directives
                  blocked out the mentioned OT headers to compile a simple C program. I
                  must've did something right.
                  Yes, it should work; it just doesn't make any sense.

                  You have a "#ifndef linux" nested within a "#ifdef linux". What is
                  this supposed to accomplish? What is the purpose of the "#undef
                  linux"?

                  Under what circumstances, if any, do you expect the symbol "linux" or
                  "LINUX" to be defined? Do you intend to define it yourself?

                  Do you intend to write code that uses things declared in those
                  headers? Is your program going to *depend* on those features?

                  Based on what you've told us so far, a much simpler solution would be
                  to omit the #include directives for the system-specific headers,
                  leaving just this:

                  #include <stdio.h>
                  #include <stdlib.h>

                  What exactly are you trying to accomplish?

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Bill Cunningham

                    #24
                    Re: preprocessing statements


                    "Keith Thompson" <kst-u@mib.orgwrote in message
                    news:ln4p8jqswy .fsf@nuthaus.mi b.org...

                    Yes, it should work; it just doesn't make any sense.
                    >
                    You have a "#ifndef linux" nested within a "#ifdef linux". What is
                    this supposed to accomplish? What is the purpose of the "#undef
                    linux"?
                    >
                    Under what circumstances, if any, do you expect the symbol "linux" or
                    "LINUX" to be defined? Do you intend to define it yourself?
                    >
                    Do you intend to write code that uses things declared in those
                    headers? Is your program going to *depend* on those features?
                    Yes I use them too. But they are OT here. I want to write something like
                    this.

                    #include "master.h"
                    #define linux

                    Then use those non-standard headers with stdio.h and stdlib.h. Otherwise if
                    linux is not defined.

                    #include "master.h"

                    int main(void) {

                    C code.

                    }


                    Comment

                    • Keith Thompson

                      #25
                      Re: preprocessing statements

                      "Bill Cunningham" <nospam@nspam.c omwrites:
                      "Keith Thompson" <kst-u@mib.orgwrote in message
                      news:ln4p8jqswy .fsf@nuthaus.mi b.org...
                      >Yes, it should work; it just doesn't make any sense.
                      >>
                      >You have a "#ifndef linux" nested within a "#ifdef linux". What is
                      >this supposed to accomplish? What is the purpose of the "#undef
                      >linux"?
                      >>
                      >Under what circumstances, if any, do you expect the symbol "linux" or
                      >"LINUX" to be defined? Do you intend to define it yourself?
                      >>
                      >Do you intend to write code that uses things declared in those
                      >headers? Is your program going to *depend* on those features?
                      >
                      Yes I use them too. But they are OT here. I want to write something like
                      this.
                      >
                      #include "master.h"
                      #define linux
                      >
                      Then use those non-standard headers with stdio.h and stdlib.h. Otherwise if
                      linux is not defined.
                      >
                      #include "master.h"
                      >
                      int main(void) {
                      >
                      C code.
                      >
                      }
                      Based on what you've told us so far, I'm unable to figure out what
                      you're trying to do. I asked you several specific questions; you've
                      answered almost none of them. Rather than telling us what you're
                      trying to accomplish, you've shown us some pseudo-code of what I
                      presume is intended to be a solution.

                      What problem are you trying to solve?

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      Nokia
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • Bill Cunningham

                        #26
                        Re: preprocessing statements


                        "Keith Thompson" <kst-u@mib.orgwrote in message
                        news:lnwslfp9d2 .fsf@nuthaus.mi b.org...
                        What problem are you trying to solve?
                        >
                        In all compilation files include stdio.h and stdlib. When linux is
                        defined include several non-standard headers at compile time.

                        Bill


                        Comment

                        • D. Power

                          #27
                          Re: preprocessing statements

                          In article <lnd4n7qx3n.fsf @nuthaus.mib.or g>,
                          Keith Thompson <kst-u@mib.orgwrote:
                          "D. Power" <powerd@pcisys. net.invalidwrit es:
                          In article <g1hn6u$nhj$1@c anopus.cc.umani toba.ca>,
                          roberson@ibd.nr c-cnrc.gc.ca (Walter Roberson) wrote:
                          <snip>
                          #ifdef if
                          /* this section will *not* be compiled, because the macro if is -not-
                          defined. */
                          #endif
                          This seems to be an odd use of a keyword, but it seems to work.

                          #include <stdio.h>

                          #define if

                          int main (void)
                          {
                          #ifdef if
                          puts ("It seems to be ok.");
                          #else
                          puts ("No go.");
                          #endif

                          return 0;
                          }

                          Compiles with gcc with the -ansi -pedantic -W -Wall -Wextra switches
                          with no complaints; it just doesn't look right.
                          >
                          "if" isn't a keyword as far as the preprocessor is concerned; it's
                          just another identifier. Though I agree that the use of "if" in this
                          context is ugly.
                          I see, time to read more closely on translation phases in the Standard.
                          Thank you, and Walter Roberson, for the reply.

                          --
                          D. Power
                          "I hold it to be the inalienable right of anybody to go to hell in his
                          own way."
                          -Robert Frost

                          Comment

                          • Keith Thompson

                            #28
                            Re: preprocessing statements

                            "Bill Cunningham" <nospam@nspam.c omwrites:
                            "Keith Thompson" <kst-u@mib.orgwrote in message
                            news:lnwslfp9d2 .fsf@nuthaus.mi b.org...
                            >What problem are you trying to solve?
                            >>
                            In all compilation files include stdio.h and stdlib. When linux is
                            defined include several non-standard headers at compile time.
                            No, that's your proposed *solution*. What *problem* are you trying to
                            solve? How is your program supposed to behave?

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            • Bill Cunningham

                              #29
                              Re: preprocessing statements

                              No, that's your proposed *solution*. What *problem* are you trying to
                              solve? How is your program supposed to behave?
                              I want one header called master.h that includes all the headers that
                              have been typed. I am tired of typing them all in one at a time. I wish to
                              have a header with conditional compilation.

                              Bill


                              Comment

                              • vippstar@gmail.com

                                #30
                                Re: preprocessing statements

                                On May 28, 4:19 am, "Bill Cunningham" <nos...@nspam.c omwrote:
                                No, that's your proposed *solution*. What *problem* are you trying to
                                solve? How is your program supposed to behave?
                                >
                                I want one header called master.h that includes all the headers that
                                have been typed. I am tired of typing them all in one at a time.
                                Use a proper text editor that does this for you.
                                clc is not a group to ask about text editors.

                                Comment

                                Working...