标签: printf

如何打印指针指向的字节的二进制内容?

我有一个从 malloc 返回的指针。它指向分配大小的第一个字节。对于 EX:

  void* p=malloc(34);
Run Code Online (Sandbox Code Playgroud)

如何打印这 34 个字节中包含的二进制值。?

c malloc printf pointers

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

printf() 的时间复杂度?

我想确定 printf 的时间复杂度,例如:

{
    printf("%d",
           i);
}
Run Code Online (Sandbox Code Playgroud)

或者:

{
    printf("%c",
           array[i]);
}
Run Code Online (Sandbox Code Playgroud)

假设 printf 的时间复杂度始终为 O(1) 是否正确?

[编辑] 让我们采用一个交换两个值的函数:

void swap(...)

{

      tmp = x;
      x = y;    
      y = tmp;  

}
Run Code Online (Sandbox Code Playgroud)

每个赋值表达式的成本为 1(就时间复杂度而言),因此 T(n) = 1 + 1 + 1 = 3,这意味着 O(1)。但对于这个功能我能说什么呢?

void swap(...)

{

      tmp = x;
      x = y;    
      y = tmp;  

      printf("Value of x: %d", x);
      printf("Value of y: %d", y);

}
Run Code Online (Sandbox Code Playgroud)

在这种情况下我可以说 T(n) 仍然是 O(1) 吗?

c time complexity-theory big-o printf

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

STM32 IAR 没有 ITM 跟踪输出,除非代码中包含 printf

我有一个小项目,我添加了自己的自定义调试功能,这样我就可以拥有一些额外的功能。他们一直工作得很好,并使用以下方法发送数据:

while(*bp)
  ITM_SendChar(*bp++);
Run Code Online (Sandbox Code Playgroud)

我终于开始切换所有 printf 语句以使用我自己的函数,并且所有输出都停止了。稍微玩了一下,我发现只要我编译了一个 printf 函数,无论在哪里,ITM_SendChar 都能正常工作。

当在项目中使用 printf 时,似乎编译了一些功能,允许 ITM_SendChar 工作。

这不是什么大不了的事,但我很好奇为什么会这样。是否有另一种方法可以初始化 ITM(仪器跟踪宏单元)系统,而不必插入虚拟 printf?

embedded printf stm32 iar cortex-m

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

以运行时可选择的精度打印浮点数

这与这个问题类似但不完全相同。

我天真地尝试过这个:

let s prec = "%." ^ (string_of_int prec) ^ "f" in
Printf.printf (s 2) 1.23
Run Code Online (Sandbox Code Playgroud)

但这被拒绝了,并被替换^为^^. 有什么办法可以做到这一点吗?

printf ocaml

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

有没有办法在控制台中打印不向下滚动的内容?(在C中)

好吧,如果我提供一些背景信息,也许会更容易理解。

我想在主程序的第一行打印“Inventory”,但我不希望“Inventory”从其位置移动,因此,当我的其余命令显示在控制台中并将向下滚动时,此特定行“库存将留在原地。” https://i.stack.imgur.com/hVItr.png

我不确定这是否可以在 C 中完成

c printf

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

为什么单元素元组被解释为Python中的该元素?

谁能解释为什么单元素元组在Python中被解释为该元素?

和

他们为什么不将元组打印(1,)为(1)?

请参阅下面的示例:

>>> (1)
1
>>> ((((1))))
1
>>> print(1,)
1
>>> print((1,))
(1,)
Run Code Online (Sandbox Code Playgroud)

python convention printf tuples

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

如何在C中将字符输入到数组中?

char name[2];
scanf("%c",name);
printf("%c",name);
Run Code Online (Sandbox Code Playgroud)

我刚刚开始学习 C。我对上面的代码很好奇,我从 printf 输出中得到的内容与我输入的字符不一样。相反,输出是一些看起来很有趣的符号。谁可以给我解释一下这个?

c arrays printf scanf

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

字符串不兼容

不确定我是否在这里遗漏了一些东西,当我尝试打印网址时它会抛出错误。

错误字符串与 Printf.TextWriteFormat 不兼容

type Credentials = {clientId: string; clientSecret: string}

let credentials = {clientId = ""; clientSecret = ""; deviceId = ""; deviceToken = ""}
let host = "https://example.com?grant_type=token";
let url = String.Format("%s?&client_id=%s&client_secret=%s", host, credentials.clientId, credentials.clientSecret)

printf url

let request = WebRequest.Create(url)
Run Code Online (Sandbox Code Playgroud)

编辑请求标头接受不起作用

    let request = WebRequest.Create(url)
    request.Method <- "POST"
    request.ContentType <- "application/x-www-form-urlencoded"
    //request.Headers.Set "Accept" "application/json;charset=UTF-8"
    request.ContentLength <- (int64)data.Length


request.Accept <- "application/json;charset=UTF-8" // doesnt' work either
Run Code Online (Sandbox Code Playgroud)

printf f#

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

没有从 WinMain 打印到标准输出 - C/VS 2017

我应该先说一下,我对 VS 比较陌生,但对 C 来说并不陌生。

我遇到的问题是打印时标准输出上没有显示任何内容。printf/_s 和 fprintf/_s(stdout, ...) 都不会产生任何输出。有趣的是, fprinf(file, ...) 实际上确实会生成给定文件的输出。这是否有可能与 printf 弃用有关(我已经尝试过 preproc._CRT_SECURE_NO_DEPRECATE)?

下面是我的完整程序:

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)                  
{
    FILE * pFile = fopen("outputTest.txt", "w");
    fprintf(pFile, "At top of main1.\n");       //works
    printf("At top of main2.\n");               //doesn't work
    printf_s("At top of main3.\n");             //doesn't work
    fprintf_s(stdout, "At top of main4.\n");    //doesn't work
    fflush(stdout);
    fclose(pFile);
    return FALSE;
}
Run Code Online (Sandbox Code Playgroud)

我使用的是 Visual Studio 2017,该程序是 Win32(应用程序?)。我还排除了链接器->系统->子系统是问题的可能性。

任何想法表示赞赏。 …

c printf winmain visual-studio-2017

5
推荐指数
0
解决办法
904
查看次数

打印 10 的幂 - C 中的 Printf

我必须打印 0-19 之间的 10 的幂。问题是当我想显示第一个幂(应该是 1 )时(10^0),我只是无法强制不printf重复 0 。我只能printf在我的程序中使用一个和一个{ }块(这是主函数)。

#include <stdio.h>

int main(void) {
    int power = 19;
    for (int i = 0; i <= power; i++)
        printf("1%0*d\n",i, 0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的输出:

10 // this should be 1, but printf still puts 0 here
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000000
100000000000
1000000000000
10000000000000
100000000000000
1000000000000000
10000000000000000
100000000000000000
1000000000000000000
10000000000000000000
Run Code Online (Sandbox Code Playgroud)

c printing printf

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