Thread: Help with the code

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    Help with the code

    Code:
    main()
    {
       int i=0;
       char c;
       while(1)                 
         {
          c='\0';           
          while(c!=13&&c!=27) 
            {
             c=getch();
             printf("%c\n", c);
            }
          if(c==27)
             break;         
          i++;
          printf("The No. is %d\n", i);
          }
        printf("The end");
     }
    Why I can not read the sentence "The No. is " in the output. Thank you very much.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you can. No really: I got
    Code:
    T
    h
    i
    s
    
    i
    s
    
    a
    
    t
    e
    s
    t
    
    The No. is 1
    
    The end
    where the funny symbol is ASCII 27, I guess (it showed up as a left-pointing arrow in my command prompt window).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sourceforge.net/Implicit_main
    And try avoiding magic numbers. In this code, it's not very easy, but in the future, if you wish representable characters such as 'a' or 'z', just type 'a' or 'z' instead of some number.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    And try avoiding magic numbers. In this code, it's not very easy, but in the future, if you wish representable characters such as 'a' or 'z', just type 'a' or 'z' instead of some number.
    I used to think like that, but compilers are now translating character codes like 'a' by using the *current* system encoding which may vary depending on the machine you're using (or you can also switch of encoding on a same machine fortunately). So if you really need to work with the ASCII encoding, it seems reasonable to use the corresponding code directly. I don't think it can be considered as 'magic numbers' as those codes are known and fixed (as for any encoding anyway). Obviously if you are working on a program which is independent of any encoding, I agree it's better to use character litterals instead of raw codes.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by root4 View Post
    I used to think like that, but compilers are now translating character codes like 'a' by using the *current* system encoding which may vary depending on the machine you're using (or you can also switch of encoding on a same machine fortunately).
    Well, this is somewhat true, but not all that much that you're hyping it to be.

    So if you really need to work with the ASCII encoding, it seems reasonable to use the corresponding code directly. I don't think it can be considered as 'magic numbers' as those codes are known and fixed (as for any encoding anyway). Obviously if you are working on a program which is independent of any encoding, I agree it's better to use character litterals instead of raw codes.
    The whole reasoning behind "magic numbers" aren't that they are dynamic or static but that it makes your code hard to read.
    What ascii character is number 63, for example?

    And if you consistently use correct variable type and character type, then you won't get undefined behavior.
    In C and C++ - there are two types, char and wchar_t, respectively. What they might be on the target machine doesn't matter to the language.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by Elysia View Post
    The whole reasoning behind "magic numbers" aren't that they are dynamic or static but that it makes your code hard to read.
    What ascii character is number 63, for example?
    At the very least, put a comment explaining what you're doing with those "magic numbers". This would help other people reading your code, and YOU, months from now when you have to look at the code again.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Thank you very much.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    The whole reasoning behind "magic numbers" aren't that they are dynamic or static but that it makes your code hard to read.
    This is indeed one possible definition, I would rather consider magic numbers as undocumented/arbitrary values used in a program.

    And if you consistently use correct variable type and character type, then you won't get undefined behavior.
    Why undefined behavior? this is not undefined, the standard is explicit on this point. Types don't change anything as this is a semantics problem. Whatever type you use "c='a'" is not guaranteed to result in the same value on a different machine (or on the same machine with a different configuration). So if the program expects a precise encoding, I simply say this is not a good solution. But you're right for the readability issue, using raw codes is not a good idea, constants (or equivalent) are better in this case.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by root4 View Post
    This is indeed one possible definition, I would rather consider magic numbers as undocumented/arbitrary values used in a program.
    Yes, a likely definition.

    Why undefined behavior? this is not undefined, the standard is explicit on this point. Types don't change anything as this is a semantics problem. Whatever type you use "c='a'" is not guaranteed to result in the same value on a different machine (or on the same machine with a different configuration). So if the program expects a precise encoding, I simply say this is not a good solution. But you're right for the readability issue, using raw codes is not a good idea, constants (or equivalent) are better in this case.
    But the point is, that if we want a 'c', we want a 'c'. Using magic numbers, or static numbers will provide hard-to-read code and may result in undefined behavior on systems where 'c' isn't the same as on the other system you hard-coded the value on.
    So by forcing the use of 'c', we will get 'c' wherever the program runs.

    If we're talking about something else - something not about displaying data, then yes, you should probably use "magic numbers." But not when displaying or working with data representable to the user.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    If we're talking about something else - something not about displaying data, then yes, you should probably use "magic numbers." But not when displaying or working with data representable to the user.
    Ok, that concludes the discussion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM