how printf(100) works in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aswal
    New Member
    • Aug 2013
    • 38

    how printf(100) works in C

    The prototype of printf() is

    Code:
    printf(const char *__format, ...);
    and i typed printf(100) and it run successfully printing a string. How it happened why it doesn't show a type mismatched error.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    printf has a char* as the first argument. If your compiler accepts 100 as valid, then I would get another compiler.

    printf(100) does not compile without a cast using Visual Studio 2013.

    Comment

    • aswal
      New Member
      • Aug 2013
      • 38

      #3
      I am using turbo C++ when i run it as a C program then it works but when i run it as a C++ program then it shows the error which should be shown.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Was there a prototype for printf in scope?
        That is, did you include <stdio.h> before calling printf(100)?

        Comment

        • aswal
          New Member
          • Aug 2013
          • 38

          #5
          No because in C we don't have to include header files. On including those it is showing the error. But I want to know why it works when we did not gave it's prototype and the output it gave.

          Also, was the approach of function prototype was present in C or it came in C++

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            In C you so not need to have a function prototype. You don't need to include stdio.h.

            If you do this, then printf will be considered an external function that returns an int. If your makefile includes the printf library, which is probably does, then the 100 is the char* argument and your build will be OK. When you run the program, you crash trying to access memory location 100 which is not in your process.

            Stuff like this is why C++ was started in the first place.

            Comment

            • androidapp
              New Member
              • Jun 2014
              • 10

              #7
              Format is a pointer, pointer accepts integer value.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                The original question was why didn't printf(100) provoke a compiler error. The answer is that without a prototype, the compiler had no reason to think there was anything wrong with printf(100).

                The C Standard allows you to call a function without using a prototype. You can also hike into the desert without any water. The only thing stopping you from doing either of those things is a common-sense concern for the consequences.

                Comment

                Working...