Thread: using double codes in assigning value to an int variable

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    using double codes in assigning value to an int variable

    Code:
    main()
    {
       int i = "a";
       printf("%d       %c",a,a);
       getch();
    }
    the output shows a no. 170 and a ASCII character corresponding to it. Even if we change the character we get the same no. 170. I know I am int variable wrongly but it is just a curiosity why does it shows 170. If we use float or char variable it shows an error.
    I am usinng turbo c++ in windows xp.

    thanks a lot
    bye n take care

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    All you're printing is the address of where the string is stored.
    Or in the case of %c, the LSB of the address, expressed as a char.

    Since the compiler seems to put the first string it finds in the same place in memory, the actual content of the string is irrelevant.
    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.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I would dought that program will atleast compile. a is a variable which us undeclared. Din't your compile complain you anything about.

    ssharish2005

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    @Salem Thanks Salem, I somewhat understood your point, one thing is still there, why does using int variable does not give a error but using float or char gives error.

    @ssharish2005, the compliler doesn't give any error in this case, but if we use float or char in place of int then it gives error.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Yeah, there is so much wrong with the code that analysis is nigh on impossible.
    It was probably just typed from memory, rather than a more accurate copy/paste from the code editor.
    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.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by hitesh_best View Post
    @Salem Thanks Salem, I somewhat understood your point, one thing is still there, why does using int variable does not give a error but using float or char gives error.

    @ssharish2005, the compliler doesn't give any error in this case, but if we use float or char in place of int then it gives error.
    He was asking where 'a' came from (the variable),

    Code:
    main()
    {
       int i = "a";
       printf("%d       %c",a,a);
       getch();
    }
    But I assume you meant to print i not a

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Probably because your ancient compiler will silently convert a pointer to an int without complaining.
    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.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In short there is a big difference between double quotes " and a single quote '
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To the original poster: If you enable WARNINGS in your compiler, it will definitely tell you that you are converting a pointer into an integer, which is probably not really what you wanted.

    --
    Mats

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Oh yes, there is a mistake in the code.

    It should have been variable i in the print statement, instead of a.

  11. #11
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    Quote Originally Posted by hitesh_best View Post
    Oh yes, there is a mistake in the code.

    It should have been variable i in the print statement, instead of a.
    which means you actually posted the code without even looking at it once or bothering to compile it?
    Or just typed it from memory and never bothered to check it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM