Question

I am reading a piece of C heap sorting code and encounter this:

 do{
    printf("\n\t\t1:INSERT\n");
    printf("\n\t\t2:SEARCH\n");
    printf("\n\t\t3:DELETE\n");
    printf("\n\t\t1:DISPLAY\n");
    printf("Enter your choise\n");
    scanf("%d",&choise);
    switch(choise)
    {
        case 1: printf("Enter value to insert\n");
                scanf("%d",&val);
                last=insert(root,val);
                break;
        case 2:printf("Enter value for search \n");
                scanf("%d",&val);
                search(root,val);
                break;
        case 3:delete(root);
                delete(last);
                break;
        case 4:printf("\n\tHEAP\n");
                display(root);
                break;
        default : printf("INVALID choise ... can't you see properly?\n");

    }

Anyone knows whats \t1 and \t2 in the printfs and how do they work? I tried google but did not get any useful information. Thank you.

Was it helpful?

Solution

\t denotes a tab character. The 1 and 2 are nothing to do with it; they are just literal 1 and 2.

OTHER TIPS

You won't find any mention of \t in any printf documentation because it has nothing to do with printf. It's part of the syntax of a string literal, similar to the \n that precedes it. \n denotes a new-line character; \t denotes a tab character. printf never even sees the '\' characters, which are replaced at compile time; it just sees the new-line and tab characters, which it treats like any other ordinary characters that aren't part of any %... sequence. (There may be some special treatment of these particular characters, but that's done by lower-level routines that are called by printf.)

And \t1 denotes a tab character followed by a digit 1.

(An aside: Unix-like systems have a printf command, used from a shell prompt, that behaves similarly to the C printf function. It does handle \ sequences itself.)

The specifiers are just \t, which is the tab character. The numbers are part of the literals printed.

\t is just used to print a horizontal tab. The numbers will be printed as they are, and are not part of the \t. I.e:

printf("\n\t\t1:INSERT\n");
printf("\n\t\t2:SEARCH\n");
printf("\n\t\t3:DELETE\n");
printf("\n\t\t1:DISPLAY\n");
printf("Enter your choise\n");

will print something like:

    1:INSERT

    2:SEARCH

    3:DELETE

    1:DISPLAY
Enter your choise

\t is tab. It prints tab character and then you print 1. Its like doing /n1. It means leave a line and then print 1.

\t  -----> I am a tab character that prints spaces.

1   -----> I am a number.

We both are not a single character.

\t1 -----> Print tab character and then print 1. 

\t2 -----> Print tab character and then print 2. 

Get that now?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top