1

In the number scanf() part of this code, if I enter input 01234567 , the printf() function prints 1234567 and it does not print the 0 . What to do to solve the problem?

#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;
}

for the number part , this code shows the output : 1234567 but input was 01234567

Expected output : 01234567

J...S
  • 5,079
  • 1
  • 20
  • 35
rhythm
  • 13
  • 3
  • Are you considering it as an octal number? – J...S Dec 28 '18 at 06:02
  • no , not a 7 digit number , I want to scan 11 digit . – rhythm Dec 28 '18 at 06:04
  • Please read documentation of [printf](https://en.cppreference.com/w/c/io/fprintf) and [scanf](https://en.cppreference.com/w/c/io/fscanf). You should use the return count of `scanf` – Basile Starynkevitch Dec 28 '18 at 07:12
  • regarding: `gets(name);` the function: `gets()` has been depreciated for years and completely removed from the latest versions of C. Your compiler should have told you this. Suggest using `fgets()` (see the MAN page for the details of `fgets()` – user3629249 Dec 28 '18 at 22:12
  • the leading 0 is not a valid part of an integer. Suggest replacing the field definition with an array of characters and using the '%s' input format specifier in the call to `scanf()` – user3629249 Dec 28 '18 at 22:14
  • OT: when calling any of the `scanf()` family of functions.1) always check the returned value (not the parameter values) to assure the operation was successful. 2) when using the input format specifiers: '%s' and/or '%[...]' always include a MAX CHARACTERS modifier that is 1 less than the length of the input field, because those format specifiers always append a NUL byte to the input. This avoids any possibility of a buffer overflow and the resulting undefined behavior – user3629249 Dec 28 '18 at 22:18

3 Answers3

2

There is no real difference between the numbers 1, 01, 0000001, 1.0, and so on(a). That's because the number is the value, not the formatting.

If you want to preserve leading zeros, you should probably be treating it as a string rather than a number.

That's especially true if you're talking about phone numbers (which seems to be the problem case at hand) since they can come in many forms that don't comply with a simple integer, such as:

0413 198 234
(0407) 123-456
0412 diablo

You should also be circumspect about gets(), there's a reason why it's one of the few things ever to have actually been removed from the standard - there is no safe way to use it. Instead, look into any of the many options that are far safer, such as here.


(a) In terms of mathematical quantities, obviously. They may be treated differently in C source code in terms of data type or radix used.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2
printf("Number: %011ld\n",number);

This will prefix 0 to make it eleven character as required in OP. Hence 01234567 will become 00001234567

Rizwan
  • 3,324
  • 3
  • 17
  • 38
1

To print leading zeros, code can record the length with "%n" of the numeric input for subsequent printing.

printf("Enter mobile number\n");

int n1 = 0;
int n2 = 0;
//     v------------ consume leading spaces.
//     |vv---vv----- record offset of scan
scanf(" %n%ld%n",&n1, &number, &n2);
//               v--------------- pad with zeros as needed
//               |v-------------- width determined by argument
printf("Number: %0*ld\n", n2-n1, number);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256