开发者

What does %s mean inside a string literal?

开发者 https://www.devze.com 2023-03-26 20:26 出处:网络
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 d

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

What does %s mean inside a string literal?

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.


%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.


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.


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"


%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.

0

精彩评论

暂无评论...
验证码 换一张
取 消