Wikipedia presents a decent overview of formatted output for the printf-family of functions in this article. From my experience at least, the best way to go about it is to pass the width and precision settings as variables just to keep things as manageable and flexible as possible. Here's a simple example of creating left-justified output:I don't know how to explain but here is my program which I wrote and the problem is the column like ID Midterm Final, it doesn't fit with the score below
like the example does. how do I solve this? and how do I turn int into float?
#include <stdio.h>
#include <stdlib.h>
#define COUNT 6
int
main(void)
{
int
width = -12,
precision = 2;
const char*
categories[COUNT] = { "ID", "Midterm", "Final", "Lab", "Score", "Grade" };
float
values[COUNT] = { 1, 38.8294, 67.981779, 24.4185, 91, 100.635 };
for(size_t index = 0; index < COUNT; ++index)
printf("%*s", width, categories[index]);
printf("\n");
for(size_t index = 0; index < COUNT; ++index)
printf("%*.*f", width, precision, values[index]);
printf("\n");
return 0;
}

| Thread starter | Similar threads | Forum | Replies | Date |
|---|---|---|---|---|
|
|
How to Equalize 2 columns I & Q data of the 16 QAM constellation? | Programming & Languages | 0 |