delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/08/21:11:38

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Centering printf text on a 80 column screen
Date: Tue, 08 Apr 1997 05:41:41 -0700
Organization: Two pounds of chaos and a pinch of salt
Message-ID: <334A3D05.230D@cs.com>
References: <334aed58 DOT 5263168 AT news DOT mindspring DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp205.cs.com
Mime-Version: 1.0
Lines: 44
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

dabrownz AT mindspring DOT com wrote:
> 
> How do you center printf text on a DOS screen?  I can't find anyway to
> do this other than using tabs and spaces (very annoying).  What is the
> 'correct' way to do it?  Do I have to use 'cprintf' or some other
> keyword or include another library?  I'd would really appriciate any
> help, Thanks.

Here's probably the easiest way to do what you describe:

    char line[256];
    sprintf( line, format-string, args );
    printf( "%*s\n", screen_width / 2 + strlen( line ) / 2, line );

You could put this in a function that could be called just like the regular printf() like so:

#include <stdio.h>
#include <stdarg.h>

#define SCREEN_WIDTH 80

int printf_centered( const char *fmt, ... )
{
    char line[256];
    va_list v;

    va_start( v, fmt );
    vsprintf( line, fmt, v );
    va_end( v );

    return printf( "%*s\n", (int) ( SCREEN_WIDTH / 2 + strlen( line ) / 2 ),
                   line );
}

If I had some more time I could probably come up with a more elegant way,
but this should be fine for most purposes.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I      |   mailto:fighteer AT cs DOT com   |
| God's final message to His Creation: | http://www.cs.com/fighteer |
| "We apologize for the inconvenience."| Fight against proprietary  |
|                      - Douglas Adams | software - support the FSF!|
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019