-2

Im learning from start C basic.And i dont understand character wide.When i change it nothing visual happens,so i dont know what is purpose of this

I was researching and at first i discovered that its not string length but character datatype,but more info i wasnt able to find

 main()
 {
     float farh, celsium ;
     int lower, upper, step;

     lower = 0;
     upper = 300;
     step = 20;

     farh = lower;
     while(farh <= upper) {
         celsium = (5.0/9.0) * (farh - 32.0);
         printf( "%5.2f %5.2f\n",farh, celsium);
         farh = farh + step;
     }
 }

output is temperature in celsuim and fahrenheit (0.000000 -17.777779)

in the code above

%5.2f 

represent each argument to be alerted with character wide at least 5 and with 2 numbers after decimal point. But what changes if i write 2, 6 or other number instead of 5?

Dakuha
  • 19
  • 4
  • Be aware that "wide characters" in C don't refer to what you're presently asking about. Instead, it has to do with Unicode 16-bits encoded characters. Check out this entry : https://stackoverflow.com/questions/11287213/what-is-a-wide-character-string-in-c-language – Obsidian Aug 06 '19 at 22:11

2 Answers2

0
 printf("%5.2f\n", 3.223467856);

Consider the above statement:

printing with %5.2f will generate : _3.22 ( the symbol _ represents a blank space at the beginning, won't be printed in console)

5: The number will occupy space of 5 character wide. (.) dot occupies 1 char space too)

2: The number will occupy 2 digits after decimal point(.)

printf("%6.2f\n", 3.223467856);

printing with %6.2f will generate : __3.22

printf("%6.3f\n", 3.223467856);

printing with %6.2f will generate : _3.223

A.N.Tanvir
  • 76
  • 3
0

If you modify the program (with some cleanup):

    #include <stdio.h>
    #include <stdlib.h>

    int main(void) {
        int const lower_limit = 0;
        int const upper_limit = 300;
        int const step = 20;

        float fahrenheit = lower_limit;
        while (fahrenheit <= upper_limit) {
            float const celsius = (5.0f / 9.0f) * (fahrenheit - 32.0f);
            printf("%9.6f\t%10.6f\n", fahrenheit, celsius);
            fahrenheit += step;
        }

        return EXIT_SUCCESS;
    }

You'll see the difference between %9.6f and %10.6f which should show something like:

 0.000000   -17.777779
20.000000    -6.666667
40.000000     4.444445
60.000000    15.555556
80.000000    26.666668
100.000000   37.777779
120.000000   48.888893
140.000000   60.000004
160.000000   71.111115
180.000000   82.222229
200.000000   93.333336
220.000000  104.444450
240.000000  115.555557
260.000000  126.666672
280.000000  137.777786
300.000000  148.888901

You see a difference in behaviour when fahrenheit goes from 0 to 20 vs from 80 to 100.

Bo R
  • 2,334
  • 1
  • 9
  • 17