Akshay Pethani Akshay Pethani - 24 days ago 5x
C Question

'\t' character space differance in same printf() function in array

I've written this C code and compile in GNU GCC and TURBO C compiler both in windows 8.1. The code have float array of size 20 and print the array using for loop and '\t' character put into the printf() function into the for loop for spacing purpose.
In the entire loop in 20 times the '\t' should print equal spaces but it print only 2 space for value of a[i] 0 to 10 and regualr space in remaining execution.
I cannot understand why this happen. Does it is happen due to I use after printing float variable?

the code C code is as shown below:

#define n 20

int main()
float a[n]={0.1,0.9,0.23,0.8,0.32,0.57,0.4,0.14,0.25,0.11,0.7,0.86,0.75,0.19, 0.55,0.95,0.34,0.29,0.64,0.45},ex,temp,x;
int i,idx;

/*for(i=0 ; i<n ; i++)
printf("a[%d]: %f\t",i,a[i]);

for(i=0 ; i<n ; i++)

printf("\na[%d]: %f \t idx: %d",i,a[i],idx);
//printf("\na[%d]: %f *\t idx: %d",i,a[i],idx);

return 0;

The Image of O/p is as below:

enter image description here

As per o/p shown in Image the space in red lined area is lesser than blue lined area. How this possible? what is the reason of it?


\t is just a single character. Your terminal will decide what to do with it, normally it means "move on to the next tab stop position", a common convention is to have such a position every 8 columns. You can't control in your program what your terminal will do with it.

If you want to control your output formatting, use the features provided by printf() instead. The conversions allow to specify field widths and precisions, use these, see for example here: http://www.cprogramming.com/tutorial/printf-format-strings.html