c - printf 中不存在扫描的 0

标签 c printf

在此代码的数字 scanf() 部分,如果我输入 01234567 ,则 printf() 函数会打印 1234567 而不会打印 0 。如何解决问题?

#include<stdio.h>
int main()
{
    long int number , date , month , year ;
    char name[50];

    printf("Enter name \n");
    gets(name);
    printf("Enter mobile number\n");
    scanf("%ld",&number);

    printf("Enter date of birth(date)\n");
    scanf("%ld",&date);

    printf("Enter date of birth(month)\n");
    scanf("%ld",&month);

    printf("Enter date of birth(year)\n");
    scanf("%ld",&year);

    ///printing starts
    printf("Name: %s\n",name);
    printf("Number: %ld\n",number);
    printf("DOB: %ld/%ld/%ld",date,month,year);
    return 0;
}

对于数字部分, 此代码显示输出:1234567 但输入是 01234567

预期输出:01234567

最佳答案

printf("Number: %011ld\n",number);

这将在 0 前面加上 OP 要求的 11 个字符。因此 01234567 将变为 00001234567

关于c - printf 中不存在扫描的 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53954283/

相关文章:

c - 使用静态指针的动态内存分配

c++ - 什么是 c printf %f 默认精度?

c++ - CL_UNSIGNED_INT8 和 CL_RGB 不兼容

arrays - 使用指向指针数组的指针进行合并排序不起作用

无法将字符串复制到 C 中的字符串数组

c - 64 位环境中的 printf/snprintf 行为

c - 将数字从文件复制到数组时使用 printf 时出现问题

c - 如何将结构体值传递到打印函数中,然后在 main 中调用它? - C语言

c - 从文本文件中读取 3 个字符串

C. 如何根据数字的大小从数字中获取前 N 位数字(例如数字 = 1234 n = 2)