1

Although I have solved my question, but while doing so I came across unexpected behaviour of \t. where I'm getting different tab length for single \t.Here is my code:

  #include<stdio.h>

int calculate_intpart_qutent(int dividend, int diviser);
int main()
{

    int a;
    int b;
    int choice;
  
    
    do
    {
        printf("Enter the Dividend (a) :\t");
        scanf("%d", &a);

        printf("\nEnter the Divisor (b) :\t");
        scanf("%d", &b);

        printf("\n\nThe quotient is:\t%d", calculate_qutent(a, b));
        
        printf("\n\nDo you want to try again?(Y\\N):\t");
        choice = getchar();

        if (choice == '\n') choice = getchar();
        printf("\n\n\n");

    } while (choice=='y'|| choice=='Y');
    

 }

int calculate_intpart_qutent(int dividend, int diviser)
{
    return (dividend/diviser);

}

Here is my output:

enter image description here

Since I have used single tab in both of the first printf statements, why I'm getting different tab length on my output screen? I'm I doing something wrong here?

I'm using Visual Studio 2017 RC.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rishi Shukla
  • 306
  • 1
  • 3
  • 13

2 Answers2

7

Since I have used single tab in both of the first printf statements, why I'm getting different tab length on my output screen?

Using tab won't guarantee the number of printed spaces.

If you want fixed length in printf then try something like %-30s instead. It guarantees 30 spaces for the string being printed with - means left aligned.

    printf("%-30s", "Enter the Dividend (a) :");
    scanf("%d", &a);

    printf("\n%-30s", "Enter the Divisor (b) :");
    scanf("%d", &b);
Community
  • 1
  • 1
artm
  • 17,291
  • 6
  • 38
  • 54
4

A '\t' character will align text to the next '\t' stop. In your case, Tab stops at every 8th character.

printf("Enter the Dividend (a) :\t");

Number of characters before '\t' is equal to 24. so next '\t' will be aligned at 8*4= 32nd Position.

printf("\nEnter the Divisor (b) :\t");

Number of characters before '\t' = 23. So next '\t' will be aligned at 8*3 =24th position.

This is not an issue and you are not doing wrong. You need to understand how tab behaves in terminal (As user can change the tab width). You can remove the '\t' and can use fixed length in printf statment like mentioned in @artm answer.

Rocoder
  • 1,083
  • 2
  • 15
  • 26