0

AS the title. Any way to do the trick? Let's say I want to print "Hello World" in the center of the screen.

Newbie
  • 17
  • 1
  • 1
  • 4
  • Just use jQuery for that. [/meta] –  Mar 09 '13 at 15:37
  • 3
    @H2CO3: Really? *Really?* ;) – Linuxios Mar 09 '13 at 15:38
  • Not enough jQuery. :P – Tony The Lion Mar 09 '13 at 15:38
  • If your environment is linux, you may have a look at ncurses library which provides function for manipulating terminal (get width, height, move cursor to a position, draw windows, colors, etc.). – neodelphi Mar 09 '13 at 17:54
  • Most of them make is so much more complicated..... – TheBoringGuy Apr 18 '14 at 04:41
  • @neodelphi This is true but it doesn't (to my memory) have a way to centre text as a function; rather you have to use the functions mvprintw() etc. Still the function getmaxyx() is very useful and the other things you mention too of course. Terminals are anything but portable and if you're not careful the terminal can become insane .. You might say .. terminally insane :) (though there are ways round that and using `stay sane` is one way to help) – Pryftan Dec 02 '19 at 12:53

3 Answers3

2

You need to know how wide a space you need to centre the string in; you need to know how long the string is. You write an appropriate number of blanks, the string, and a newline.

#include <stdio.h>

int main(void)
{
    int width = 80;
    char str[] = "Hello world";
    int length = sizeof(str) - 1;  // Discount the terminal '\0'
    int pad = (length >= width) ? 0 : (width - length) / 2;

    printf("%*.*s%s\n", pad, pad, " ", str);
    return(0);
}

An exhaustive test program (up to width 80):

#include <stdio.h>
#include <string.h>

int main(void)
{
    int width = 80;
    char str[81];
    for (int i = 1; i <= width; i++)
    {
        memset(str, 'a', i);
        str[i] = '\0';
        int length = i;
        int pad = (length >= width) ? 0 : (width - length) / 2;
        printf("%*.*s%s\n", pad, pad, " ", str);
    }
    return(0);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • So much hassle just to show some texts at the center :O But its really helpful! I was actually wanting to printout an empty box at the center and in side the box there are texts. But i think this won't work in that circumstance. Thankss anyway! :) – Newbie Mar 09 '13 at 17:21
1

You can use the SetConsoleCursorPosition function. Get the width and height of the console window, then call it.

COORD coord;
coord.X = width / 2;
coord.Y = height / 2;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
Jong
  • 9,045
  • 3
  • 34
  • 66
  • 1
    This assumes the context of the question is a DOS console. That might be valid, but nothing has been provided to suggest this is the case. – mah Mar 09 '13 at 15:42
  • 6
    Just a note, this is Windows specific, will not work on Linux, OSX, etc. – Linuxios Mar 09 '13 at 15:42
  • @mah: I'm waiting for a response on that. – Linuxios Mar 09 '13 at 15:43
  • I am working with Windows. It did work but I don't want everything after the syntax at the center, I just want the first line to be at the center. Thanks though.. – Newbie Mar 09 '13 at 16:40
-1

You can use the gotoxy() function, like in C++, but in C, it is not pre defined so you should define it first. Another way is to just tab and newline (\t and \n...)

krato
  • 1,226
  • 4
  • 14
  • 30
  • Hmm used to use \t and \n but its kinda improper.. thanks for recommendation though. – Newbie Mar 09 '13 at 16:38
  • Maybe. But it should be noted that that function isn't portable. It's a DOS thing isn't it? Even if the OP was working under Windows it should still be noted. – Pryftan Dec 02 '19 at 14:43