Some small questions

This is a discussion on Some small questions within the C Programming forums, part of the General Programming Boards category; I grouped these under one thread since they are all kinda inter-related, and I didn't wanna take up 5 sepearate ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    Some small questions

    I grouped these under one thread since they are all kinda inter-related, and I didn't wanna take up 5 sepearate threads...

    1. When you are using the escape sequence '\b' to backspace in printf, is there anyway to make it do it more than once...for example, instead of having to type \b\b\b is there an easier way to make it backspace 3 times?

    2. Is there anyway to advance the cursor forward 1 position using printf, kinda like the reverse of \b. I know you could just hit the spacebar, but I was wondering if that's the only way?

    3. Is there anyway to make the cursor dissapear from the screen when using printf? Like when it sits there at the end of whatever i've displayed on the screen blinking.

    4. I know %10d means put 10 spaces before printing the corresponding integer (in printf), but what if you want 10 blank spaces, and you aren't using an integer, is there a way? What I mean is:
    Code:
    printf("              blah blah");
    Is there any other way to do the above that doesn't involve hitting the spacebar a bunch of times?

    Thanks in advance,

    --Ash

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Also it seems like when defining a char array you can only define
    it when you create it, not later on... like
    Code:
    char name[10] = "kris";
    Works fine, but
    [code]
    char name[10];
    name = "kris";
    [\code]
    doesn't work. Am I right, that you can only define it when you create it? I know that you can change individual parts later on like kris[1] = 'r', but it seems like you can't do the whole literal string at once, unless you do it when you define it....

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Ash1981
    I grouped these under one thread since they are all kinda inter-related, and I didn't wanna take up 5 sepearate threads...

    1. When you are using the escape sequence '\b' to backspace in printf, is there anyway to make it do it more than once...for example, instead of having to type \b\b\b is there an easier way to make it backspace 3 times?
    No, unless you want to use putchar('\b'); in a loop.
    2. Is there anyway to advance the cursor forward 1 position using printf, kinda like the reverse of \b. I know you could just hit the spacebar, but I was wondering if that's the only way?
    putchar(' ');

    3. Is there anyway to make the cursor dissapear from the screen when using printf? Like when it sits there at the end of whatever i've displayed on the screen blinking.
    Not portably, it depends on your platform.

    4. I know %10d means put 10 spaces before printing the corresponding integer (in printf), but what if you want 10 blank spaces, and you aren't using an integer, is there a way? What I mean is:
    Code:
    printf("              blah blah");
    Is there any other way to do the above that doesn't involve hitting the spacebar a bunch of times?
    No, unless you include putchar(' '); with a loop, or similar.


    Quote Originally Posted by Ash1981
    Also it seems like when defining a char array you can only define
    it when you create it, not later on... like
    Code:
    char name[10] = "kris";
    Works fine, but
    [code]
    char name[10];
    name = "kris";
    [\code]
    doesn't work. Am I right, that you can only define it when you create it? I know that you can change individual parts later on like kris[1] = 'r', but it seems like you can't do the whole literal string at once, unless you do it when you define it....
    See http://c-faq.com/charstring/assign.html
    Last edited by cwr; 12-30-2005 at 04:53 AM.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    cwr - wow, the FAQ you routed me to had my *exact* question, damn, your good

    As always, thanks for the awnsers.

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Well, I've read that FAQ several times and therefore can see which questions it addresses. You should read the entire FAQ as well as the one on this site.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    > printf(" blah blah");
    Try
    Code:
      printf( "Hello%10sworld\n", "" );

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    The other thing you can do with printf is generate your format string beforehand to include however many spaces, backspaces etc. you like. You can use sprintf, strncat and so on inside loops to build a format string then output it all at once. Depending on what you're doing this might be more convenient than putchar in a loop and can also be used to create format strings to print out a string to a maximum length determined at runtime, so as to allow indefinate length strings while still keeping things safe.

    For instance, to make a format string that contains ten backspaces, you might do:
    Code:
    char fmt[11];
    int i;
    
    for(i = 0; i < 10; i++)
    {
        fmt[i] = '\b';
    }
    fmt[11] = '\0';
    
    printf(fmt);
    That's just a simple example to show you what I mean, but sprintf is very useful for more complicated format strings. You use "%%" to get a literal '%' for your later printf call, so for instance you would do sprintf(fmt, "%%d"); to make a format string that will print an integer.

    Not all entirely on topic, and cwr already answered your question, but it's another way to do things which might be useful to you.

  8. #8
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by Ash1981
    Also it seems like when defining a char array you can only define
    it when you create it, not later on... like
    Code:
    char name[10] = "kris";
    Works fine, but
    [code]
    char name[10];
    name = "kris";
    [\code]
    doesn't work. Am I right, that you can only define it when you create it? I know that you can change individual parts later on like kris[1] = 'r', but it seems like you can't do the whole literal string at once, unless you do it when you define it....

    dunno why this ain't working but I've tried postig code in th same window as quoting this and it just fuggs up. sry for the double post...

    use:
    Last edited by mako; 12-30-2005 at 07:25 AM.

  9. #9
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    char source_string_name[35], target_string_name[35];
    
    
    strcpy(source_string_name, "I had your old mobile from the USA");
    
    printf("%s\n", source_string_name); //returns:I had your old mobile from the USA
    
    
    strcpy(target_string_name, source_string_name);
    
    printf("%s\n", target_string_name); //returns:I had your old mobile from the USA
    
    
    //and last but not least, adding into a string a certain number of characters fomr another:
    
    
    strncpy(target_string_name+16, source_string_name+24, 2);
    target_string_name[18]=0;
    
    printf("%s\n\n", target_string_name); //returns:I had your old mom
    
    
    system("PAUSE");
    return 0;
    }
    €DIT: Shoot, I just realised that question's already been answered -___-
    Last edited by mako; 12-30-2005 at 07:28 AM.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    22
    Quote Originally Posted by lemnisca
    For instance, to make a format string that contains ten backspaces, you might do:
    Code:
    char fmt[11];
    int i;
    
    for(i = 0; i < 10; i++)
    {
        fmt[i] = '\b'; // #1
    }
    fmt[11] = '\0'; //  #2
    
    printf(fmt);
    I hate to be a nit, but..

    #1:

    Your loop will fill fmt[0] to fmt[9]

    You skip fmt[10] and you overflow the buffers bounds in #2 by writing to fmt[11].

    fmt[10] should be the null terminating character.

    Since most memory is usually aligned on a 4 byte boundary, you probably would not notice this bug, it probably doesn't actually overwrite anything of use / anything in the stack frame, but it's a bug none the less.

    I only pointed it out because you seem to have problems understanding the 0 indexing that C uses for it's arrays.
    Last edited by slcjoey; 12-30-2005 at 07:39 AM.

  11. #11
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by slcjoey
    I hate to be a nit, but..

    ...(snip)

    Since most memory is usually aligned on a 4 byte boundary, you probably would not notice this bug, it probably doesn't actually overwrite anything of use / anything in the stack frame, but it's a bug none the less.

    ...(snip)

    And what will tend to happen in the real world, given Murphy's Law, is that the bug will tend to lie dormant for weeks, perhaps even months. As the program is fleshed out and expanded, something will eventually get tromped on by that little overflow.

    At that point, you could be looking in some other part of the program for a problem that you won't find there.....

    Good catch!

  12. #12
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by Ash1981
    2. Is there anyway to advance the cursor forward 1 position using printf, kinda like the reverse of \b. I know you could just hit the spacebar, but I was wondering if that's the only way?
    If you're always working with an ANSI console or terminal, you can do non-destructive movement with the control sequence "<esc>[C", where '<esc>' is the C0 control Escape (0x1B). If you want to move 5 characters to the right, the sequence is "<esc>[5C". This is a standard ANSI control sequence. If you replace the 'C' at the end of the sequence with 'A', the cursor will move up, 'B' will move the cursor down, and 'D' will move the cursor to the left.
    Quote Originally Posted by Ash1981
    3. Is there anyway to make the cursor dissapear from the screen when using printf? Like when it sits there at the end of whatever i've displayed on the screen blinking.
    Probably another ANSI control sequence, but I can't remember off the top of my head what it is. You might want to do a Google search on "ANSI control sequences" and see if anything useful shows up.
    [edit]The sequence "<esc>[<selector> q", where '<esc>' is as above, '<selector>' is a number selecting the cursor rendition (0 for none, 1 for blinking block, 2 for steady block, 3 for blinking underline, 4 for steady underline), and with a space character between the selector and the 'q', works to control what the cursor looks like on some DEC terminals, but this is not supported by all ANSI displays.[/edit]
    Quote Originally Posted by Ash1981
    4. I know %10d means put 10 spaces before printing the corresponding integer (in printf), but what if you want 10 blank spaces, and you aren't using an integer, is there a way?
    "%10d" means to print an integer in a field 10 characters wide (if it fits), padding with spaces if the decimal representation of the integer doesn't fill the entire 10 characters. You can use various things for whitespace, including '\t' to move to the next tab stop position.

    In general, ANSI display terminal programming is fairly easy if you have a good reference for it. What I call "control sequences" you may see called "escape sequences" because they usually start with the escape character.

    Not all ANSI console implementations are created equal, so you should try your program on as many of them as you can find.
    Last edited by filker0; 12-30-2005 at 10:21 AM. Reason: Add sequence for setting cursor style
    Insert obnoxious but pithy remark here

  13. #13
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Quote Originally Posted by slcjoey
    I hate to be a nit, but..

    #1:

    Your loop will fill fmt[0] to fmt[9]

    You skip fmt[10] and you overflow the buffers bounds in #2 by writing to fmt[11].

    fmt[10] should be the null terminating character.

    Since most memory is usually aligned on a 4 byte boundary, you probably would not notice this bug, it probably doesn't actually overwrite anything of use / anything in the stack frame, but it's a bug none the less.

    I only pointed it out because you seem to have problems understanding the 0 indexing that C uses for it's arrays.
    oh, indeed...that's just me not checking the code I post carefully enough. It should, of course, be fmt[10] = '\0';. This is why I always turn on plenty of warnings when I compile...

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Thanks for all your responses, it's a great help.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by lemnisca
    oh, indeed...that's just me not checking the code I post carefully enough. It should, of course, be fmt[10] = '\0';. This is why I always turn on plenty of warnings when I compile...
    Your warnings won't catch this sort of problem however, because C doesn't prevent you from running off the end of your arrays.


    Quzah.
    Last edited by quzah; 12-31-2005 at 06:41 AM.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  3. Some questions about Status Bars/Menus...
    By AoA in forum Windows Programming
    Replies: 8
    Last Post: 07-31-2006, 12:36 AM
  4. questions about new and delete
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2001, 02:48 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 08:22 AM

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