标签: printf

Sprintf崩溃我的程序

我只是尝试使用sprintf连接一些字符串,但我有这个问题,我不明白为什么我的程序在C中使用sprintf崩溃.为什么这个代码运行?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char* dateTime = malloc(16*sizeof(char));

    printf("Date: %s\n", __DATE__);
    printf("Time: %s\n", __TIME__);

    sprintf (dateTime, "%s, %s\0", __DATE__, __TIME__);

    printf("%s", dateTime);
    free(dateTime);

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

这不是吗?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char* dateTime = malloc(16*sizeof(char));

    //printf("Date: %s\n", __DATE__);
    //printf("Time: %s\n", __TIME__);

    sprintf (dateTime, "%s, %s\0", __DATE__, __TIME__);

    printf("%s", dateTime);
    free(dateTime);

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

c printf pointers

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

为什么printf只显示第一个字母?

首先,我有一个功能,我有unsigned char*类型存储的标志.例如,Unicode中每个符号Abcdef!?为[65 0] [98 0] [99 0] [100 0] [101 0] [102 0] [33 0] [63 0]2个字节.

当我使用for(unsigned char i=0; i<17; i++) printf("%c", pointer[i]);一切正常时,它显示Abcdef!?.但是当我使用printf("%s" pointer);它时,它只给我,A而不是别的.你能告诉我为什么吗?

c unicode printf

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

C程序在被告知之前执行命令

我已经开始研究使用C的命令处理,但我遇到了这个C程序的问题.它正在执行ls命令之前.

Gcc信息:

gcc version 6.2.1 20161124 (Debian 6.2.1-5)
Run Code Online (Sandbox Code Playgroud)

这是代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;

    printf("Is command processor available?\n");
    if (system(NULL))
    {
        printf("Command processor available!\n");
    }
    else
    {
        printf("Command processor not available!\n");
        exit(1);
    }

    printf("Executing command ls");
    i=system("ls");

    printf("Returned value is: %d.\n",i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我所说的代码就是这条特定的代码:

    printf("Executing command: ls");
Run Code Online (Sandbox Code Playgroud)

如果使用该段代码运行程序,则输出为:

Is command processor available?
Command processor is available
systemProcessing  systemProcessing.c
Executing command: lsReturned value is: 0.
Run Code Online (Sandbox Code Playgroud)

它在实际被告知之前执行命令

但是当我使用新行"\n"完成代码时,其输出符合预期:

Is command processor available?
Command processor is …
Run Code Online (Sandbox Code Playgroud)

c printf command-execution

3
推荐指数
2
解决办法
103
查看次数

如何动态地将值传递给Sprintf或Printf

如果我想填充一个字符串,我可以使用这样的东西:

https://play.golang.org/p/ATeUhSP18N

package main

import (
    "fmt"
)

func main() {
    x := fmt.Sprintf("%+20s", "Hello World!")
    fmt.Println(x)
}
Run Code Online (Sandbox Code Playgroud)

来自https://golang.org/pkg/fmt/

 +  always print a sign for numeric values; 
    guarantee ASCII-only output for %q (%+q)
 -  pad with spaces on the right rather than the left (left-justify the field)
Run Code Online (Sandbox Code Playgroud)

但是,如果我想动态更改打击垫大小,我怎么能传递该值?

我的第一位客人是:

x := fmt.Sprintf("%+%ds", 20, "Hello World!")
Run Code Online (Sandbox Code Playgroud)

但我明白了:

%ds%!(EXTRA int=20, string=Hello World!)
Run Code Online (Sandbox Code Playgroud)

有没有办法在不创建自定义填充函数的情况下执行此操作,可能会使用for循环向左或向右添加空格:

for i := 0; i < n; i++ {
    out += str
}
Run Code Online (Sandbox Code Playgroud)

printf go

3
推荐指数
2
解决办法
2062
查看次数

printf中使用的符号

我想知道符号中使用的符号是什么printf.我试过谷歌搜索但找不到合适的名字用于打印字符和字符串长度的程序

这里是使用Unicode BOTTOM SQUARE BRACKET字符U + 23B5表示的问题:

printf("?%d\n", br);
Run Code Online (Sandbox Code Playgroud)

(这是为了显示手写代码的样子;该字符不会出现在实际代码中.)

c unicode printf

3
推荐指数
2
解决办法
235
查看次数

CUDA printf的限制

使用printfCUDA内核打印到标准输出的行数是否有限制?如果是的话,有没有办法改变它?

printf cuda

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

sprintf()中的意外结果

有人可以帮我理解发生了什么吗?

char pcMessage[255];
iMsgAllocatedLength=255;
unsigned long long ullRecordID = 135290303ULL;
sprintf_s(pcMsg, iMsgAllocatedLength, "%08X;%llu", ullRecordID, ullRecordID);
Run Code Online (Sandbox Code Playgroud)

给出以下结果

08105DBF ; 581067426850930688;

而

sprintf_s(pcMsg, iMsgAllocatedLength, "%010llu;%08X;", ullRecordID, ullRecordID);
Run Code Online (Sandbox Code Playgroud)

给出预期的结果

0135290303; 08105DBF

请注意,我只是颠倒了订单,并注意到
581067426850930688是08105DBF 00000000

请帮我理解.

c printf endianness

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

使用putchar()(C编程)在整数末尾添加换行符?

我是C编程的新手,我想弄清楚如何在使用putchar()函数打印int值后添加换行符("\n").在K&R的"The C Programming"一书中,他们提供了下面的脚本,但所有字符都印在同一行上.如何修改代码以每行打印一个字符?

#include <stdio.h>

void main() {
int c;

c = getchar();
while (c != EOF) {
    putchar(c);
    c = getchar();
 }
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用printf()这样的东西:

printf("%d\n", c);
Run Code Online (Sandbox Code Playgroud)

但我想知道C编程和putchar()函数是否有类似的东西:

putchar(str(c)+"\n");
Run Code Online (Sandbox Code Playgroud)

有点像你在python中所做的那样.谢谢!

c string file-io printf

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

无法正确理解PHP中的vprintf()

我无法理解以下代码:

<?php
   $number = 123;
   vprintf("With 2 decimals: %1\$.2f
   <br>With no decimals: %1\$u",array($number));
?>
Run Code Online (Sandbox Code Playgroud)

浏览器输出:

With 2 decimals: 123.00 
With no decimals: 123
Run Code Online (Sandbox Code Playgroud)

但是这里只有一个元素在数组中,而它必须是两个参数.

还有什么意思 %1\$

php string printf

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

覆盖ToString和`%A`说明符

如果ToString是类型中的覆盖

type TestMe ()=
    override __.ToString() = null
Run Code Online (Sandbox Code Playgroud)

然后我通过说明"%A"符输出它

printfn "*%A*" (TestMe())
Run Code Online (Sandbox Code Playgroud)

它为什么抛出System.NullReferenceException?

我原以为它会表现得像

printfn "*%A*" null
Run Code Online (Sandbox Code Playgroud)

它只是打印<null>而没有任何例外.

printf f# nullreferenceexception

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