Thread: Printf query

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Printf query

    In the function printf why "const char *" is used?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you mean why is the format string of that type? Because that's the type of a string literal, especially one that printf should not be changing.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I dont get your question correctly but what we write inside printf is a string literal and is non-writable. Eg.
    Code:
    printf("Hello World"); // string literal and can't be modified
    I think this is what you're asking.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Kerninghan Ritchie, chapter 5
    Perhaps the most common occurrence of string constants is as arguments to functions, as in
    Code:
    printf("hello,world\n");
    When a character string like this appears in a program, access to it is through a character
    pointer; printf receives a pointer to the beginning of the character array. That is, a string
    constant is accessed by a pointer to its first element.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Well thanks a lot for all your help.

    But what I need to ask is why 'const' is used in printf prototype. If the reason is it should not be changing then why in numerical functions 'const' is not used such as in sqrt and others. Hope you all get me now...

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I think Tabstop answered your question. The format string is a string literal, that should not be modified by printf. If it was necessary that the parameter variables should not change in pow or sqrt, they would be declared const as well. However, with functions like pow and sqrt, you are really only passing in a copy of the variable, and so even if they do get modified, it does not change the variable outside of the function. With the string literal passed to printf, what you are getting is a pointer to the string, which could then be used to modify the original (Though I think any tampering with the string literal would produce a segfault these days), which is undesirable.
    Last edited by kermit; 09-21-2009 at 08:58 AM.

  7. #7
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Quote Originally Posted by kermit View Post
    I think Tabstop answered your question. The format string is a string literal, that should not be modified by printf. If it was necessary that the parameter variables should not change in pow or sqrt, they would be declared const as well. However, with functions like pow and sqrt, you are really only passing in a copy of the variable, and so even if they do get modified, it does not change the variable outside of the function. With the string literal passed to printf, what you are getting is a pointer to the string, which could then be used to modify the original (Though I think any tampering with the string literal would produce a segfault these days), which is undesirable.
    Thanks a lot! I am now fully satisfied!
    Others also, thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM