c language

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sekitoleko
    New Member
    • Sep 2006
    • 21

    c language

    I have this piece of code,
    int i,j,l=0,k=l+4;
    l - = k;
    j=k+10;
    i+=2;
    What is the value of i,j,k after the code has been executed.
    would i be right to say that k=4;l=3,j=14 and i=1.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by sekitoleko
    I have this piece of code,
    int i,j,l=0,k=l+4;
    l - = k;
    j=k+10;
    i+=2;
    What is the value of i,j,k after the code has been executed.
    would i be right to say that k=4;l=3,j=14 and i=1.
    why don't you write a program to try it?

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Originally posted by sekitoleko
      I have this piece of code,
      int i,j,l=0,k=l+4;
      l - = k;
      j=k+10;
      i+=2;
      What is the value of i,j,k after the code has been executed.
      would i be right to say that k=4;l=3,j=14 and i=1.
      No k=4, l=-4, j=14, and i was never assigned.

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by Motoma
        No k=4, l=-4, j=14, and i was never assigned.
        Not quite true.
        i was never defined (or initialized) which means it could contain any amount of garbage contained in the 4 bytes of memory allocated to it. It was then assigned that garbage + 2, the result of which could be anything.

        Comment

        • Gjhoanne
          New Member
          • Jan 2007
          • 1

          #5
          Originally posted by sekitoleko
          I have this piece of code,
          int i,j,l=0,k=l+4;
          l - = k;
          j=k+10;
          i+=2;
          What is the value of i,j,k after the code has been executed.
          would i be right to say that k=4;l=3,j=14 and i=1.
          what do u think?

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by Gjhoanne
            what do u think?
            Code:
            int i,j,l=0,k=l+4;
            i, j, l and k are defined (name and type declared and storage allocated) and l and k initialised to 0 and 4 respectively. Being local variables i and j are not initialised and their values will be whatever is in the memory locations.
            Code:
            l - = k;
            K (value 4) is subtracted from l (value 0) and the result (value -4) assigned to l
            Code:
            j=k+10;
            10 is added to k (value 4) and result (value 14) assigned to j
            Code:
            i+=2;
            2 is added to i (value ?) and result (value ?) assigned to i

            if you are not sure about the results of some expression write a program to try it, e.g.
            Code:
            #include <iostream>
            using namespace std;
            int main()
            {
            int i,j,l=0,k=l+4;
            l -= k;
            j=k+10;
            i+=2;
            cout << "i="<< i << " j="<<  j << " k="<<  k << " l=" << l << endl;
            cin.get();
            }
            Last edited by horace1; Jan 27 '07, 08:53 AM. Reason: added program

            Comment

            Working...