Printf a string using "string_name" (quotes)

This is a discussion on Printf a string using "string_name" (quotes) within the C Programming forums, part of the General Programming Boards category; Hi guys! I would like to printf a string using quotes e.g. I would like my output to be like ...

  1. #1
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    Printf a string using "string_name" (quotes)

    Hi guys!

    I would like to printf a string using quotes e.g.

    I would like my output to be like "Hello" and not Hello.

    e.g.

    char *name = "Hello";
    printf(" "%s" \n",name);

    But i get the output :

    a.c: In function `main':
    a.c:128: error: `s' undeclared (first use in this function)
    a.c:128: error: (Each undeclared identifier is reported only once
    a.c:128: error: for each function it appears in.)
    a.c:128: error: parse error before string constant

    What shall i do in order to get "Hello" ?

    Thanks, in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    You have to escape the double quotes by preceding each one with a backslash, e.g., \".
    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 Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Or you can use single quotes to keep your strings from looking like gibberish:
    Code:
    printf( "'%s'\n", name );

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by g_p View Post
    char *name = "Hello";
    and if you don't know what string literals are, rather write:

    Code:
    char name[] = "Hello";
    The result is the same but it's a C string while the other is a point to a constant (read-only) string literal.

  5. #5
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Thank you guys!

    It worked both ways!

    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM

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