Like Tree1Likes
  • 1 Post By JohnGraham

doubt in printf

This is a discussion on doubt in printf within the C Programming forums, part of the General Programming Boards category; Code: int main() { int a = 10; printf("val = %lu",(unsigned long int*) a); return 0; } still the output ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    doubt in printf

    Code:
    int main()
    {
    
    int a = 10;
    printf("val = %lu",(unsigned long int*) a);
    return 0;
    }
    still the output is 10.... why? Though I am casting a to a pointer, how did it print??.... If I print only a, also results 10, pointer to a also results 10

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    What you are doing results in undefined behaviour since the type of the argument is not correct. Therefore, the answer is: it printed like that because it was implemented to print that way. Something else could have been printed, or your compiler might even have refused to compile that.
    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

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by karthik537 View Post
    Code:
    printf("val = %lu",(unsigned long int*) a);
    If I print only a, also results 10, pointer to a also results 10
    The above code (i.e. (unsigned long int*)a) does not make a pointer to a. The "(unsigned long int *)" is a cast - it converts the literal value you've given it into the type you say, so in this case it creates a pointer with value 10 (i.e. dereferencing the pointer would be an attempt to address byte number 10 on your system).

    If you want a pointer to a, use "&a", which will have the type "int *" - I suggest you use "%p" as your format specifier in this case, i.e.:

    Code:
    printf("val = %p", &a);
    Last edited by JohnGraham; 10-09-2012 at 02:52 AM.
    Salem likes this.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in printf ??
    By ganesh bala in forum C Programming
    Replies: 3
    Last Post: 02-16-2009, 02:54 AM
  2. Doubt in c
    By kanagu_raj82 in forum C Programming
    Replies: 4
    Last Post: 01-10-2007, 10:22 AM
  3. doubt
    By chinuchinu in forum C Programming
    Replies: 5
    Last Post: 08-19-2006, 03:36 AM
  4. make printf using printf?
    By germaneater in forum C Programming
    Replies: 9
    Last Post: 11-10-2004, 09:58 PM
  5. Doubt
    By ansi_1999 in forum C Programming
    Replies: 5
    Last Post: 05-05-2004, 08:49 AM

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