简体   繁体   中英

C program not running but getting compiled with no errors

this is the program in question:

#include <stdio.h>
#include <tgmath.h>
#include <simpio.h>

long main(){
    long cars_t,years;
    cars_t=80000;
    years=0;
    while (cars_t<=160000){
        cars_t=ceil(cars_t*(1+7/100));
        years+=1;
    }
    printf("Xronia:%ld\n",years);
    printf("Arithmos Autokinhton:%ld\n",cars_t);
}

Just an extremely simple program with a while function. But for some reason it doesn't give any output at all. What i have noticed however is that, the moment i remove the while function(and the code inside of it) the program runs perfectly fine. Can anyone tell me how to fix this? thanks in advance.

This is because you have declared cars_t as an integer (long) value, and 7/100 which are also also integers and so evaluate to zero. Hence you get stuck in the loop as cars_t does not increase.

Instead you want cars to be a floating value and force 7/100 to be evaluated as floating:

#include <stdio.h>
#include <tgmath.h>

long main(){
    long years;
    double cars_t;
    cars_t=80000;
    years=0;
    while (cars_t<=160000){
        cars_t=ceil(cars_t*(1+7.0/100));
        years+=1;
    }
    printf("Xronia:%ld\n",years);
    printf("Arithmos Autokinhton:%lf\n",cars_t);
}

https://onlinegdb.com/vNusyZ9xD

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