Warning: sizeof(): Parameter must be an array or an object that implements Countable in /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wordpress-firewall.php on line 245

Warning: sizeof(): Parameter must be an array or an object that implements Countable in /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wordpress-firewall.php on line 245

Warning: Cannot modify header information - headers already sent by (output started at /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wordpress-firewall.php:245) in /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 62
Most Common Formatting example in C: restrict decimal points – Coal Geology and Mining: Consulting Services

Most Common Formatting example in C: restrict decimal points

//Most Common Formatting example in C: restrict decimal points
//this program illustrates how C performs divisions and why we should be careful
//declaring variable types as int / float etc.

#include <stdio.h>
int main(void){
signed char sister;
short romance;
int age;
long life;
long long dino;
float true_age, true_romance;
float right_answer, truly_right_answer;
int wrong_answer;

romance=4; //average romance per person!! kidding!
life=90; //average life of a person;

true_age=100;
true_romance=4;

wrong_answer=life/romance;
right_answer=life/romance; ///right answer is declared as float; would produce wrong answer; see output
//fix:
truly_right_answer=true_age/true_romance;

// romance and life are both “integer variables”
//so, life/romance would only produce an integer.
printf(“INT life/romance %d\n”, wrong_answer);
printf(“FLOAT life/romance %f\n”, right_answer);
printf(“FLOAT true_life/true_romance %f\n”, truly_right_answer);
printf(“FLOAT true_life/true_romance %.2f\n”, truly_right_answer);
printf(“FLOAT true_life/true_romance %10.2f\n”, truly_right_answer);
printf(“FLOAT true_life/true_romance %.3f\n”, truly_right_answer);
printf(“FLOAT true_life/true_romance %10.3f\n”, truly_right_answer);
//Now, lets add some control to the decimal point display to the output screen.
printf(“The size of signed char is %d\n”, sizeof(sister));
printf(“The size of short is %d\n”, sizeof(romance));
printf(“The size of int is %d\n”, sizeof(age));
printf(“The size of long is %d\n”, sizeof(life));
printf(“The size of long long is %d\n”, sizeof(dino));
return 0;
}

formatting in CC progra

Leave a Comment

Your email address will not be published. Required fields are marked *