The output should be "the wallet belongs to Warren Buffett with balance of 85500000000" but I am getting "The wallet belongs to Warren Buffett with balance of 0.000000". Can you please let me know where am I making the mistake?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *owner;
long double balance;
} Wallet;
int main(int argc, char **argv) {
const char *owner = "Warren Buffett";
Wallet *wallet = (Wallet *) malloc(sizeof(Wallet));
wallet->owner = (char *) malloc(strlen(owner) * sizeof(char));
strcpy(wallet->owner, "Warren Buffett");
wallet->balance = 85500000000;
printf("The wallet belongs to %s with balance of %Lf",
wallet->owner, wallet->balance);
return 0;
}
(0.1 + 0.2) == 0.3isfalse). Better to use the smallest quantity (e.g. cents or 1/00 of a cent or whatever is appropriate) and use well established banking rounding rules when multiplying or dividing.free()what youmalloc(). In the code above (no recursion, no loops, no functions exceptmain(), ...) there's no big harm by leaving the memory marked as "in use" when the program terminates.