标签: printf

为什么在引用int变量的大小时我必须使用%ld?

我像这样定义一个int:

int a;
Run Code Online (Sandbox Code Playgroud)

当我想查找这个int的大小时,我必须使用格式说明符%ld,如下所示:

printf("int size is %ld\n", sizeof(a));
Run Code Online (Sandbox Code Playgroud)

当我使用%d作为格式说明符时,我收到以下错误:

foo.c:7:10: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
    printf("int size is %d\n", sizeof(a));
Run Code Online (Sandbox Code Playgroud)

问题是,当函数的参数为​​int时,为什么sizeof()的结果定义为long unsigned int?

c printf

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

C程序Printf函数显示unsigned long long类型的异常行为?

请考虑以下程序并观察输出.

#include <stdio.h>
int main()
{
    unsigned long long val ;
    val =144111444250;
    printf ("%llu\n", val);
    printf ("%u %llu\n",val, val);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

   144111444250

   33 10211385600662044705
Run Code Online (Sandbox Code Playgroud)

在第一个printf中使用%llu会得到正确的结果,但在第二个printf中使用%llu会得到错误的结果.

c unix printf solaris

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

获得C中的"最高"字符

我试图写一个程序,它获得了一个人进入的最高性格.我制作了一个程序,它可以获得最高数量的工作,没有任何问题但是字符不起作用.这是我的代码:

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[]) {
    char characters[5];
    char highest = "a";

    printf("Please enter five characters: \n");
    for (int i = 0; i <= 4; i+=1) {
        scanf("%c", characters[i]);
    }

    printf("These are the characters you entered: ");

    for (int i = 0; i <= 4; i+=1) {
        printf("%c  ", characters[i]);
    }

    for (int i = 0; i <= 4; i+=1) {
        if (characters[i] > highest) {
            highest = characters[i];
        }
    }

    printf("\nThe highest …
Run Code Online (Sandbox Code Playgroud)

c printf for-loop

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

printf()是否修改了它的参数?

我试图看看不同语言如何处理浮点数.我知道浮点表示有一些固有的问题,这就是为什么如果你在Python中做0.3 + 0.6,你得到0.899999而不是0.9

然而,这些代码片段让我感到震惊:

double x = 0.1,
    sum = 0;

for(int i=0; i<10; ++i) 
    sum += x;

printf("%.9lf\n",sum);
assert(sum == 1.0);
Run Code Online (Sandbox Code Playgroud)

上面的代码片段工作正常.它打印1.0.但是,以下代码段会给出运行时错误:

double x = 0.1,
    sum = 0;

for(int i=0; i<10; ++i) 
    sum += x;

assert(sum == 1.0);
printf("%.9lf\n",sum);
Run Code Online (Sandbox Code Playgroud)

上面两个片段中唯一的变化是assert和printf语句的顺序.这让我觉得printf以某种方式修改了它的参数并以某种方式将它们四舍五入.

有人可以对此有所了解吗?

c++ precision printf assert floating-accuracy

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

是否可以在C中打印数组第一个元素的内存地址?

是否有可能printf在C中的数组的第一个元素的内存地址?

我尝试以下时编译器报告错误:

printf("i%/n", @array[0])
Run Code Online (Sandbox Code Playgroud)

我宣布的地方array[] = {23, 56, 78}.

c printf

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

我的代码有什么问题,我希望在int中打印输出,但似乎不是?

这是我的代码,我仍然继续看错了,我是初学者,我想学习:

#include<stdio.h>
int main()
{
    int a;

    printf("Input: ");
    scanf("%d", &a);
    printf("\n%d", &a);
    a+=2;
    printf("\n%d", &a);
    a+=4;
    printf("\n%d", &a);
    a+=2;
    printf("\n%d", &a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

Input: 10

-1078169908
-1078169908
-1078169908
-1078169908
Run Code Online (Sandbox Code Playgroud)

c printf

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

无符号整数溢出不会"环绕"

我从Integer Overflow Wiki读取以下行:

无符号整数溢出导致数量减去模2的幂,这意味着无符号整数在溢出时"环绕".

我有下面的代码,我试图创建一个哈希函数,并得到int溢出的情况.我试图通过使用缓解它,unsigned int但它没有工作,我能够看到负值.

我知道我可以通过其他方式处理它并且它可以工作,如我的代码注释所示 - Comment 2:.但这是正确的方式,为什么unsigned int没有环绕和溢出?

int hash(char *word) {
    char *temp = word;
    unsigned int hash = 0; // Comment 1: I tried to handle int overflow using "unsigned" int.
    while (*word != '\0') {
        // Comment 2: This works but I do not want to go this way. 
        //while ((hash * PRIME_MULTIPLIER) < 0) {
        //    hash = (hash * PRIME_MULTIPLIER) + 2147483647;
        //}
        hash = …
Run Code Online (Sandbox Code Playgroud)

c printf integer-overflow

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

解释fprintf()在程序中的奇怪行为,在文件中打印一些不寻常的字符

int index = 0;
FILE *fptr;
fptr = fopen("File.txt", "w");

char arr[] = {'A', '\b', 'B', '\b', 'C', '\b', '\b', '\n'};

while(arr[index] != '\0'){  
    fprintf(fptr, "%c", arr[index++]);
}
Run Code Online (Sandbox Code Playgroud)

这里从文件中获取的输出是

A|BS|B|BS|C|BS||BS|
hDC2¦v
Run Code Online (Sandbox Code Playgroud)

| BS | 是为了清晰起见,我这样写的退格字符.我无法理解为什么显示第二行输出.

c io printf file

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

如何在C中打印\\?(fprintf中)

已经多次询问如何打印一个\的问题.我找不到任何关于打印两个反斜杠(\\)的信息.

当我试着写这个: fputs("\\\\",w_ptr);

没有比一个更多的反斜杠.

如果你感兴趣:

这是一个自定义帐单编写程序,它使用csv feed创建Latex PDF.并且有很多双反斜杠表示新的换行符.

提前致谢!

c printf backslash

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

从C中的文件读取整数时遇到麻烦

我在尝试从文本文件中读取整数时遇到了麻烦:

#include <stdio.h>
#include <string.h>

int main()
{  
    int op;

    /* Open file for both reading and writing */
    FILE *d = fopen("intento1.txt", "r");
    FILE *f = fopen("bubbletry.txt", "w+");

    /* Read and display data */
    fread(&op, 4, 1, d);

    printf("%d\n", &op);
    fclose(d);
    /* Write data to the file */
    fprintf(f,"%d\n",&op);

    fclose(f);

    return(0);
}
Run Code Online (Sandbox Code Playgroud)

"intento1.txt"中的第一个数字是30771,但写在"bubbletry.txt"的文本是926363699.你能告诉我为什么会这样吗?

c printf fread

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