-1
#include <stdio.h>

int main(void) {
  int Phone_number1,Phone_number2,Phone_number3 ; 
  printf("Enter phone number [(xxx) xxx-xxxx]: ");
  scanf("(%d) %d-%d" , &Phone_number1 , &Phone_number2 , &Phone_number3);
  
   printf("you entered: %d.%d.%d ",Phone_number1,Phone_number2,Phone_number3 );
  return 0;
}
Enter phone number [(xxx) xxx-xxxx]: (010) 7568-5230 // this was my input

you entered: 10.7568.5230 // this was my out put

display user phone number //my code

but i wanted to print like this 010.7568.5230

i don't find it out where is zero at the first of output

i think zero is same role as white space during computing so the first zero couldn't show up in my output

1
  • 1
    Try %03d.%04d.%04d instead of %d.%d.%d when you printf. Commented Mar 1, 2023 at 5:37

1 Answer 1

0

printf in C has formatting rules. Click_here for reference in regards to understanding printf in detail. Here I'll briefly outline with respect to your question.

"%03d %04d" is a formatting in printf will specify how your phone number will be printed.

  • d stands for decimal integer (not double!), so it says there will be no floating point or anything like that, just a regular integer.
  • 3 shows how many digits will the printed number have. More precisely, the number will take at least 3 digits: 010 will be 10 but adding 3 will place zero at the start. (similar is case for %04d
  • 0 before 3 shows that leading spaces should be replaced by zeroes.

Check similar explanation for sprintf.

Modified code:

#include <stdio.h>

int main(void) {
  int Phone_number1,Phone_number2,Phone_number3 ; 
  printf("Enter phone number [(xxx) xxx-xxxx]: ");
  scanf("(%d) %d-%d" , &Phone_number1 , &Phone_number2 , &Phone_number3);
  printf("you entered: %03d.%04d.%04d ",Phone_number1,Phone_number2,Phone_number3 );
  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.