# How would i align to the left using strings in C?

Page 1 of 1

## 4 Replies - 9223 Views - Last Post: 16 May 2010 - 07:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=173920&amp;s=5503ea25cc065884fa4b2694252969ff&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Cloudyeyes

Reputation: 1
• Posts: 65
• Joined: 15-May 10

# How would i align to the left using strings in C?

Posted 16 May 2010 - 06:46 PM

Whenever I type in a bit of code to print a string out, it'll automatically align to the right. However, I want the strings to align to the left.

For example, if I do printf("%s", stringvalue); //with stringvalue = abcdef, with length 9, etc...
It'll align abcdef to the right.

How do i change it so it aligns to the left?
Is This A Good Question/Topic? 0

## Replies To: How would i align to the left using strings in C?

### #2 David W

• DIC supporter

Reputation: 281
• Posts: 1,788
• Joined: 20-September 08

## Re: How would i align to the left using strings in C?

Posted 16 May 2010 - 07:07 PM

Cloudyeyes, on 16 May 2010 - 07:46 PM, said:

Whenever I type in a bit of code to print a string out, it'll automatically align to the right. However, I want the strings to align to the left.

For example, if I do printf("%s", stringvalue); //with stringvalue = abcdef, with length 9, etc...
It'll align abcdef to the right.

How do i change it so it aligns to the left?

Try searching under C strings left align

- Causes printf to left-align the output of this placeholder (the default is to right-align the output).

This post has been edited by David W: 16 May 2010 - 07:11 PM

### #3 #define

• Duke of Err

Reputation: 1368
• Posts: 4,735
• Joined: 19-February 09

## Re: How would i align to the left using strings in C?

Posted 16 May 2010 - 07:11 PM

```#include <stdio.h>

int main()
{

printf ("%10s\n",  "Right");

printf ("%-10s\n", "Left");

printf ("%-10s\n", "   abcdef");

printf ("%-10s\n", "abcdef   ");

getchar();

return (0);
}

```

Output.
```     Right
Left
abcdef
abcdef

```

### #4 Cloudyeyes

Reputation: 1
• Posts: 65
• Joined: 15-May 10

## Re: How would i align to the left using strings in C?

Posted 16 May 2010 - 07:27 PM

#define, on 16 May 2010 - 06:11 PM, said:

```#include <stdio.h>

int main()
{

printf ("%10s\n",  "Right");

printf ("%-10s\n", "Left");

printf ("%-10s\n", "   abcdef");

printf ("%-10s\n", "abcdef   ");

getchar();

return (0);
}

```

Output.
```     Right
Left
abcdef
abcdef

```

Thanks! Got it working! Exactly what I was looking for.

### #5 #define

• Duke of Err

Reputation: 1368
• Posts: 4,735
• Joined: 19-February 09

## Re: How would i align to the left using strings in C?

Posted 16 May 2010 - 07:34 PM

Cloudyeyes, on 17 May 2010 - 03:27 AM, said:

Thanks! Got it working! Exactly what I was looking for.

Cool