sprintf中的字符串格式化概念现在几乎可以在任何语言中找到(你知道,用%s%d%f等窒息字符串并提供一个变量列表来填充它们的位置).
哪个langugage最初具有提供此功能的库函数或语言结构?
请指定某种来源参考来确认您的声明,以便我们避免纯粹的推测或猜测.
问候
罗伯特
码:
printfn "%10s" "abc"
printfn "%-10s" "abc"
printfn "%10d" 123
printfn "%-10d" 123
printfn "%10c" 'a'
printfn "%-10c" 'a'
Run Code Online (Sandbox Code Playgroud)
输出:
abc
abc
123
123
a
a
Run Code Online (Sandbox Code Playgroud)
所以正确的对齐%c并不像我预期的那样有效......
F# 2.0 build 4.0.30319.1
即可以printf告诉它在小数点之前忽略零?
只需在VC2008中构建并运行它:
struct A
{
int a;
int b;
int c;
};
A a = { 10, 20, 30 };
printf("%d %d %d\n", a);
Run Code Online (Sandbox Code Playgroud)
这是正常的吗?
10 20 30
Run Code Online (Sandbox Code Playgroud)
我想投!但它不起作用:
struct A
{
int a;
int b;
int c;
operator int()
{
return a + b + c;
}
};
A a = { 10, 20, 30 };
printf("%d\n", a);
Run Code Online (Sandbox Code Playgroud)
仅输出:
10
Run Code Online (Sandbox Code Playgroud)
我需要自动转换模板实用程序.这是:https://code.google.com/p/boolib/source/browse/boolib/crypt/ShakedValue.h 它应该隐藏在内存中,任何hack-programms(ArtMoney)都找不到值.
还有一个技巧:打印struct/class的私有成员
对于存储在x中的double值,缓冲区需要在下面的函数调用中有多大?
sprintf(buffer, "%.*g", DBL_DIG, x);
Run Code Online (Sandbox Code Playgroud) 如何在printf中以粗体显示输出?"[1m testing bold"什么都不做
String format = "%-20s %-15s %-20s %-15s %-10s";
System.out.printf(format, "Name", "Group_name", "Java_Prof_Level", "Cpr_Nr", "Gender", "[1m testing bold");
Run Code Online (Sandbox Code Playgroud) 我的程序要求用户提供一个字符串,该字符串将被复制到一个字符数组中.然后,通过for循环,程序只是将第一个数组的元素复制到第二个数组中.
int main() {
int i;
char string1[4], string2[4];
// Get the first string
printf("Insert your string: ");
scanf("%s", string1);
// Copy the values into the second array
for (i = 0; i < 4; i++) {
string2[i] = string1[i];
}
// Print the second string
printf("%s", string2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用函数打印字符串时printf(),字符串会被打印两次.
假设我输入了这个词
酒吧
输出将是
barsbars
为什么会这样?
我正在尝试用C++创建一个命令行应用程序,我想确保在某个命令参数之后输入是一个整数.
对于此示例,我想检查下一个参数是否是"-p"命令参数后面的整数.这是我现在代码的片段.
while (count < argc){
if (strcmp("-p", argv[count]) == 0){
has_p = true; //Boolean
pid = atoi(argv[count + 1]);
if (pid == 0 && argv[count + 1] != "0" ){
err = 1;
cout << "pid argument is not a valid input" << endl;
pid = -1;
}
count++;
}
...
}
Run Code Online (Sandbox Code Playgroud)
现在,此代码正确捕获此输入中的错误:
但是输入格式失败了
我尝试通过使用sprintf进行比较来解决这个问题.不幸的是,使用char数组指针提供sprintf只会在buffer2中输出1个字符.
while (count < argc){
if (strcmp("-p", argv[count]) == 0){
has_p = true; //Boolean …Run Code Online (Sandbox Code Playgroud) 在C中,当我移动此printf行时:printf("%f\n", 5 / 2);对于不同的行,其输出会发生变化.有任何想法吗?
下面是代码:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int a = 65;
char c = (char)a;
int m = 3.0/2;
printf("%c\n", c);
printf("%f\n", (float)a);
printf("%f\n", 5.0 / 2);
printf("%f\n", 5 / 2.0);
printf("%f\n", (float)5 / 2);
printf("%f\n", 5 / (float)2);
printf("%f\n", (float)(5 / 2));
printf("%f\n", 5.0 / 2);
printf("%d\n", m);
printf("%f\n", 5 / 2);
system("PAUSE");
return(0);
}
Run Code Online (Sandbox Code Playgroud)
继承人的产量:
A
65.000000
2.500000
2.500000
2.500000
2.500000
2.000000
2.500000
1
2.500000
Run Code Online (Sandbox Code Playgroud)
如果我移动printf("%f\n", 5 / 2);到第一行之一(在输出A的那一行和输出65.000000的那一行之间),它将打印0.000000(这是有意义的)而不是现在的2.500000.有任何想法吗?
这段代码对我的品味有点奇怪.拜托,有人在乎解释原因吗?以及如何强制'\n'被解释为一个特殊的字符?
beco@raposa:~/tmp/user/foo/bar$ ./interpretastring.x "2nd\nstr"
1st
str
2nd\nstr
beco@raposa:~/tmp/user/foo/bar$ cat interpretastring.c
#include <stdio.h>
int main(int argc, char **argv)
{
char *s="1st\nstr";
printf("%s\n", s);
printf("%s\n", argv[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
底线,意图是第二个字符串要打印成两行,就像第一行一样.这个程序是一个简化.真正的程序在使用文件读取时遇到了问题fgets(不是argv像这里那样的SO参数),但我认为在这里求解也会解决问题.