Thread: string format specifier

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    string format specifier

    I just have a question about the string ("%s") format specifier.

    is it possible to limit the amount of characters printed with %s? for example, I tried this code, but it did not work, it printed the whole string:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char str[] = "blehbleh";
    	printf("%4s", str);
    
    	return 0;
    }
    is this possible, or am I just doing it wrong?

    any help is appreciated. thank you in advance.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The number is a minimum. To limit the amount, you have to make a copy of the string and insert a NUL at the right place.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not exactly true. You could do something like this.
    Code:
    #include <stdio.h>
    
    int main( ) {
        printf("%.4s\n", "bleh is a lousy word");
        return 0;
    }
    $ ./foo
    bleh

    Of course where you get the number is completely up to you, even if it's magic. You can write an asterisk instead: %.*s and specify one later in printf's argument list.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Indeed. Didn't know that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    Quote Originally Posted by citizen
    Not exactly true. You could do something like this.
    Code:
    #include <stdio.h>
    
    int main( ) {
        printf("%.4s\n", "bleh is a lousy word");
        return 0;
    }
    $ ./foo
    bleh

    Of course where you get the number is completely up to you, even if it's magic. You can write an asterisk instead: %.*s and specify one later in printf's argument list.
    aha. you have to add the precision point. thats what was wrong, I wasn't adding it. thanks citizen .

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM