Take the 2-minute tour ×

I'm looking at the following code I found in libgksu and I'm wondering what the %s inside the string does. I'm unable to use Google for this since it strips away characters such as the percentile during the search, leaving me only with 's' as a search term.

if (!strcmp(context->user, "root"))
        msg = g_strdup_printf (_("<b><big>Enter your password to perform"
                     " administrative tasks</big></b>\n\n"
                     "The application '%s' lets you "
                     "modify essential parts of your "

The purpose of this piece of code is to provide the text for the dialogue box that the user sees when an application requests superuser privileges on Linux, as can be seen in this screenshot

enter image description here

The %s in this case is the variable that contains the name of the application requesting privileges, but it isn't as simple as that because I've seen the %s used throughout the code in completely different contexts. For example, the else component of the above if statement is

    msg = g_strdup_printf (_("<b><big>Enter your password to run "
                 "the application '%s' as user %s"
               command, context->user);

and %s is being used to mark the name of both an application and a user. Can someone please tell me what purpose of %s is and where I can find out more information on it's use? I'm assuming this is a regular expression, but as I said earlier, I can't Google to find out.

share|improve this question

4 Answers 4

up vote 3 down vote accepted

%s is a C format specifier for a string.

                         "the application '%s' as user %s"
                         command, context->user);

means "where you see the first %s, replace it with the contents of command as a string, and where you see the second %s, replace it with the contents of context->user as a string.

share|improve this answer

printf() has a long C-based history. the %s is a 'format character', indicating "insert a string here". The extra parameters after the string in your two function calls are the values to fill into the format character placeholders:

In the first example, %s will be replaced with the contents of the command variable. In the second example, the first %s will get command, and the second %s will get context->user.

share|improve this answer

It's a format flag. You can look at the 'printf' man page for more informations.

Basicaly, every %s will be replaced by the function argument corresponding. printf("%s %s", "hello", "world") will print a simple "hello world"

share|improve this answer
(The 's' flag means 'string', by the way) –  Maël Nison Aug 9 '11 at 16:35

%s will simply be replaced by string like

char a[15]="some string";

printf("this is %s.",a);

so output will be

this is some string.

share|improve this answer

Your Answer


