0

I made a simple program:

#include <stdio.h>

int main()
{
    printf("Please enter a value: ");
    int uservalue;
    scanf("%d", &uservalue);
    printf("The value entered was: %d", uservalue);
    return 0;
}

As you probably already know, this program has many faults. The ones I found are:
1) The program malfunctions weirdly if the user does not enter a number.
2) Large numbers, like 1000000000, are stored as negative numbers.

Why are these problems occurring and how can I overcome them?

Thanks for any suggestions,
popcorn99

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by dkalita
0

1) The program malfunctions weirdly if the user does not enter a number.

its because scanf() is expecting u to enter some integer numbers.
If u want it to work for any input then better take your input as string, validate it and get its' integer value to your variable.

2) Large numbers, like 1000000000, are stored as negative numbers.

it's because u are storing a number greater than the capacity and by default the int data type is signed int hence the most significant bit is the sign bit and the number u are trying to store have that bit as 1 which says its a negative number.
If u want larger numbers u can use unsigned long datatype.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.