1

I'm new at C programming.

What I am looking to do is to find a way to freeze the " | " that are in the printf statements, mainly the ones to the right of the code.

What happens is that as you can see in the code if I put anything inside those printf statements that generate a variable amount of characters it moves the " | " accordingly to the amount of characters it generates.

So I'm hoping to see if someone could help me with this, so that what ever is generated within the printf does not shift the position of the " |".

Here's the code so that you guys have an idea f what I'm talking about.

#include <stdio.h>

int main ()
{

    char Employee1_NameF[20];
    char Employee1_NameL[20];

        printf("Employee Name: ");
        scanf("%s %s", &Employee1_NameF,&Employee1_NameL);

    printf(" __________________________________________________________________________________________________ \n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|--------------------------------------------------------------------------------------------------|\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");;
    printf("|__________________________________________________________________________________________________|\n");
    printf("|--------------------------------------------------------------------------------------------------|\n");
    printf("|         THE FACE OF THIS DOCUMENT HAS A MULTICOLORED BACKGROUND ON WHITE PAPER                   |\n");
    printf("|--------------------------------------------------------------------------------------------------|\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                 %s %s            |\n",  Employee1_NameF, Employee1_NameL);
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|                                                                                                  |\n");
    printf("|__________________________________________________________________________________________________|\n");
}
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Alfredo
  • 35
  • 1
  • 4

1 Answers1

3

I won't spoil your learning fun, just giving some hints.

strlen and %<width>s and %<-width>s specifiers in printf format strings are your friends. Reading about them may help you.

For example compare the output of

printf("<%8s%8s>\n", "foo", "bar");

v/s

printf("<%-8s%-8s>\n", "foo", "bar");

v/s

printf("<%s%s>\n", "foo", "bar");

and use some mathematics.

Another way to achieve the functionality is to prepare your fixed length strings to be printed on the fly.

You may also want to read Centering strings with printf()

Community
  • 1
  • 1
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • Thank you so much for the help it really helped me a lot. – Alfredo Mar 30 '16 at 14:04
  • As a side note, learning about intricate printf format specifiers or other stdio details is likely a huge waste of your time, unless maybe you are aiming for a career as Linux programmer. Outside Linux, very few professional programs use stdio.h for production code. – Lundin Mar 30 '16 at 15:01