'\b' '\n' '\r' etc - good code?

This is a discussion on '\b' '\n' '\r' etc - good code? within the C++ Programming forums, part of the General Programming Boards category; Okay so i dont know many of those but few: '\b' backs the cursor one character '\n' end line '\r' ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    71

    '\b' '\n' '\r' etc - good code?

    Okay so i dont know many of those but few:
    '\b' backs the cursor one character
    '\n' end line
    '\r' goes at the begining of line

    Those were just to give you a remember for what im talking about, anyway i was wondering are those commands a good or pro code to use? Or they are stuff similliar to system("pause") that do the work but are not suggested to use?

    I was doing a function that took me like 4 days when i give up on browsing google how to delete a line and came up on those. Saves me few lines of code and time but i wondered, are the high level programmers use such code in their programs?

    Tnx.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    As standard IO is line buffered, most of them (other than newline) won't work in portable code.
    (unless you pass them in the same call.. which defeats the purpose, imho)
    But if you're using functions native to your OS, they are perfectly fine to use, AFAIK.
    Last edited by manasij7479; 05-17-2012 at 06:34 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    71
    Some more thoughts please?
    Thanks for reply manasij7479

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,701
    It doesn't really make sense to avoid using escaped characters. They are a part of programming language's syntax. You may as well be asking if it is OK to ignore the alphabet. Escaped characters do not have to mean anything.

    I never really thought of this as a portability issue. There is a very boring discussion about when something is an escaped character or a control character on the internet. A control character is a character that does something on its own, such as tab, for display purposes. What an escaped character sequence represents is the domain of compiler vendors, and if you care you can find some sequences that are different. Where they are different is usually irrelevant because there are other ways to represent a control character, like '\x1b' for escape, when you want to use it. The values for control characters that you already know are actually defined by ASCII and Unicode and other character sets, as well as the ones that you don't necessarily know but could use.

    I said before that I don't think this is a portability issue. The way I see the world you don't really have a choice when it comes to what character set you should use in a program. You have to use the appropriate set and you have to memorize it well enough. Think different if you like, but my standard for portable also doesn't include such statements as, "There is a use for the escape key that applies everywhere." I can program that and you can program that and there is no "right" practice.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    71
    whiteflags thanks for the reply.
    I was just wondering couse, system("pause") is also part of the programing language but its not recomended, thought those escape character might be to same or diferent but still not recomended.

    If you ever encounter a issue that you are forced to use for example '\b', in a very large project for like one line of code, are you going to use it, or you will code your own function?

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by CoffeCat View Post

    I was just wondering couse, system("pause") is also part of the programing language but its not recomended, thought those escape character might be to same or diferent but still not recomended.

    system("pause") is not exactly a part of the language..as it just sends the "pause" command to the system.
    Depending on the system, it may or may not work
    If you ever encounter a issue that you are forced to use for example '\b', in a very large project for like one line of code, are you going to use it, or you will code your own function?
    Can you cite such an issue?
    I can't think of one in which '\b' is urgently needed.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,701
    Quote Originally Posted by CoffeCat View Post
    If you ever encounter a issue that you are forced to use for example '\b', in a very large project for like one line of code, are you going to use it, or you will code your own function?
    Those ideas are not mutually exclusive. I'll even give you an example issue. If I cared about portability I would make a function that somehow uses '\b'. That function is also a portable interface: It's not like it makes an amazing difference what would replace '\b' in the function.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    71
    I see, thanks for the reply's, i just wanted to make sure i use good stuff, wanna make some good and maybe pro habits xD
    Will use them for now

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,294
    Quote Originally Posted by CoffeCat View Post
    I was just wondering couse, system("pause") is also part of the programing language but its not recomended, thought those escape character might be to same or diferent but still not recomended.
    Firstly, a quibble. The system() function is not formally part of the programming language. It is part of the standard library. The library is something distinct from the language. Despite the fact they are described in the same document (the C standard, whichever version you are using) the library and the language are different things.

    It is possible to write conforming C code that does not use the standard library. It is not possible to write conforming C code that does not make use of the language (grammar, syntax, etc).

    That quibble aside, the effect of the system() function is implementation defined. It's effect, if passed a NULL pointer, is to determine if the host system has a command processor. If passed a non-NULL pointer, the pointer is assumed to be a string, and that string is passed to the command processor to be executed in some manner. The manner in which it does this is required to be documented, but the effect is not specified by the C standard. The standard does permit one possible effect to be termination of the calling program, and another possible effect is for the calling program to behave in some non-conforming manner (i.e. some behaviour that goes outside constraints described by the C standard, some of which the programmer might not expect either).

    The C standard certainly does not impose any limits on what the effects of system("pause") are. The command processor on some systems (notably MS-DOS and windows) does cause a message to be written, and then a wait for a key to be hit, before returning control to the program that called system(). On other systems, however, system("pause") might have no effect, might cause an error condition, or many other effects ..... not all "command processors" support a pause command.

    THAT is why usage of system("pause") is not recommended.

    This is a very different situation from usage of '\n', '\b' and similar escape codes. Their effects are well defined by the standard. What is not well defined is the effects those characters have when passed (whether as individual characters, or as characters within strings) to library functions or output devices other than those specified in the C standard (eg standard files, standard streams like stdout, stderr, etc). So, if you are using a third party library, it is up to you to understand what those characters do. Just as it is up to you to understand (eg by reading available documentation) what ANY arguments supplied to third-party library functions will do.

    Quote Originally Posted by CoffeCat View Post
    If you ever encounter a issue that you are forced to use for example '\b', in a very large project for like one line of code, are you going to use it, or you will code your own function?
    I've never encountered an issue that forced me to use '\b'. I start with some requirement on what effect the code is supposed to achieve, and work out how to achieve it.

    If '\b' provides a means of implementing that effect (eg because I am writing to stdout, for which the standard describes what '\b' does, or because I am using a third-party library for which documentation describes using '\b' as giving an effect I seek) then I use it. If some other option gives the effect I seek, I might use that.

    If '\b' is just one of several options for achieving an effect I seek, I will probably pick the simplest one (the one that works, without having to think to hard) or do some assessment of which is "best" for the task at hand, and use that.

    There is a point where software development has to be a pragmatic activity. The questions you are asking are not pragmatic.
    Last edited by grumpy; 05-19-2012 at 02:11 AM. Reason: Fixed typos
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    71
    Well explained sir, thank you. I just readed that those systems arent recomended but only that so i mentioned it here as an example, you did answer my question. Thanks to all, it was just for a simple programm but as i said i wanna use some good habits for now and specialy later on.

    On a side node,
    I really dont feel like opening a brand new thread for that question, so i will ask here really shortly.

    I was wondering how does the online games for example, saves their values after the program quits? I tried few things but each definition of a variable is reseting any values.
    If the answer is short, plase answer, if not give me some topic or something to browse?

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,701
    I was wondering how does the online games for example, saves their values after the program quits?
    The answer is a file of some sort, that is loaded after you start the game or load a save. Flash can use flash cookies, or you can use browser cookies if your doing something simpler than that. PC games that use the internet (e.g. WoW, LoL, Dota2) probably store your character data in a database, which is on a server, and query it as part of the load process. ...Very different technologies...

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    71
    A file ofcourse..i will save mine in .txt lol.
    Thanks for reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good study source code
    By Inanna in forum C++ Programming
    Replies: 6
    Last Post: 06-01-2011, 07:24 AM
  2. is this code is good?
    By ExDHaos in forum C++ Programming
    Replies: 3
    Last Post: 05-23-2009, 09:56 AM
  3. Why it is not good code for constructor
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 07:29 AM
  4. Is this code good?
    By AsmLover in forum C Programming
    Replies: 3
    Last Post: 08-05-2003, 04:00 PM
  5. does anyone have good code????
    By K&J in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2001, 10:32 AM

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