标签: printf

什么是这个代码的输出为什么?

我有一个C++函数,我知道会是什么,但为什么?

int c[5];
int* pc = c;

for (int i = 0; i < 5; i++)
{
    c[i] = i*2;
}
*pc++;
printf("%d\n", pc-c );
Run Code Online (Sandbox Code Playgroud)

c c++ printf pointers

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

*p ++和++*p之间的区别

int a = 5;
int *p = &a;
printf("%d\n\n", ++*p);
printf("%d\n", *p++);
Run Code Online (Sandbox Code Playgroud)

++*p相当于++(*p).但是*p++增加指针,而不是p指向的值.但我无法理解为什么我的代码中的printf语句显示相同的值"6".这背后有什么具体的逻辑吗?

c printf pointers

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

为什么以下代码的输出变为零?

main()
{
    float a=10;
    float c;
    float b=5.5;
    c=a+b;
    printf("%d",c);
}
Run Code Online (Sandbox Code Playgroud)

上面代码的输出为零.为什么?我很抱歉,如果这是一个非常简单的C概念,我是一个初学者.

c printf add output

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

为什么printf四舍五入?

我有一个简单的C程序(在树莓派上编译)应该计算17.67*20(这是353.4)下面的程序打印出353. printf它是否四舍五入?我试过" %g"和" %.1f"

#include <stdio.h>
#include <math.h>

int absolute_humidity(float temp)
{
    float abs_hum = (17.67 * temp);
    return abs_hum;
}

int main()
{
    float abshum = absolute_humidity(20.0);
    printf("%g",abshum);
}
Run Code Online (Sandbox Code Playgroud)

c floating-point printf

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

需要特定java打印的帮助

所以我有这个程序:

for(int i=1;i<=5;i++){ 
    int y=(int)Math.pow(4,i); 
    System.out.println(y);
}
Run Code Online (Sandbox Code Playgroud)

我怎么能这样打印:

   4
  16
  64
 256
1024 
Run Code Online (Sandbox Code Playgroud)

(在一位数字之前,它应该是3个空格,2个数字2个空格,3个数字1个空格).

谢谢你:D

java printf

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

调用printf("%d")两次,const结果

我写了以下程序:

#include<stdio.h>

int main(){
        printf("%x\n");
        printf("%x\n");
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道它是未定义的行为,我只是检查发生了什么.编译器是gcc.

样本输出是:

541d3118
7ffffff7
Run Code Online (Sandbox Code Playgroud)

另一个示例输出是:

e0b08078
7ffffff7
Run Code Online (Sandbox Code Playgroud)

当我使用-O3flag 编译它时,结果是:

5ec20f18
9
Run Code Online (Sandbox Code Playgroud)

3bedfa08
9
Run Code Online (Sandbox Code Playgroud)

为什么第一个值会改变,而不是第二个值?为什么高优化级别的第二个值不同?

c printf

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

格式化程序转换 - 为什么以下代码的结果="true"?

执行此代码时,我总是得到true结果:

public class TestDeclare {
public static void main(String[] args) {

double var = 34;  // any other litteral is valid
System.out.printf("%b ", var); 

}
}
Run Code Online (Sandbox Code Playgroud)

为什么我会得到true而不是false?是true默认值Number吗?

java format printf boolean number-formatting

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

返回fprintf的值

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(void)
{
    FILE *fh = fopen ("file.txt", "w");
    if (fh != NULL) {
        int i = 0;
        while(i < 5){
            if (fprintf (fh, "%s", "hello") < 0)
            {
                fprintf (stderr, "err=%d: %s\n", errno, strerror (errno));
            }
            if(ferror(fh))
            {
                printf("wrong\n");
            }
            sleep(10);
        }
    }  
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

当我在程序运行期间删除文件"file.txt"时,我希望fprintf返回小于0的值以打印错误消息.但是fprintf总是返回"hello"字符串的大小,即5.

注意:由于有10秒的睡眠调用,我在程序终止之前删除了该文件.

如果fprintf/fwrite写入不再存在的文件(由文件描述符指向),请说明如何生成错误消息.

c printf fwrite

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

调用free()会导致程序崩溃

我有一个非常奇怪的问题,试图在分配的内存上调用free导致我的程序崩溃.

这是相关的代码:

int i, count;
char *specifier;
char aisle[1];
count = 0;

/*Find name of the new item and assign to the name field of new_node...*/
for (i = 0; input[i] != ','; i++){
    count++;
}
specifier = (char*)malloc((count+1)*sizeof(char));
if (specifier == NULL){
    printf("Out of memory. Shutting down.\n");
    exit(EXIT_FAILURE);
}
for (i = 0; input[i] != ','; i++){
    specifier[i] = input[i];
}
specifier[count+1] = '\0';
new_node->name = specifier;
printf("%s\n", new_node->name);
free(specifier); /*PROGRAM CRASHES HERE*/
printf("boom\n");
specifier = NULL;
/*Function continues …
Run Code Online (Sandbox Code Playgroud)

c crash malloc free printf

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

为什么printf()在这段代码中打印出来?

这个简单的代码令我感到困惑 - 我故意打印出比传递给printf更多的整数.我预计会出错.我得到了奇怪的数字 - 它们来自哪里?

#include <stdio.h>
/* learn arrays */
void main(){
    int pout;
    pout = 6;
    printf("%i %i %i\n%i %i %i\n%i %i %i\n", pout);
}
Run Code Online (Sandbox Code Playgroud)

输出的一个例子:

6 608728840 0
-885621664 -885543392 608728816
0 0 -889304251
Run Code Online (Sandbox Code Playgroud)

单个数字不会随着重复运行而改变,但是大整数会改变.

c printf

-1
推荐指数
2
解决办法
134
查看次数

标签 统计

printf ×10

c ×8

java ×2

pointers ×2

add ×1

boolean ×1

c++ ×1

crash ×1

floating-point ×1

format ×1

free ×1

fwrite ×1

malloc ×1

number-formatting ×1

output ×1