Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C printf char more than or equal to 8 character in a row manner

Tags:

c

char

if (namelist==NULL)
{
   namelist=(char**)malloc(sizeof(char**));
   namelist[i]=name;
}
else
{
   namelist=(char**)realloc(namelist,(i+1)*sizeof(char**));
   namelist[i]=name;
}

for(i=0;i<count;i++)
{
   printf("%s\t\t%s\n",namelist[i],namelist[i]);
}

The problem is if I enter "abcdefg", "abcdefgh" and "abc" as input, I will get

abcdefg          abcdefg
abcdefgh                    abcdefgh
abc              abc

Is there any way to get the second "abcdefgh" to get in line as the second "abcdefg" and "abc"?

like image 667
Jack Avatar asked Nov 23 '12 13:11

Jack


1 Answers

stop using TAB characters (\t) as separators, use proper format specifications instead, if you want your strings to start at 20th column, write:

printf( "%-20s%s", namelist[i],namelist[i]);
like image 154
lenik Avatar answered Sep 29 '22 03:09

lenik