为什么以下程序的输出是d?
另外,请解释+printf语句中的用法.
?#?include?"stdio.h"
int main(){
int a=1;
printf("%d"+a);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 任何人都可以解释为什么运行以下代码只打印换行符?
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
int x = 12;
char *s = (char *) &x;
printf("%s\n", s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因为我们正在&x作为一个字符串进行转换,所以不应该打印的是地址的字符串表示x(可能是一些十六进制的内存地址)?
我从这里学习C语言中与时间相关的函数.他们使用以下示例演示了strftime()函数:
#include <stdio.h>
#include <time.h>
#define LEN 150
int main ()
{
char buf[LEN];
time_t curtime;
struct tm *loc_time;
//Getting current time of system
curtime = time (NULL);
// Converting current time to local time
loc_time = localtime (&curtime);
// Displaying date and time in standard format
printf("%s", asctime (loc_time));
strftime (buf, LEN, "Today is %A, %b %d.\n", loc_time);
fputs (buf, stdout);
strftime (buf, LEN, "Time is %I:%M %p.\n", loc_time);
fputs (buf, stdout);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对printf()中的%m说明符进行了调整.它说%m转换说明符不是C,而是printf的GNU扩展.'%m'转换在errno中打印对应于错误代码的字符串.
我知道格式说明符%是C99中的新内容.它以十六进制形式打印浮点数. …
在我最近编写的一个程序中,我遇到了一个相当奇怪的错误,经过大量研究,我无法弄清楚它为什么会发生.我使用printf显示一个字符串,后跟一个整数到stdout.这看起来像是一个想法:
printf("The number of characters on the screen is %d", &numberofchar);
Run Code Online (Sandbox Code Playgroud)
很标准吧?该变量被声明为一个整数并初始化为1.循环后,整数保持正确的值,例如3,在调试中我可以确认变量在printf语句中赋值.
但是从stdout读取时的输出是一个很大的随机数,例如2530801.
我想了解为什么会发生这种情况并知道是否有任何替代方案,例如使用%i阻止此错误发生.我想象它在堆栈中的某种错误,但我对C的了解并不是太深入.
谢谢您的帮助.
注意:这适用于所有printf("%d", &int_variable)用于在程序中输出整数的实例.所有整数的值总是小于20.
所有printf语句都出错,说明:
格式字符串不使用数据争论.
我在互联网上环顾四周,发现了一些相关的事情,但是我并没有完全理解这个解决方案,希望也许有人可以进一步解释.
void displayPuzzle()
{
int i, j;
char x = 'A';
for (i = 0; i < COLOUMNS; i ++)
{
printf("%c ", x); //error here
x++;
}
printf("\n\n");
for (i = 0; i < ROWS; i ++)
{
printf("%d\t", i); //error here
for (j = 0; j < COLOUMNS; j ++)
{
printf("%c ", puzzle[i][j]); //error here
}
printf("\n\n");
}
}
Run Code Online (Sandbox Code Playgroud) 以下代码用于编译罚款.我将项目迁移到了新版本,现在编译器抱怨我的一个sprintf论点.这是警告:
warning: format '%02d' expects type 'int', but argument 12 has type 'double'
Run Code Online (Sandbox Code Playgroud)
仅供参考,IDE是MPLAB X 2.35,编译器是XC 1.34.我关闭了优化,我尝试了清理/构建.我只能怀疑结构对齐可能不合适.我希望我只是错过了明显的,比如无法计算.
违规代码:
typedef struct _AnalysisEvent
{
unsigned short id;
unsigned char day;
unsigned char month;
unsigned char year;
unsigned char hour;
unsigned char minute;
unsigned char second;
unsigned int duration;
double min;
double max;
double avg;
} AnalysisEvent;
AnalysisEvent AnalysisEvents[ANALYSIS_MAX_COUNT][ANALYSIS_EVENTS_MAX_COUNT];
unsigned char AnalysisEventGetValueStrAlt(unsigned short id, unsigned char index, char *buffer, int length)
{
if (Analysis[id].count > index)
{
sprintf(buffer, "Analysis ID: %d\r\nEvent …Run Code Online (Sandbox Code Playgroud) 我有一些非常简单的代码,我遇到了问题.这个程序只是计算一个人的体重(千克)并显示它.但是,每次运行时,打印的答案都会返回0.00000,但会返回正确的答案.有人看到我的代码有什么问题吗?
#include <stdio.h>
#include <math.h>
int main(void)
{
float w;
float const wc=0.454;
printf("Enter your weight in pounds ");
scanf("%f", &w);
float wk = w * wc;
printf("Your weight in kilograms is: %f", &wk);
return(wk);
}
Run Code Online (Sandbox Code Playgroud) String s1=sc.next();
int x=sc.nextInt();
System.out.printf( "%-15s%03d\n", s1, x);
Run Code Online (Sandbox Code Playgroud)
有人可以解释“ printf”括号内的部分吗?
以下代码的输出是:
Hello World4
怎么样?
#include "stdio.h"
int func()
{
return(printf("Hello world"));
}
int main()
{
printf("%d",sizeof(fun));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
PS: - 据我说,sizeof()调用func()函数,其中返回statemnt调用printf函数打印hello world nd返回字符串的长度为11返回函数然后sizeof()函数返回11的大小是int和int的值取决于编译器2或4
我有以下功能,如果我的值是0,它会给出一些奇怪的输出.有人可以解释一下为什么这表现得很奇怪吗?我的意图是仅在非零值时打印该值,否则应为空白
# include<stdio.h>
int main(int argc, char *argv[]) {
int i=0;
printf("Number is %d\n", i ? i : "");
return 0;
}
-> gcc print.c
print.c: In function 'main':
print.c:5: warning: pointer/integer type mismatch in conditional expression
-> ./a.out
Number is 4195848
Run Code Online (Sandbox Code Playgroud)
我知道我可以像下面这样做,但我想用上面的逻辑做同样的事情
if(i != 0) {
printf("%d", i);
} else {
printf("%s", "");
}
Run Code Online (Sandbox Code Playgroud)