Like Tree3Likes
  • 1 Post By Salem
  • 1 Post By AndrewHunter
  • 1 Post By CommonTater

What's the output

This is a discussion on What's the output within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int fun(int num); void main() { int p,n=20; p=fun(n); printf("%d",p); getch(); } int fun(int num) { (num>20)?return (100):return ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    What's the output

    Code:
    #include<stdio.h>
    int fun(int num);
    void main()
    {
    int p,n=20;
    p=fun(n);
    printf("%d",p);
    getch();
    }
    
    int fun(int num)
    {
    (num>20)?return (100):return (200);
    }
    return statement in fun() is not working proper.Can anyone please tell me what is wrong with this?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    Try

    return (num>20)?(100):(200);
    Vxyz likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2011
    Location
    Ghaziabad, India, India
    Posts
    48
    Quote Originally Posted by Salem View Post
    Try

    return (num>20)?(100)':'(200);
    Absolutely right Salem.
    And the reason is that in a program there can exist only one return keyword.
    Last edited by Vxyz; 08-02-2011 at 05:56 AM.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Additionally, void main is wrong. Read How to define main()-FAQ

    Getch is compiler specific; a more standard solution would be to use getchar();

    Learn how to indent - it's not too late!
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Vxyz View Post
    Absolutely right Salem.
    And the reason is that in a program there can exist only one return keyword.
    What silly person told you this?

    Here is a sample of live code... How many return statements do you see?
    Code:
    DWORD WINAPI ServerContact(LPVOID Parm)
      { SERVERINFO svr = {0};  // server data
        BYTE       tries;      // try counter 
        BYTE       mac[6];     // mac address
        // load user data
        if (!LoadServer(Server.Name,&svr))
          { ReportError(10002,0); 
            EnableWindow(Wind[2],1);  
            return 0; }
        Server.Pass = SetPassCode(svr.Password);
        // resolve IP
        tries = 0;
        while (! GetHostAddr(Server.Name,svr.Port,&Server.Addr))
          { if (++tries > svr.Pings)
              { ReportError(10003,0);
                EnableWindow(Wind[2],1);  
                return 0; }
            Sleep(svr.Timeout * 1000); }
         // test if server alive
         SendDatagram(Server.Addr,RM_PING,NULL,0);
         if (WaitForSingleObject(PingEvent,svr.Timeout * 1000) == WAIT_OBJECT_0)
           return 1;
        // wake host machine
        tries = 0;
        while (! GetMacAddr(Server.Addr,mac))
          { if (++tries > svr.Pings)
              { ReportError(10004,0);
                EnableWindow(Wind[2],1);  
                return 0; }
            Sleep(svr.Timeout * 1000); }
        SendMagicPacket(Server.Addr,mac);
        Sleep(100);   // pacing
        ResetEvent(PingEvent);
        // ping RMServer
        for (tries = 0; tries < svr.Pings; ++tries)
          { SendDatagram(Server.Addr,RM_PING,NULL,0);
            if (WaitForSingleObject(PingEvent,svr.Timeout * 1000) == WAIT_OBJECT_0)
              return 1; }
      ReportError(10005,0);
      EnableWindow(Wind[2],1);  
      return 0; }

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    What silly person told you this?
    Why his professor of course. Actually, I heard the ministry of education over there hands out fliers with "required knowledge" and 3.5" disks with turbo C on them.
    kpark91 likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Why his professor of course. Actually, I heard the ministry of education over there hands out fliers with "required knowledge" and 3.5" disks with turbo C on them.
    Good lord... do they not have web browsers over there? (obviusly they do)

    With so many forums and so much energy devoted to advancing the state of programming, one has to wonder how so many people can remain so staunchly ignorant of what's going on in the real world... It's not bad enough that the educational system seems "stuck in the 80s" the real crime here seems to be that students have no idea how regressive it really is. When these guys graduate their diplomas aren't going to be worth the paper they're printed on... ESPECIALLY the ones who've written their phone numbers on the exam papers and PURCHASED a passing grade.

    Really... this is bad. It threatens to cause problems world wide as these guys spread out after graduation.
    Vxyz likes this.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    5
    Thank you......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  2. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 09:46 PM
  4. output a string to a standard output
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 05:59 AM
  5. Replies: 3
    Last Post: 02-19-2003, 07:34 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21