Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what happens in following code exactly? [modified]

what happens in following code exactly? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sam_psycho
    wrote on last edited by
    #1

    what happens with if condition in following code?

    int function(int i)
    {
    printf("inside function");
    return 0;
    }

    int main()
    {
    if(function)
    printf("hi");
    else
    printf("bye");
    }

    and output is only hi why compiler doesn't complaints about wrong function call? :confused:

    modified on Thursday, July 23, 2009 12:08 PM

    C I J 3 Replies Last reply
    0
    • S sam_psycho

      what happens with if condition in following code?

      int function(int i)
      {
      printf("inside function");
      return 0;
      }

      int main()
      {
      if(function)
      printf("hi");
      else
      printf("bye");
      }

      and output is only hi why compiler doesn't complaints about wrong function call? :confused:

      modified on Thursday, July 23, 2009 12:08 PM

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      What is fun in your code? Is that suposed ot be:

      if (function) ...

      ? If so, then the answer is simple, in your if the address of the function will be checked against zero, and since it is not zero, it will come out as TRUE thus, you get "hi".

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

      S 1 Reply Last reply
      0
      • C Code o mat

        What is fun in your code? Is that suposed ot be:

        if (function) ...

        ? If so, then the answer is simple, in your if the address of the function will be checked against zero, and since it is not zero, it will come out as TRUE thus, you get "hi".

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

        S Offline
        S Offline
        sam_psycho
        wrote on last edited by
        #3

        yes.. it's

        if(function)

        and thanx ..I got it.

        1 Reply Last reply
        0
        • S sam_psycho

          what happens with if condition in following code?

          int function(int i)
          {
          printf("inside function");
          return 0;
          }

          int main()
          {
          if(function)
          printf("hi");
          else
          printf("bye");
          }

          and output is only hi why compiler doesn't complaints about wrong function call? :confused:

          modified on Thursday, July 23, 2009 12:08 PM

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          Your function doesn't get called. I think you mean:

          if (function (1234))
          ...

          But instead you're checking if the pointer to function is not 0, which it isn't, so it counts as true, so you get "hi". Iain.

          I have now moved to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need cotract work done, give me a job! http://cv.imcsoft.co.uk/[^]

          1 Reply Last reply
          0
          • S sam_psycho

            what happens with if condition in following code?

            int function(int i)
            {
            printf("inside function");
            return 0;
            }

            int main()
            {
            if(function)
            printf("hi");
            else
            printf("bye");
            }

            and output is only hi why compiler doesn't complaints about wrong function call? :confused:

            modified on Thursday, July 23, 2009 12:08 PM

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            In more detail, a procedural function name in C/C++ is a pointer to a function (a memory address). This is how you can pass one function to another function (like with the CRT qsort() function.) So in your if statement, you are checking whether function exists, which it does. To call a function, you have to use the parentheses syntax, i.e. function(1).

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups