difference between variables and constants.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cyber dorkz
    New Member
    • Aug 2007
    • 25

    difference between variables and constants.

    i have readed the guid.l and now i'm ready for some action.

    what is a constant? i've readed in a manual what it is but i dont understand.
    is that a number or something?
    what is the difference between variables and constants?


    thank you.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Cyber dorkz
    i have readed the guid.l and now i'm ready for some action.

    what is a constant? i've readed in a manual what it is but i dont understand.
    is that a number or something?
    what is the difference between variables and constants?


    thank you.
    Oh please. What do those very names suggest? A constant is a value that
    can not be changed. A variable can change values.

    kind regards,

    Jos

    Comment

    • Cyber dorkz
      New Member
      • Aug 2007
      • 25

      #3
      can you please give somthing for example; a constant, cause i see that a constant is a number and it beggins with 0X (16 numbers) , 0(8 numbers), and ends with F, L, U.

      actually , a constant is very difficul to understand cause of all that ^^^^^^




      thanks for answering, i repete again: i've readed the guidl.

      Comment

      • Cyber dorkz
        New Member
        • Aug 2007
        • 25

        #4
        oops, i forgotn something to say, you said to me that a variable can change, but a constant not, what do you mean with changing?; please give a program for example that we can change and we cannot change.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by Cyber dorkz
          oops, i forgotn something to say, you said to me that a variable can change, but a constant not, what do you mean with changing?; please give a program for example that we can change and we cannot change.
          These are really basic questions - things that aren't really in the scope of this site. Read a tutorial, take a class. If you're having issues with these types of things, we're not going to be able to do much to help you.



          Go through that. Play with ALL the examples. Look up the functions. Play with the examples they give. Then come back and ask questions.

          Comment

          • Cyber dorkz
            New Member
            • Aug 2007
            • 25

            #6
            thanks , but to be honest , i do know how templa.te classes works.

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by Cyber dorkz
              thanks , but to be honest , i do know how templa.te classes works.
              You know how template classes work, but you don't know the difference between a var and a const?

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by Cyber dorkz
                can you please give somthing for example; a constant, cause i see that a constant is a number and it beggins with 0X (16 numbers) , 0(8 numbers), and ends with F, L, U.

                actually , a constant is very difficul to understand cause of all that ^^^^^^
                You are confusing constants and literals (sorry I may have added to this confusion with an earlier post). A constant is actually a type modifier on a variable that indicates the value of the variable doesn't change.

                A literal is were you type a value directly into your code for instance 5 is an integer literal.

                [code=cpp]
                int main(int argc, char **argp)
                {
                int i1; // This is an integer variable
                101; // This is an integer literal

                const int i2 = 100; // This is an integer constant initialised by an integer literal

                i1 = 200; // This is OK

                i2 = 300; // This is NOT OK and causes a compiler error because i2
                // is declared constant and there for can not be changed

                return 0;
                }
                [/code]

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by sicarie
                  You know how template classes work, but you don't know the difference between a var and a const?
                  and that's the beauty of C++ you can learn the complex stuff while completely by-passing the basics :D

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by Banfa
                    and that's the beauty of C++ you can learn the complex stuff while completely by-passing the basics :D
                    Isn't that how most of the programming community operates nowadays? Build
                    on top of things you don't understand but just deliver. What a great scientific exercise.

                    kind regards,

                    Jos ;-)

                    Comment

                    • Cyber dorkz
                      New Member
                      • Aug 2007
                      • 25

                      #11
                      i'll give a simple example of variable that can change and a constant that cannot change; ( must be good, if it isn't say it to me)

                      [code=c]
                      #include < iostream >

                      int main()
                      {
                      int number = 5 ;
                      cin.get() ;

                      number = number + 1 ; // you can do also this number++
                      cin.get() ;
                      }
                      [/code]

                      that was a variable , it can change!


                      here is a constant :

                      WAIT : constant can't change!

                      is everything good what i just said????

                      Comment

                      • Cyber dorkz
                        New Member
                        • Aug 2007
                        • 25

                        #12
                        please answer!


                        i need to be professional, cause i am , haha
                        but just please answer.


                        thanks


                        kind regards ,


                        dorkz

                        Comment

                        • Cyber dorkz
                          New Member
                          • Aug 2007
                          • 25

                          #13
                          hello?
                          can someone whos very nice answer?

                          Comment

                          • TRScheel
                            Recognized Expert Contributor
                            • Apr 2007
                            • 638

                            #14
                            Originally posted by Cyber dorkz
                            hello?
                            can someone whos very nice answer?
                            Amigo, they have answered your question, even going above and beyond to explain literals. I don't know what more you want.

                            Constant = Cannot change / Bad practice to even attempt to
                            Variable = Changes at will
                            Literal = A value (10, 543, 4934030, 0x24, 'c', etc)

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              Originally posted by JosAH
                              Isn't that how most of the programming community operates nowadays? Build
                              on top of things you don't understand but just deliver. What a great scientific exercise.
                              Unfortunately you are correct, I am constantly surprised at how few C/C++ programmers know how a makefile works despite using them daily to build there projects.

                              Comment

                              Working...