Initialization and assignment

This is a discussion on Initialization and assignment within the C++ Programming forums, part of the General Programming Boards category; Hi, I am very much confused with the two things i.e. Initialization and assignment. Can anyone please explain me what ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    5

    Exclamation Initialization and assignment

    Hi,
    I am very much confused with the two things i.e. Initialization and assignment.
    Can anyone please explain me what exactly these two things

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Refer to Stroustrup's glossary:
    initialization - giving an object an initial value. Initialization differs from assignment in that there is no previous value involved. Initialization is done by constructors.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    5
    suppose I say

    int a;
    a=123;

    then how can we realize this is initialization or assignment

    Thanks

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by rajesh1978 View Post
    suppose I say

    int a;
    a=123;

    then how can we realize this is initialization or assignment

    Thanks
    Anything using '=' after the semicolon on the line of the declaration, is an assignment.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    5
    so if we say
    int a=123; then it should be initialization
    am I correct

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,349
    Quote Originally Posted by rajesh1978 View Post
    so if we say
    int a=123; then it should be initialization
    am I correct
    as Elysia said, it is initialization, but when you come right down to it, it's still also assignment.

    if it were

    int a(123);

    that would be initialization only. any time you have an '=' it's an assignment, even if it is also initialization.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Quote Originally Posted by Elkvis
    any time you have an '=' it's an assignment, even if it is also initialization.
    No, rajesh1978's example in post #5 is that of copy initialisation, not "initialisation and assignment", while your example is that of direct initialisation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 11:42 AM
  2. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  3. Assignment vs. Initialization
    By arrgh in forum C Programming
    Replies: 6
    Last Post: 05-06-2008, 03:08 AM
  4. class initialization and the = operator
    By krygen in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2005, 11:44 PM
  5. How does this work?...assignment using vectors...
    By Gamma in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2002, 03:58 PM

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