标签: printf

为什么使用"%s"格式说明符printf一个char数组在数组中的最后一个char之后输出额外的无意义值?

我偶然发现了我很好理解的这种行为.

我错误地在程序结束时编写了以下内容,以便在以下数组中打印元素char:

printf("output: %s",outputText[]);
Run Code Online (Sandbox Code Playgroud)

当我应该(并最终)迭代数组并打印每个元素时:

for(int i = 0; i < textLength; i++){

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

促使我实施后者的是我得到的输出.尽管初始化数组以限制字符outputText[textLength],确保数组中没有意外的元素,但是在实现前一代码时我的输出总是会被其他怪异的元素所占据,如下所示:

怪异的角色补充

我只是连续三次运行相同的程序,并在我的数组末尾附加了三个随机字符.

(编辑替换outputText[]- > outputText在第一个示例中.)

c arrays printf format-specifiers

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

fmt.Sprint()返回不正确的结果

我需要在里面创建一个带有html链接的字符串.

fmt.Sprint("<a href=\"%s\">%s</a>", "/myUrl", "link text");
Run Code Online (Sandbox Code Playgroud)

预期的结果是

<a href="/myUrl">link text</a>
Run Code Online (Sandbox Code Playgroud)

但真正的结果是

<a href="%s">%s</a>/myUrllink text
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

顺便说一句,我在GoLand中有这个警告

在此输入图像描述

printf go

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

需要解释在什么情况下我们应该在 C 中将 &amp; 与 printf 一起使用

据我所知,在 c 中使用时printf()我们不使用 &.对吗?但是在我的程序中,如果我不在显示功能中使用它,它会给我一个错误。有人可以解释一下吗?谢谢

#include<stdio.h>
#define max 100
void enqueue();
char dequeue(void);
int front=-1,rear=-1,option;
char name[max][max],val;
void display(void);


void enqueue() {

    printf("Enter the name of the paitent : ");
    if(rear==max-1)
        printf("Line is full");
    else if (front==-1 && rear==-1)
        front=rear=0;
    else
        rear++;
    scanf("%s",&name[rear][rear]);

}

char dequeue(void) {
    char val;
    if(front==-1 || front >rear )
        printf("Line is empty");
    else
    {
        val=name[front];
        front++;
        if(front>rear)
            front=rear=-1;
        return val;
    }
}
void display(void) {
    int i;
    if(front==-1|| front >rear)
        printf("The queue is empty"); …
Run Code Online (Sandbox Code Playgroud)

c printing printf output

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

在C中,将2个双精度转换为一个字符串

我有这两个变量C:

double predict_label = 6.0;
double prob_estimates = 8.0;
Run Code Online (Sandbox Code Playgroud)

如何将这两个变量转换Cchar并打印出一个字符串,其中包含"预测标签的值为6,概率估计值为8"的字符串.

c printf type-conversion

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

[C]关于%s,%c的字符串和数组

为什么我应该在这里使用%c而不是%s?如果我使用%s将错误

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

当我定义 char Array[STRSIZE]="apple"; should use %s here

那么如何正确使用%s和%c?

码:

#include<stdio.h>
#include<string.h>
   int main(){
      #define STRSIZE 81

         char Array[STRSIZE];
         printf("Enter a string: ");
         gets(Array);
         printf("\nYou entered: ");
         int i ;
    for(i=0;i<strlen(Array);i++){

         printf("%c",Array[i]);
    }
   return 0 ;} 
Run Code Online (Sandbox Code Playgroud)

结果:

Enter a string: apple
You entered: apple
Run Code Online (Sandbox Code Playgroud)

c string printf

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

C 编程中的 printf("%2d")

我在 stackoverflow 和其他网站上看到了很多问题和答案,但我很困惑,当 %2d 创建了 2 个字符空格并且我们想要执行此代码时,会发生什么?

int a = 20011;
printf("%2d\n",a);
Run Code Online (Sandbox Code Playgroud)

根据我的观点,它应该只打印 2 个字符宽,但是当我们打印时,它会打印整个字符。为什么?

我已经编辑了这个问题。现在我想知道 - 符号的用途。我读过很多文章说 - 符号告诉左对齐但是当它被使用时给出例子。例如:

int str[] = "skgskg";
 printf("%2s\n",str);
Run Code Online (Sandbox Code Playgroud)

c printf

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

如何打印字符 *

我正在尝试打印 char * name;

我试过了

fprintf(stderr,"%c",* name)
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用。我的推理是因为 name 是一个字符指针,所以我可以使用 * 来获取指针的值。

它给出了错误错误:格式指定类型“char *”但参数的类型为“char”

c printf pointers c-strings char

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

c 的 if、switch 和 while 条件中的打印语句

有人可以解释为什么代码打印 "HelloWorld" 而不是 "HelloWorldThere" 吗?另外,为什么它会打印任何内容,因为 if 或 switch 语句中没有条件?这是代码:

#include <stdio.h>

int main()
{
    int a, b;

    if(printf("Hello"))
        switch(printf("World"))
            while(printf("There"))
            {
                return 0;
            }
}
Run Code Online (Sandbox Code Playgroud)

c printf if-statement while-loop switch-statement

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

如果我 printf 一个没有匹配参数的整数,具体决定输出的是什么?

例如,当我的代码如下所示时,对于 a = 0,我得到 -1301113336,对于 a = 1 到 99,得到 -1266657529。

#include <stdio.h>

int main()
{
    int a;
    for (a = 0; a < 100; a++)
    {
        printf("\n%i");
    }
}
Run Code Online (Sandbox Code Playgroud)

当行为因编译器而异时,这是某种未定义的情况吗?如果我使用 gcc,这些数字来自哪里,为什么每次迭代都不会改变?

c printf

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

为什么 %c 格式说明符在以下代码中不起作用?

我刚刚开始阅读 C 并且目前正在使用格式说明符。下面是代码示例:

#include <stdio.h>


int main(void) {
    char code = 'a' - 'A';
    printf("\n>>>%c (%d)", code, code);
    printf("\n>>>%c", 32);

    char incode;
    printf("\n\nGive me some char: ");
    scanf("%c", &incode);
    printf("\n>>>%c (%d)", incode, incode);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

PS C:\ex> ./print

>>>  (32)
>>>

Give me some char: A

>>>A (65)
Run Code Online (Sandbox Code Playgroud)

那么,为什么%c在最后有效printf而在开始时无效?我在 Windows 和 Linux 上测试了这个示例,两者的行为都相同。

  • Windows 编译器: gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
  • Linux编译器: clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)

c printf clang

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