Thread: doubt in printf

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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.
    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, 03:54 AM
  2. Doubt in c
    By kanagu_raj82 in forum C Programming
    Replies: 4
    Last Post: 01-10-2007, 11: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, 10:58 PM
  5. Doubt
    By ansi_1999 in forum C Programming
    Replies: 5
    Last Post: 05-05-2004, 08:49 AM