Click here to Skip to main content
14,882,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a very small code in C which basically takes input from user, save in variable and print it out the same input given by user to screen.
But, When I entered "9879041552" it printed out really strange number. Although, some numbers are working fine, the numbers which are limited to 4 digits.
What's causing this unexpected behavior?

C++
#include 
#include 

int main()
{
    int mobileNumber;
    printf("Enter your mobile number: ");
    scanf("%d", &mobileNumber);
    printf("Your mobile number is: %d", mobileNumber);
    
return 0;
}


What I have tried:

I am beginner to programming. I tried searching online but it doesn't give me any reason why this happened.
Posted
Updated 1-Oct-18 21:08pm
v2

1 solution

The number is bigger than an integer (with 4 bytes) which holds up to 2^31-1 and a bit for the sign.

Use for phone numbers a char array often named string.

Google for some tutorials. ;-)
   
Comments
CPallini 2-Oct-18 3:17am
   
5.
Member 14004425 2-Oct-18 3:23am
   
Thanks I understood.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900