string format specifier

This is a discussion on string format specifier within the C Programming forums, part of the General Programming Boards category; I just have a question about the string ("%s") format specifier. is it possible to limit the amount of characters ...

  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,893
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    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,893
    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, 02:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21