C++

Discussion in 'Scripting & Programming' started by Mof, Jul 10, 2008.

  1. Mof

    Mof Megabyte Poster

    526
    2
    49
    Hi again decided to go for the C++ got a book called "C++ in easy steps" being an absolute begginer it seems to cater for me, My question is when writing a program does it matter about spacing as in these examples

    example1
    cout <<"char letter:" << letter << endl;

    example 2
    cout<<"char letter:"<<letter<<endl;

    its just something thats been niggling me
    Cheers Martin
     
    WIP: C++ and A+
  2. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    In general it is better to use spaces.

    In many cases the compiler can work out how to parse a line, but there are also many cases where it might get confused, or where a space is mandated.

    Also it is clearer to anybody reading the code if there are spaces!

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  3. Mof

    Mof Megabyte Poster

    526
    2
    49
    Cheers Harry
     
    WIP: C++ and A+
  4. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Whitespace is sometimes used as a delimiter in C/C++, the delimiters define to the lexer what the tokens are, the tokens define what the parser does and whitespace is not generally involved in this. Delimiters can be whitespace, seperators and operators. Delimiters help the lexer determine the tokens.

    See here :-

    http://msdn.microsoft.com/en-us/library/3yx2xe3h(VS.80).aspx
    http://www.osdata.com/topic/language/cplus.htm

    Consider :-
    int a = 5;

    Without spaces becomes :-
    inta=5;

    Despite int being a reserved word the lexer cannot determine this and sees it as one token 'inta' which it does not recognise.

    As harry said readability is considered good form so try to copy a good coding style.

    Delimiters in your example :-
    <<
    ;

    Tokens in your example :-
    cout
    "char letter:"
    letter
    endl


    In this case :-

    cout << "char letter:" << letter << endl;

    or this :-

    cout<<"char letter:"<<letter<<endl;

    Are equally fine, the lexer can parse/lex the operator '<<' into a delimiter/token just like a '+' in 1+2, the operators help delimit the expression. The expression is the entire line in this case and also includes sub expressions.

    See here :-

    http://aspire.cs.uah.edu/textbook/CPP7001.html

    For this reason '<' is an invalid character for inclusion into a variable name.

    Maybe look at this for more info on parsing :-

    http://en.wikipedia.org/wiki/Parser
    http://en.wikipedia.org/wiki/Recursive_descent_parser
    http://en.wikipedia.org/wiki/Lexical_analysis
     
  5. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    C++ is a 'free form language', in that spaces have no significant meaning. So for the examples that you have provided they are semantically equivalent even though the syntax appears to differ. What are 'semantics'? Well, doing a quick google search for a clean explanation:

    In a nutshell, this means that the structure of the language (the 'syntax') is fashioned in a way such that when your source file is parsed (the file being read with syntax errors being picked out and notified to you as an error), it passes onto the next phase called 'Lexical Analysis'. This is the phase where tokens such as reserved words (strings like 'cout' and 'endl' that cannot be used as variable names) are picked out, as well as your variable names. Also in your example you will note that you have specified a literal string ("char letter:") which will be parsed in a rather unusual way. When the first double-quote is encountered this will itself be parsed as a token specifying the beginning of a string literal, then the 'c' and the 'h' and the 'a' and the 'r', etc. (before others jump in and say that it is pared as a string object - not quite yet, since the object type and size needs to be determined prior to being classified as an object!). Now, once the final double-quote is encountered the parser will be aware syntax is correct, so no errors will be raised, for which the lexical analyser will interpret the string as a token and give it a temp name (as if you assigned the string to a variable name yourself) for further processing.

    That is it in a nutshell. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  6. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    C++ is mostly free form meaning its not like ALGOL or COBOL where you must put code in a specific column etc. In general extra spaces and tabs will have no ill effects.

    However some spaces and <CR><NL> combinations are necessary as well as the more standard delimiters like ';'.

    http://www.nfc-development.org/2007/01/concatenating-literal-c-strings.html

    http://cs.fit.edu/~mmahoney/cse2050/how2cpp.html

     
  7. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Yes, but there is an important distinction here. Where the spaces are significant is where there is 'code' that does not affect the logic of your program. For instance:

    Code:
    #ifndef _SOME_LABEL_
    #define _SOME_LABEL_
    
    // Code in here.
    
    #endif
    
    Will specify the code defined after _SOME_LABEL_ to be part of the translation unit (the code included in the final executable). The logic itself isn't affected, just the code that is included. Likewise for both the C++ single-line comments and the C multi-line comments. Where the non printable characters are significant (<CR>, <NL>), the logic of your code isn't affected.
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  8. Mof

    Mof Megabyte Poster

    526
    2
    49
    Thanks for the links guys will have a look at them, I am a beginner but already im enjoying the learning curve,
    this is what it was teaching me.


    #include <iostream>
    using namespace std;

    int main()


    {
    char letter; letter= 'A'; // declared then initalized
    int number; number=100; // declared then initalized
    float decimal=7.5; // declared and initalized
    double pi=3.14159; // declared and initalized
    bool isTrue = false; // declared and initalized
    cout << "char letter:" << letter << endl;
    cout << "int number:" << number << endl;
    cout << "float decimal:" << decimal <<endl;
    cout << "double pi:" << pi << endl;
    cout << "bool isTrue:" << isTrue << endl;
    return 0;
    }
     
    WIP: C++ and A+
  9. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    I've spotted two errors because you haven't compiled this! :p
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  10. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Compiles fine for me on VS 2003.

    Yes C++ still has the C macro preprocessor used for header guards and other things as you point out. It is mostly this that is picky about newlines, however whitespace is still a critical delimiter for main C++ parser as well.

    C++ Preprocessor :-
    http://msdn.microsoft.com/en-us/library/y4skk93w(VS.80).aspx

    The preprocessor is used to perform basic optional inclusion of code blocks, header includes and basic macros. It is run on the source before it gets to the main parser. The preprocessor also processes the #includes and generates one big pre-processed source file for the C++ parser/compiler. Multiple source files get turned into multiple object files or linkage units and the linker then deals with joining them together to make a program executable.

    Header guards can optionally be replaced by pragmas in some modern compilers, they exist for compatibility and to ensure maximum portability between compilers as pragmas are not part of the C++ standard. Most people advise the use of both together if your current compiler supports the pragma as it give slightly better compile performance, and if you port your code with still continue to work falling back to the preprocessor header guards.

    http://en.wikipedia.org/wiki/Pragma_once
     
  11. Mof

    Mof Megabyte Poster

    526
    2
    49
    "I've spotted two errors because you haven't compiled this! "


    Compiles fine for me to, I was using Mini GW as the book recomends but yesterday found a copy of VS 2000 well i think its 2000 and this seems to have taken over from Mini GW was wondering should I uninstall VS until im a bit more experienced.
     
    WIP: C++ and A+
  12. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Upto you really, best to understand the C++ Standard and the fundamentals, GNU, GCC and make and the like are good for that.

    A lot of people use VS, its possibly not the best compiler in the world but its good for learning a modern IDE, windows programming and libraries.
     
  13. Mof

    Mof Megabyte Poster

    526
    2
    49
    I suppose the only differance is in Mini GW you have to go to the command promt to compile the program,
    VS seems to do it automatically.
     
    WIP: C++ and A+
  14. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    My bad. Compiled it under VS2005, and it appears that standard have changed over the last few years to somewhat support inferred types. Very bad programming style to be taught. :blink
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  15. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    I started learning C using GCC on Unix. It's a good way to start, but using the Vi editor was a nightmare with its unintuitive keystrokes to get to certain functionality. GCC when the compiler options are set to 'strict' enforces proper programming style. At work, for instance, we code using Visual Studio then recompile using GCC with these very strict settings before code is submitted; you'd be surprised the silly mistakes one makes that can break code in the future.

    Anyway, very strict coding standards shouldn't really concern you at the moment as you are just beginning. The Visual Studio IDE is desgned to be easy to use both for novices and experts alike. I would base your learning around and using the Beginner Developer Learning Center stuff and downloading Visual C++ Express 2008. This way you will be learning on a platform that you're most likely to be working on in the future, and your learning will be better guided. :)

    I wish I had these resource around when I was a novice!
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  16. Mof

    Mof Megabyte Poster

    526
    2
    49
    Thanks Dmarsh26 and Mathmatix finally got to have a look at the links you both posted sorry its taken so long we are fostering a two month old baby at the moment and he needs a lot of our time, dont all baby's
    anyway they look very interesting will go down that root after completing the book im reading at the moment,hopefully no more than a month, dont want to spread myself to thin, but look forward to more of your help im my quest to be A programmer.:eek:

    Martin
     
    WIP: C++ and A+
  17. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    C++ or baby...? :hhhmmm

    Baby!! :cheers

    Good luck with the code. We're always here mate. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  18. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    No problem ! :D

    Best of luck with the baby and the programming !
     
  19. Mof

    Mof Megabyte Poster

    526
    2
    49
    Just A Quickie, does the code in this example have to be on 2 lines or can it all be on the same line

    float numbers[3];
    numbers[0] = 1.5; numbers[1] = 2.5;numbers[2] = 3.25;


    float numbers[3]; numbers[0] =1.5; numbers[1] = 2.25; numbers [2] = 3.25;

    book shows it as example 1 was wondering if this could be just to save space in the book.
     
    WIP: C++ and A+
  20. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Doesn't matter. They could all be on the same line. However the book version is clearer!

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.