odd compiler behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yakir22@gmail.com

    odd compiler behaviour

    hi all,
    can anyone explain me please why this line of code doesn't generate
    any compiler error ( both on vs2005 and kdevelop ).

    printf("%s") , "aaa";

    this look totaly wrong ( or am i mistaken ).

    Thanks in advance.
  • Alex

    #2
    Re: odd compiler behaviour

    On 9ÔÂ17ÈÕ, ÏÂÎç4ʱ04·Ö, yaki...@gmail.c om wrote:
    hi all,
    can anyone explain me please why this line of code doesn't generate
    any compiler error ( both on vs2005 and kdevelop ).
    >
    printf("%s") , "aaa";
    >
    this look totaly wrong ( or am i mistaken ).
    >
    Thanks in advance.
    It's correct, 'Cause compiler view "," as operator.

    Comment

    • Juha Nieminen

      #3
      Re: odd compiler behaviour

      Michael DOUBEZ wrote:
      >printf("%s") , "aaa";
      >
      It is syntactically acceptable.
      This line executes two instructions:
      printf("%s");
      "aaa";
      Note, however, that the original and those two instructions are not
      the same thing. The original is one single expression which value is a
      const char*.

      Comment

      • Default User

        #4
        Re: odd compiler behaviour

        Pascal J. Bourguignon wrote:
        yakir22@gmail.c om writes:
        >
        hi all,
        can anyone explain me please why this line of code doesn't generate
        any compiler error ( both on vs2005 and kdevelop ).

        printf("%s") , "aaa";
        Therefore printf("%s"),"a aa" is a valid expression,
        It is syntactically valid, but not semantically.
        therefore printf("%s"),"a aa"; is a valid statement;
        Depending on your definition of "valid". I would not call it valid,
        although there is no requirement for a diagnostic.
        it calls the function printf with as argument a pointer to a vector of
        three characters '%', 's', 0.
        You mean an array. As vector is a standard library construct in C++,
        that distinction is important.
        and then it results in a pointer to a
        vector of four characters, 'a', 'a', 'a' and 0, which it promptly
        ignores.
        Again, array.

        As calling printf() with insufficient arguments for the format results
        in undefined behavior, there's no requirement for anything specific to
        happen after the function call. So it need not evaluate the second
        expression at all.




        Brian

        Comment

        • peter koch

          #5
          Re: odd compiler behaviour

          On 17 Sep., 10:04, yaki...@gmail.c om wrote:
          hi all,
          can anyone explain me please why this line of code doesn't generate
          any compiler error ( both on vs2005 and kdevelop ).
          >
          printf("%s") , "aaa";
          >
          this look totaly wrong ( or am i mistaken ).
          >
          Thanks in advance.
          As others have pointed out, the syntax is fine. But I would expect a
          compiler warning here, and recommend that you play with your compilers
          settings: most likely you will be able to get the compiler to produce
          a warning.

          /Peter

          Comment

          • Juha Nieminen

            #6
            Re: odd compiler behaviour

            Default User wrote:
            >Therefore printf("%s"),"a aa" is a valid expression,
            >
            It is syntactically valid, but not semantically.
            Are you referring to the printf() calling syntax being incorrect (ie.
            too few parameters)?

            Actually, technically speaking, we don't know if it's semantically
            incorrect or not. It may well be that 'printf' is a user-defined
            function which takes just a const char*, in which case that expression
            is completely valid, even semantically. (Moreover 'printf' might be also
            an instance of a class which has an operator() member function defined,
            ie. a functor.)

            Comment

            • Default User

              #7
              Re: odd compiler behaviour

              Juha Nieminen wrote:
              Default User wrote:
              Therefore printf("%s"),"a aa" is a valid expression,
              It is syntactically valid, but not semantically.
              >
              Are you referring to the printf() calling syntax being incorrect
              (ie. too few parameters)?
              >
              Actually, technically speaking, we don't know if it's semantically
              incorrect or not. It may well be that 'printf' is a user-defined
              function which takes just a const char*, in which case that expression
              is completely valid, even semantically. (Moreover 'printf' might be
              also an instance of a class which has an operator() member function
              defined, ie. a functor.)
              I think that in all cases where a function is used that has the name of
              a standard library function, it must be assumed that it is being
              referenced unless there has been a definite statement to the contrary.



              Brian

              Comment

              Working...