我偶然发现了我很好理解的这种行为.
我错误地在程序结束时编写了以下内容,以便在以下数组中打印元素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在第一个示例中.)
我需要在里面创建一个带有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中有这个警告
据我所知,在 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:
double predict_label = 6.0;
double prob_estimates = 8.0;
Run Code Online (Sandbox Code Playgroud)
如何将这两个变量转换C为char并打印出一个字符串,其中包含"预测标签的值为6,概率估计值为8"的字符串.
为什么我应该在这里使用%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) 我在 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) 我正在尝试打印 char * name;
我试过了
fprintf(stderr,"%c",* name)
Run Code Online (Sandbox Code Playgroud)
但它似乎不起作用。我的推理是因为 name 是一个字符指针,所以我可以使用 * 来获取指针的值。
它给出了错误错误:格式指定类型“char *”但参数的类型为“char”
有人可以解释为什么代码打印 "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) 例如,当我的代码如下所示时,对于 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 并且目前正在使用格式说明符。下面是代码示例:
#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 上测试了这个示例,两者的行为都相同。
gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)