In this example the program will print a table with 2 columns and 2 rows using the function "printf()".
You need to use a font with same width on all characters for it to look good.
I'll need to include these files
#include <stdlib.h>
#include <stdio.h>
I'll be making a simple console application with entry point "main()"
int main()
{
I'll be using these variables
char format[20];
char str1[10];
char str2[10];
Here I write a format string to hold the format of an entry in the table, one line. It will be 2 columns of 8 characters width.
sprintf_s(format, 20, "%c%ds %c%ds\n", '%', 8, '%', 8);
Here I give the test variables some value so I have something to write to the screen!
sprintf_s(str1, 10, "Hello!!");
sprintf_s(str2, 10, "World!!");
Now I print a row of the table using the format string
printf(format, str1, str2);
Now I give some new values for the next row
sprintf_s(str1, 10, "Hello");
sprintf_s(str2, 10, "World");
Printing row 2 to the screen
printf(format, str1, str2);
This is only to stop the program so you can see the output, press any key to continue
getchar();
}
Things that could be done:
¤ If you are using many strings, ex str_1, str_2, srt_3, ..., str_n, you could check the width on all the strings with "strlen()" and then take the highest value to be the column width.