相关疑难解决方法(0)

如何使用printf格式化unsigned long long int?

#include <stdio.h>
int main() {
    unsigned long long int num = 285212672; //FYI: fits in 29 bits
    int normalInt = 5;
    printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

My number is 8 bytes wide and its value is 285212672l. A normal number is 0.
Run Code Online (Sandbox Code Playgroud)

我认为这个意想不到的结果是来自打印unsigned long long int.你怎么printf()unsigned long long int

c syntax printf format-specifiers long-long

356
推荐指数
8
解决办法
73万
查看次数

gcc:printf和long double导致输出错误.[C - 类型转换混乱]

我是C的新手.我尝试为Vector编写函数,但肯定有问题.
这是代码:

/* Defines maths for particles. */

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

/* The vector struct. */
typedef struct {
    long double x, y, z;
} Vector;

Vector Vector_InitDoubleXYZ(double x, double y, double z) {
    Vector v;
    v.x = (long double) x;
    v.y = (long double) y;
    v.z = (long double) z;
    return v;
}

Vector Vector_InitDoubleAll(double all) {
    Vector v;
    v.x = v.y = v.z = (long double) all;
    return v;
}


Vector Vector_InitLongDXYZ(long double x, long double y, …
Run Code Online (Sandbox Code Playgroud)

c gcc

4
推荐指数
2
解决办法
9850
查看次数

scanf() 如何在 printf() 之前执行

我有以下代码来实现pow()math.h 库中的 as 。嗯,我有两个问题:

  1. "power:"为什么 scanf() 语句在从 printf() 函数打印之前获取输入?
  2. 我如何计算大整数的幂,假设计算出的幂如下:22235645654789787978797797(仅作为示例)。我如何计算并打印它。

    #include <stdio.h>
    unsigned long long int pow(unsigned long long int n,unsigned long long int d);
    int main(){
    
    unsigned long long int a,x,n;
    printf("Number:");
    scanf("%u\n",&a);
    printf("power:");
    scanf("%u\n",&n);
    x = pow(n,a);
    printf("%u\n",x);
    }
    unsigned long long int pow(unsigned long long int n,unsigned long long int d){
    if(n+1==1)
        return 1;
    return d*pow(n-1,d);
    }
    
    Run Code Online (Sandbox Code Playgroud)

图像

在图像中,您可以看到变量的输入在从 printf()n打印之前获取输入"power:",因此无论您输入什么输入。

请帮助我理解它。欢迎任何建议和意见。

c

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

标签 统计

c ×3

format-specifiers ×1

gcc ×1

long-long ×1

printf ×1

syntax ×1