Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C] variable system in programming

  1. Aug 11, 2012 #1
    I just started programming and i dont understand how variables work. First the book defines a as 3 and b as 5 then defines a =a+b. For me who hasnt done programming ever that implies that b=0 but i think second a is different from the first one.
    Help regarding how variables work in computer
  2. jcsd
  3. Aug 11, 2012 #2


    User Avatar
    Homework Helper

    Re: variable system in programming

    In the computer language example you show, "=" means to assign a value to a variable, as opposed to meaning the variable is equal to that value. This would be a language like C, and each of those statement can be explained as:

    A = 3
    means to store the value 3 into A

    B = 5
    means to store the value 5 into A

    A = A + B
    means to add the values stored in A and B, then store the sum into A which will be 8.

    Some languages like APL use an extended character set and use left arrow for assignment:

    A ← 3
    B ← 5
    A ← A + B

    Others languages like Pascal use :=

    A := 3
    B := 5
    A := A + B
  4. Aug 11, 2012 #3
    Re: variable system in programming

    In that description your changing the value "a" to be equal to the sum of ("a" + "b") , your incrementing the value of "a" by value "b"


    a now = 8
    b still = 5

    it is the same as saying a=3+5
  5. Aug 11, 2012 #4
    Re: variable system in programming

    By the way there are two main types of variables, numeric and string. Its important not to get them mixed up.

    Strings work like this.

    a="hello "
    a now = "hello world"

    Always define your variable types before you use them otherwise


    c might end up being 53

    Variables are usually assigned at the start of a program and can be used to tell the computer how they are to be treated.

    As a string

    Dim A as string
    Dim B as string
    A now = 96

    As a number

    Dim A as integer
    Dim B as integer
    A now = 15
    Last edited: Aug 11, 2012
  6. Aug 12, 2012 #5


    User Avatar

    Staff: Mentor

    Re: variable system in programming

    Just as a general comment... it helps to say which programming language you are using.
  7. Aug 12, 2012 #6
    Re: variable system in programming

    Ok thank you. I am learning C.
  8. Aug 12, 2012 #7


    User Avatar
    Science Advisor

    Re: variable system in programming

    C doesn't have operator overloading in it's standard, so typically when see an arithmetic operation, the results will be numeric and have the same interpretation as you would find in mathematics, and not the string example above (although in languages like C++ and C#, the string example is common).

    The best way to think about these is that the left hand variable is where the stuff gets stored and the right hand side (of the equals sign) is what is taken in. If a variable appears in both sides then the variable may change after the line has been executed.
  9. Aug 12, 2012 #8


    User Avatar
    Science Advisor
    Homework Helper

    Re: variable system in programming

    There are two types of programming language. The most common type (including C) could be described as "imperative", i.e. most of the source code contains commands telling the computer to do something. The "=" sign is just as shorthand for "work out the value of what is on the right hand side, and then assign it to the variable on the left hand side". As you said, this isn't what "=" means in mathematics, but there is a limited set of characters available on a standard computer keyboard.

    There are a few languages that are "declarative", where the source code is more like a "set of equations" that the computer "solves" to produce a result (or several results, if the solution isn't unique). These languages are usually less general-purpose, because the method for "solving the equations" has to be built into the implementation, so you can't just throw anything at it and hope it will figure out how to solve it. PROLOG is an example of that type of language.

    Some early versons of the BASIC programming language carried the idea of an "imperative" language to its logical conclusion, and every statement in the language stared with a "verb". Arguably it's a bit more obvious what
    LET A = A + B
    means compared with just A = A + B, but since the computer doesn't need the "LET" to figure out what the statement means, it didn't survive into any "modern" programming language that I know of.

    The inventors of COBOL language (now pretty much obsolete) chose not to use symbols at all, and went for long winded alternatives like
    ADD B TO A
    instead of a = a + b and a = b + c. That might not seem too bad, but typing words like MULTIPLY and DIVIDE in full instead of * and / wasn't much fun.
    Last edited: Aug 12, 2012
  10. Aug 12, 2012 #9


    User Avatar
    Homework Helper

    Re: variable system in programming

    For COBOL, a programmer can use just COMPUTE followed by a Fortran like arithmetic statement. COBOL is still heavily used in financial institutions, and in the case of IBM mainframes, along with some amount high level assembly language (HLASM, ALC, left over legacy code).
    Last edited: Aug 12, 2012
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook