How come Dev C++ cant run some programs??

This is a discussion on How come Dev C++ cant run some programs?? within the C Programming forums, part of the General Programming Boards category; Originally Posted by Sephiroth ok wait let me think about it for a sec. Ok so u mean that "[c-'0']" ...

  1. #31
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Sephiroth
    ok wait let me think about it for a sec. Ok so u mean that "[c-'0']"
    means that whatever c is , is going to turn into the value 0????
    but then how come cwr says is a subtraction sign?

    Does the value of c (whatever u inputted) just gets subtracted by '0' which is 48??? I am still kind of confused.
    c is a charater declaration. so anything that gets into c will be character.

    if c = '1' , ascii value of character '1' is 49 so c = 49 too.

    c = '1', in [c-'0'] c's ascii value will be subratted to the ascii value of charcter '0' which will yeild the diff of 1.

    get the point?

    ascii values:
    '0' = 48
    '1' = 49
    '2' = 50
    so on...
    Last edited by loko; 09-16-2005 at 07:48 AM.

  2. #32
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    You know, I am doing the same as Sephiroth myself (working through K+R) and I am at the same place. I too was completely confused by the exact same thing.... but I think its just dawned on me whats happening with that little statement.

    essentially its the asci code for the value in c - the acsi code for the value of 0 right? So if c were 2 then [c-'0'] would essentially be the same as:

    50 - 48 = 2

    Is that right?

  3. #33
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Yes.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char digit;
       for (digit = '0'; digit <= '9'; ++digit)
       {
          printf("'%c' = %d; ",    digit, digit);
          printf("'%c' - '%c' = ", digit, '0');
          printf("%d - %d = ",     digit, '0');
          printf("%d\n",           digit - '0');
       }
       return 0;
    }
    
    /* my output
    '0' = 48; '0' - '0' = 48 - 48 = 0
    '1' = 49; '1' - '0' = 49 - 48 = 1
    '2' = 50; '2' - '0' = 50 - 48 = 2
    '3' = 51; '3' - '0' = 51 - 48 = 3
    '4' = 52; '4' - '0' = 52 - 48 = 4
    '5' = 53; '5' - '0' = 53 - 48 = 5
    '6' = 54; '6' - '0' = 54 - 48 = 6
    '7' = 55; '7' - '0' = 55 - 48 = 7
    '8' = 56; '8' - '0' = 56 - 48 = 8
    '9' = 57; '9' - '0' = 57 - 48 = 9
    */
    (Although it need not be ASCII.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #34
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    ok thanks now i understand what it means. But is that really necessary? I tried taking that out and it produces an error message. Why couldnt we just do [c-48]?? or that doesn't work?

  5. #35
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    o sorry 1 more thing, so '0' and just 0 r different. Cause in your program when u put things like this:

    '0' = 0

    thats actually different than 0 = 0 right?
    So can you use '0' to stand for the value 0 too?

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    So can you use '0' to stand for the value 0 too?
    No. There was no "'0' = 0", only "digit = '0'", which in ASCII would be equivalent to "digit = 48".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #37
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > But is that really necessary? I tried taking that out and it produces an error message
    So why didn't you post the code, the error message and a question?
    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.

  8. #38
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    ok let me try asking this a little clearer. for instance, [c-'0'] u said that is the same as 50-48 if c was 2. yeah but how do u know when c is either 2 or 50?? is it because 0 is written as '0'??

    like how do u know that [c-'0'] is 50 - 48 or 2-0 if c was 2?
    is the difference the single quotes outside the 0? I 'm confused over this part. I hope I made it clear this time.

  9. #39
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Sephiroth
    is the difference the single quotes outside the 0?
    Yes that makes the difference. The single quotes are there for a purpose.
    Kurt

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    for instance, [c-'0'] u said that is the same as 50-48 if c was 2. yeah but how do u know when c is either 2 or 50?? is it because 0 is written as '0'
    It sounds like c is an unfortunate choice of variable name.

    '0' is the character zero. In ASCII it has the value of 48.
    If a char ch = '2', then (ch-'0') is equivalent to ('2'-'0').
    Now '2' in ASCII has the value of 50.
    So ('2'-'0') is equivalent to (50-48), which is of course, 2 - but this is the integer 2, not the character '2'.

    like how do u know that [c-'0'] is 50 - 48 or 2-0 if c was 2?
    In this case, we refer to an ASCII table, such as the one provided here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #41
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    yeah i thought it would be because of the single quotes beside the 0. I just wonder y people use ascii character value instead of the value itself ; it doesn't really look like it makes anything easier...

  12. #42
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > I just wonder y people use ascii character value
    But '0' isn't necessarily ASCII

    Saying c - 48 limits your program to those implementations which use ASCII as their character set. In say EBCDIC your code is broken.

    c - '0' always produces the correct answer no matter what your implementation character set is (C assumes that '0' to '9' are contiguous, but you can't assume the same for 'a' to 'z' for example).

    What if the code said c - 77, would you instantly know what letter that was without reaching for your character tables, or would c - 'M' be more readable?
    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.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual c++ 2005 express edition to run .c files
    By the_fall_guy in forum C Programming
    Replies: 4
    Last Post: 04-05-2007, 12:33 PM
  2. Dev C++ Won't Compile
    By mburt in forum Windows Programming
    Replies: 8
    Last Post: 08-21-2006, 11:14 PM
  3. Running programs - few stupid mistakes
    By Korhedron in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 02:10 PM
  4. Programs to run C applications
    By Mak in forum C Programming
    Replies: 3
    Last Post: 03-01-2004, 11:56 AM
  5. how to compile & run c programs in unix?
    By Unregistere in forum C Programming
    Replies: 2
    Last Post: 10-09-2002, 10:53 PM

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