在 C 中制作 fstring (Python)

问题描述 投票:0回答:1

我想在 C 中创建一个 url 来使用 libcurl 发送 post 请求,并且我想经常更改它,如下所示:

url =“http://website.com/” 关键字=“此参数每次都应该更改!” auth="用户名:密码"

我已经使用 C 语言大约 2 个月了,在此之前我使用 Python 编写了这样的代码;在Python中,我曾经用fstrings写过这些东西,但现在我不知道该怎么做。

c
1个回答
0
投票

在 C 中,您可以通过使用

f-strings
sprintf
等格式化字符串函数来实现与 Python
snprintf
类似的功能。

#include <stdio.h>

int main() {
    char url[200]; // Adjust the size based on your expected URL length
    char keyword[] = "this parameter should be changed each time!";
    char auth[] = "username:password";

    // Formating String using the function
    snprintf(url, sizeof(url), "http://website.com/?keyword=%s&auth=%s", keyword, auth);

    printf("URL: %s\n", url);

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.