简体   繁体   中英

why my c program doesn't crash when i entered the wrong data type

I'm new to c and I'm writing a basic program that ask the user for input

#include <stdio.h>

int main(void){
    float weight;
    printf("enter your weight in kg:\n");
    fflush(stdout);
    scanf("%f", &weight);
    printf("your weight is %f kg\n", weight);
    return 0;
}

Everything works as expected when I under a number when asked for input. But when I accidentally entered words, the program doesn't crash and instead print out your weight is 0.00000 kg .

I was just wondering, why doesn't my program crash when it expect numbers but I have entered words? and why did it printed out 0.00000 ?

why my c program doesn't crash when i entered the wrong data type

When a user of your program enters some input that can't be converted by scanf to a float, the variable weight will not be initialized. So when the program prints the variable, it will read an uninitialized float. According to the c standard, that has undefined behavior.

However, undefined behavior does not guarantee a program crash. It may crash or it may continue execution. We can't know as it is undefined what will happen. On your system execution just continued but on another system a program may happen.

On many systems the memory allocated to a program as startup is initialized to all zeros. Therefore variables in main is (often) an all zero binary pattern when left uninitialized. For floats that translate to the value 0.0

So even if you don't initialize weight it seems as if it was initialized to zero and your program runs fine even with input like abc and prints the weight as 0.0

However, from a standard point of view it is still undefined behavior to read an uninitialized float variable

If you did the same inside a function that you called a number of times (with other function calls in between), you would see strange behavior sooner or later. Typically just a print of strange values but on some systems, you might see a crash (but that is more rare).

You should do:

if (scanf("%f", &weight) == 1)
    printf("your weight is %f kg\n", weight);
else
    printf("Illegal input\n");

Every input the user gives in C is treated as a string, though the input you provide is in numbers. After getting the input from the user, it is converted in to numeric data using atoi (ascii to integer) function ( man atoi ). The return value of this function is the converted value. Upon successfully converting the given string to an integer it returns the converted integer value and it returns zero when the resultant value is a non-integer. This is the reason why it is not showing any error when you input different data.

Try the same code with different format specifiers, so that you can have a clear understanding.

The MAN page of scanf :

Return Value

These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

So, scanf return the number of arguments successfully scanned. that's why your program not crashing.

the program does not crash because the input does nothing to cause a crash.

However, the posted code does have a major flaw. This line:

scanf("%f", &weight);

should be:

if( 1 != scanf("%f", &weight) )
{
    perror( "scanf failed" );
    exit( EXIT_FAILURE );
}

// implied else, scanf successful

In other words, always check system functions for errors, especially those that are performing input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM