Thread: Need help getting output.........

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    45

    Need help getting output.........

    Code:
    main()
    {
     char ch;
     printf("enter an alphabet");
     scanf("%c",&ch);
     if(ch>=65 && ch<=90)
       return(ch);
     else
       return(ch+10);
    }
    Different return statements should be executed depending on whether 'ch' is capital or not but ain't getting output...........
    C enlightened

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    You are not getting output because there are no output statements. Rather than returning the result from main, try printing it with printf. Returning from main simply terminates the program, and unless you are calling the program from a shell script that subsequently does something like display the return code, there will be no further output.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    I forgot to add printf statement,thanks sonjared.
    Code:
    main()
    {
     char ch;
     printf("enter an alphabet");
     scanf("%c",&ch);
     printf("%d",ch);
     if(ch>=65 && ch<=90)
     return(ch);
     else
     return(ch+10);
    }
    C enlightened

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    On a slight tangent, the return value from main is an error code. Zero represents success and non-zero represents some code that the consuming process presumably recognizes. As such, returning either ch or ch+10 in your program is unconventional and may even be detrimental depending on how the program is run.

    Other nits to be picked:


    • Code:
      main()
      is no longer universally valid. The C99 standard removed implicit int from the supported features, so this code will not compile as C99 or newer. A strictly correct definition of main taking no arguments is
      Code:
      int main(void)
          {
          return 0;
          }
      From C99 forward you may omit the return statement and execution will behave as if 0 were returned when falling off the end of main, but this means your code will not be portable to older compilers. That is not necessarily a bad thing, just something to keep in mind.
    • When printing output where the output does not end with a newline character, it is best to call fflush(stdout) to ensure that the message is displayed promptly. This is primarily useful for user prompts.
    • Technically it is unsafe to check the range of the latin alphabet characters because some character sets interleave non-alpha characters in the range. It is also unsafe to use explicit values because other character sets may not use the same value for those characters.

      To correct the latter problem you can do this:
      Code:
      /* Works for ASCII and Unicode */
      if (ch >= 'A' && ch <= 'Z')
          {
          /* ch is upper case */
          }
      To correct both problems, it is best to use isupper from the ctype.h header:
      Code:
      /* Works for across the board for the char type */
      if (isupper(ch))
          {
          /* ch is upper case */
          }

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, 10: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, 08:34 PM