标签: format-specifiers

sprintf崩溃

 int main()
 {
     char buf1[100], buf[100]="ddl";

     sprintf(buf1, "log_name = '%.*s'", buf);
  }
Run Code Online (Sandbox Code Playgroud)

上面的程序崩溃了.我无法理解为什么会崩溃.据我所知,在角色printf跳过格式代码并将buf分配给下一格式代码之前.但这里有什么意义?

c printf format-specifiers

1
推荐指数
1
解决办法
1139
查看次数

(int)-1的CUDA内核printf使用%d说明符给出了错误的输出

用C告诉我的电脑

printf("%d",(int)-1);
Run Code Online (Sandbox Code Playgroud)

我确实希望,并且通常也得到'-1'响应.但是,在我的基于Ubuntu的Cuda 5.0上使用的特斯拉M2090 Nvidia卡这个无辜的演示程序

/** cheese.cu */
#include <iostream>
#include <cuda.h>
#include <cstdio>

using namespace std;

template<typename T> struct SpacePtr
  { size_t size; T* ptr; };

 __global__ void f(SpacePtr<int>* sp)
{
  printf("This _is_ a 'minus one' or ain't it: %d\n",(int)-1);
  // Note: All appears to work fine with %lu instead of %zd
  // Still: How does the size value affect the -1?
  printf("On DEV: Size: %zd, Minus One: %d\n",sp->size,(int)-1);
}

int main()
{
  SpacePtr<int> data; data.ptr = 0; data.size …
Run Code Online (Sandbox Code Playgroud)

printf cuda format-specifiers

1
推荐指数
1
解决办法
1674
查看次数

当我使用错误的格式说明符时会发生什么?

只是想知道当我在C中使用错误的格式说明符时会发生什么?

例如:

x = 'A';
printf("%c\n", x);
printf("%d\n", x);

x = 65;
printf("%c\n", x);
printf("%d\n", x);

x = 128;
printf("%d\n", x);
Run Code Online (Sandbox Code Playgroud)

c format-specifiers

1
推荐指数
1
解决办法
1865
查看次数

这个"#"在这里做了什么

我想出了一个代码

#include <stdio.h>

int main()
{ 
   int i = 1427;
   double d = 1427.0;

   printf("%#o\n", i);
   printf("%#X\n", i);
   printf("\n%g\n", d);
   printf("%#g\n", d);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

这给出了输出:

02623
0X593

1427
1427.00
Run Code Online (Sandbox Code Playgroud)

首先我认为#用于0输出的前缀,但我错了,因为它在最后一个输出中的奇怪行为,它在十进制后打印零.
有人可以解释这#是什么以及它在这里做了什么?

c format-specifiers

1
推荐指数
1
解决办法
326
查看次数

如何在运行时更改格式说明符的值?

我在我的项目中使用sscanf将字符串从源缓冲区复制到目标缓冲区.例如:

char* target;
target = (char*)malloc(100 * sizeof(char));
char* source = "Hello World";
sscanf(source, "%[^\t\n]", target); // trying to copy "Hello World" into target.
printf("%s\n", target);//this will print "Hello World".
Run Code Online (Sandbox Code Playgroud)

但是这种编码风格的方式是不被接受的.我的经理要我做的是:

sscanf(source, "%11[^\t\n]", target); // NOTE: I've written "%11". here, 11 == length of hello world.
Run Code Online (Sandbox Code Playgroud)

这意味着,他希望我也提供格式说明符.(在这种情况下为%11).

但是,问题是源的长度可能不同,我不知道如何为每个变长字符串编写正确的格式说明符.

c c++ scanf format-specifiers

1
推荐指数
1
解决办法
709
查看次数

数组只打印零

我创建了一个包含随机数的数组,但是当我打印数组时它们都是0,这里有什么问题?我知道随机数在那里,但仍然是零?

int superArray[SIZE];
int index, counter;

srand( time(0) );

for (index = 0; index < SIZE; index++)  //random numbers

    superArray[index] = rand() % 21  +1;

for (index = 0; index < SIZE; index++)  //vertical print
{
    printf("%8.f\n", superArray[index]);
}
printf("%8d\n\n", superArray[8]); //verify that array has random numbers
Run Code Online (Sandbox Code Playgroud)

c arrays random printf format-specifiers

1
推荐指数
1
解决办法
847
查看次数

为什么%e在格式字符串中的行为与%g不同?

我在Python2,Python3和C中尝试过这个:

为什么这些格式字符串返回不同精度的数字?

>>> "%.3e" % 123456789
'1.235e+08' 
>>> "%.3g" % 123456789
'1.23e+08' 
Run Code Online (Sandbox Code Playgroud)

c python format-specifiers

1
推荐指数
1
解决办法
4604
查看次数

如何在if语句中指定单引号?

if(ch == ''')
{
   word_len++;
}
Run Code Online (Sandbox Code Playgroud)

如何指定何时ch等于'它添加一个word_len?似乎我只能使用上面的代码来指定它.

c if-statement format-specifiers

1
推荐指数
1
解决办法
50
查看次数

如何在 CString::Format 中重复一个字符

我需要在 CString::Format 中重复一个字符。printf 中类似的东西:

printf("%.*s",10, _T("-"));

result: ----------
Run Code Online (Sandbox Code Playgroud)

如何在 CString::Format 中实现这一点?

sOutput.Format(_T("%.*s"),10,_T("-")); //doesn't work
Run Code Online (Sandbox Code Playgroud)

c++ mfc repeat format-specifiers

1
推荐指数
1
解决办法
1534
查看次数

C语言中%d和%.d的区别

我编写了以下代码并收到一个空白输出

int main(){

    int y=0;

    printf("%.d", y);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

相反,如果我只使用%d,我会得到0作为输出。

是什么区别%d%.d

非常感谢您的建议!

c format-specifiers

1
推荐指数
1
解决办法
107
查看次数

标签 统计

format-specifiers ×10

c ×8

printf ×3

c++ ×2

arrays ×1

cuda ×1

if-statement ×1

mfc ×1

python ×1

random ×1

repeat ×1

scanf ×1