Use of the conversion specifier % g in C language

Source: Internet
Author: User
Tags float max

Question:

Compile a function, return the difference between the maximum and minimum values in a double array, and test the function in a simple program.
It's easy to have no sense about the question, but it's just a bit confusing, because the answer is

The code is as follows: Copy code

Printf ("The gap between max and min is: % gn", gap (array, WIDTH ));

What is % g? I did not record the notes in my notes-specifiers, but I did not go into the chapters in the book for details. I just classified it as a floating point.

Result output:

2.6
9.2
The dif between max and min is: 6.6

There is no extra 0, and it looks quite comfortable.

So, I understood % g, which is a good thing.

The full version I wrote:

The code is as follows: Copy code

# Include <stdio. h>
# Define WIDTH 6
Float dif (double [], int num );
Int main (void)
{
Double array [] = {4.3, 5.3, 2.6, 9.2, 2.8, 3.6 };
Printf ("The dif between max and min is: % g", dif (array, WIDTH ));
Return 0;
}
Float dif (double array [], int num)
{
Int I;
Float max, min;
For (I = 0, max = * array, min = * array; I <num; I ++ ){
If (* (array + I)> max)
Max = * (array + I );
If (* (array + I) <min)
Min = * (array + I );
 }
Printf ("% gn", min );
Printf ("% gn", max );
Return max-min;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.