Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Solved Programs » Print string in the center of a line

C function to print string in center of a line using macro

/* This code can be saved in any file with .c extension. Program prints any string in the center of a line using macro. You can use your own logic to solve this. */


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

/* This is the macro. */

#define print_center( x, y, z ) printf( "%*s\n", ( ( z - y ) / 2 + y ), x );

int main( void )
{
     char str[] = "Welcome to CppBuzz.com";
     int cols, len;

     /* Set cols to the number of columns on your display. */

     cols = 80;
     len = strlen( str );

     /* Calling the macro looks just like calling a function. */

     print_center( str, len, cols );
     
     return 0;
}

Printing String in center of a line

c program to print string in center of a line