LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-12-2008, 07:46 AM   #1
zebela
LQ Newbie
 
Registered: May 2008
Posts: 7

Rep: Reputation: 0
Minus sign in printf format string doesn't work.


Hi every body,

I am migrating C code from HP-UX to LINUX RHEL5.
Minus sign flag for left justifying in printf doesn't work.

Could you have a look:

On the HP-UX side :

[gidtdev/oracle-gidtdev2]/users/devt/gidt/src/pc>uname -r
B.11.23
[gidtdev/oracle-gidtdev2]/users/devt/gidt/src/pc>uname
HP-UX
[gidtdev/oracle-gidtdev2]/users/devt/gidt/src/pc>more format.c
#include <string.h>
#include <stdio.h>

main( )
{

char chaine[ 5 ];

strcpy( chaine, "-----" );

printf( "|%10-s|\n", chaine );
printf( "lg=%d\n", strlen( chaine ) );

exit( 0 );
}
[gidtdev/oracle-gidtdev2]/users/devt/gidt/src/pc>cc format.c -o format
[gidtdev/oracle-gidtdev2]/users/devt/gidt/src/pc>format
|----- |
lg=5


----------On the LINUX REHL5 SIDE NOW

[gidtdevt.latelec.gl.local/gidt]/users/devt/gidt/src/pc>
uname
Linux
[gidtdevt.latelec.gl.local/gidt]/users/devt/gidt/src/pc>
more format.c
#include <string.h>
#include <stdio.h>

main( )
{

char chaine[ 5 ];

strcpy( chaine, "-----" );

printf( "|%10-s|\n", chaine );
printf( "lg=%d\n", strlen( chaine ) );

exit( 0 );
}

[gidtdevt.latelec.gl.local/gidt]/users/devt/gidt/src/pc>
gcc format.c -ldl -lm -o format

[gidtdevt.latelec.gl.local/gidt]/users/devt/gidt/src/pc>
format
|%10-s|
lg=5[gidtdevt.latelec.gl.local/gidt]/users/devt/gidt/src/pc>

[gidtdevt.latelec.gl.local/gidt]/users/devt/gidt/src/pc>
gcc format.c -ldl -lm -o format

[gidtdevt.latelec.gl.local/gidt]/users/devt/gidt/src/pc>
format
|%10-s|
| -----|
lg=5[/i][/b]

Can anybody help ?

I wish to avoid changing the code.

I hope it exists some compilation options.
 
Old 06-12-2008, 08:26 AM   #2
GazL
Senior Member
 
Registered: May 2008
Posts: 3,312

Rep: Reputation: Disabled
The '-' is in the wrong place. Flags should come before the width. Try

printf( "|%-10s|\n", chaine );
 
Old 06-12-2008, 07:03 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
I've always been taught that order of options as well, eg see: http://www.space.unibe.ch/comp_doc/c...NS/format.html
Maybe the HP compiler is smart enough (relaxed enough ?) to handle your original order...
 
Old 06-13-2008, 01:25 AM   #4
zebela
LQ Newbie
 
Registered: May 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,

printf( "|%-10s|\n", chaine ); actually works!

Blimey! Something was for me more left justified in 10- than in -10... What a mistake!

I need to change all my code.

Thank you both of you ( Gazl and chrism01 ).

Have a nice day.

Alain.
 
Old 06-13-2008, 01:32 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
In the future, could you enclose code examples in code blocks? Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
delete a file that starts if a "-" (minus) sign Four Linux - Newbie 2 03-14-2008 04:55 PM
Removing a directory beginning with a minus sign. harry edwards Linux - General 4 03-01-2008 05:44 PM
C string and printf question exvor Programming 7 12-10-2005 10:53 AM
how to delete file which name start with minus sign? guillaume31 Linux - General 3 01-27-2005 07:15 AM
std string class and printf The_Nerd Programming 3 06-28-2004 10:46 PM


All times are GMT -5. The time now is 06:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration