Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does %s mean inside a string literal?

Tags:

printf

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 "
                     "system."),
                   command);

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

else
    msg = g_strdup_printf (_("<b><big>Enter your password to run "
                 "the application '%s' as user %s"
                 "</big></b>"),
               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.


2 Answers

%s is a C format specifier for a string.

msg = g_strdup_printf (_("<b><big>Enter your password to run "
                         "the application '%s' as user %s"
                         "</big></b>"),
                         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.

like image 198
BishopRook Avatar answered Sep 17 '25 21:09

BishopRook


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.

like image 31
Marc B Avatar answered Sep 17 '25 21:09

Marc B