首页 > 编程知识 正文

c语语言sum函数,c语语言float使用

时间:2023-05-05 21:32:09 阅读:205120 作者:4038

c语言中sprintf函数

In this article, we’ll take a look at using the sprintf() function in C / C++.

在本文中,我们将研究在C / C ++中使用sprintf()函数。

This is an important function if you want to write characters from a format string into a buffer.

如果要将字符从格式字符串写入缓冲区,这是一项重要功能。

Let’s take a look at this function, using illustrative examples!

让我们使用示例说明一下此功能!



C / C ++中sprintf()函数的基本语法 (Basic Syntax of the sprintf() function in C/C++)

This function takes a format string and writes it into a string buffer.

此函数采用格式字符串,并将其写入字符串缓冲区。

This is very similar to printf(), but writes the output to a buffer instead of stdout.

这与printf()非常相似,但是将输出写入缓冲区而不是stdout 。

This is function is present in the <stdio.h> header file.

该功能存在于<stdio.h>头文件中。

#include <stdio.h>int sprintf (char* buffer, const char* fmt, ...);

Here, this takes in an input fmt string along with necessary arguments (denoted by ...) and stores it into buffer.

在这里,这将接收输入的fmt字符串以及必要的参数(由...表示),并将其存储到buffer 。

The function returns the number of characters written to the buffer if it succeeds.

如果成功,该函数将返回写入缓冲区的字符数。

If it fails, it will return a negative integer.

如果失败,它将返回一个负整数。

The size of the buffer must be large enough to accomodate the string! Otherwise, you can access an unbound memory location.

缓冲区的大小必须足够大以容纳字符串! 否则,您可以访问未绑定的内存位置。

Now, to understand this completely, let’s look at some examples:

现在,为了完全理解这一点,让我们看一些示例:



使用sprintf()–一些示例 (Using sprintf() – Some Examples)

Let’s take an input from the user, and concatenate it onto a string, using sprintf().

让我们从用户那里获取输入,并使用sprintf()将其连接到字符串上。

#include <stdio.h>int main() { char original[] = "Hello from JournalDev"; char buffer[256]; printf("Enter an integer:n"); int n; scanf("%d", &n); sprintf(buffer, "%s_%d", original, n); printf("Buffer = %sn", buffer); return 0;}

Output

输出量

Enter an integer:100Buffer = Hello from JournalDev_100

As you can see, we used the format string to concatenate an integer to a string directly.

如您所见,我们使用格式字符串将整数直接连接为字符串。

You may just realize how handy this function can be when used at the right time!

您可能只是意识到在正确的时间使用该功能非常方便!

Let’s now take another example to verify that all the characters are written to the buffer.

现在让我们举另一个例子来验证是否所有字符都已写入缓冲区。

#include <stdio.h>int main() { char original[] = "Hello from JournalDev"; char buffer[25]; printf("Enter an integer:n"); int n; scanf("%d", &n); int num_written = sprintf(buffer, "%s_%d", original, n); printf("Buffer = %sn", buffer); printf("Number of Characters written using sprintf() = %dn", num_written); return 0;}

Output

输出量

Enter an integer:10Buffer = Hello from JournalDev_10Number of Characters written using sprintf() = 24

As you can see, the number of characters written (24), is the same as the string length!

如您所见,写入的字符数(24)与字符串长度相同!



结论 (Conclusion)

In this article, we learned about using the sprintf() function in C/C++. This is useful if you want to write to a string using a format specifier.

在本文中,我们学习了在C / C ++中使用sprintf()函数的知识。 如果要使用格式说明符写入字符串,这将很有用。

For similar content, do go through our tutorial section on C programming, which covers various aspects of C and C++.

对于类似的内容,请阅读我们有关C编程的教程部分 ,其中涵盖C和C ++的各个方面。

参考资料 (References) cppreference.com page on sprintf() in C / C++

C / C ++中sprintf()上的cppreference.com页面



翻译自: https://www.journaldev.com/41098/sprintf-function-in-c-plus-plus

c语言中sprintf函数

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。