Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Output of Simple C Program ?

Output of Simple C Program ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andy Rama
    wrote on last edited by
    #1

    Hi all, #include void fun(void) { int a[2]; a[3] +=7; } void main() { int i = 5; printf("\t %d \n\t",i); fun(); i = 10; printf(" %d \n\t",i); } Output of above program is 5 5 Why output is like this? And it is giving Run-Time error when I use " a[2] +=7; " in the function. Why like this? Thanks in advance. Aniket.

    N H L 3 Replies Last reply
    0
    • A Andy Rama

      Hi all, #include void fun(void) { int a[2]; a[3] +=7; } void main() { int i = 5; printf("\t %d \n\t",i); fun(); i = 10; printf(" %d \n\t",i); } Output of above program is 5 5 Why output is like this? And it is giving Run-Time error when I use " a[2] +=7; " in the function. Why like this? Thanks in advance. Aniket.

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      Aniket Salunkhe wrote:

      int a[2];

      You are declaring an array of size 2 (int a[2]). So it will have a zero based index i.e. 0 to 1. This means you cannot access a[2] as that does not belong to you. You only have right to modify positions from a[0] to a[1](inclusive). Modify anything beyond this range could lead to runtime errors and other crashes. Of course you can make the array bigger. :)


      Nibu thomas Software Developer Faqs by Michael dunn

      A 1 Reply Last reply
      0
      • N Nibu babu thomas

        Aniket Salunkhe wrote:

        int a[2];

        You are declaring an array of size 2 (int a[2]). So it will have a zero based index i.e. 0 to 1. This means you cannot access a[2] as that does not belong to you. You only have right to modify positions from a[0] to a[1](inclusive). Modify anything beyond this range could lead to runtime errors and other crashes. Of course you can make the array bigger. :)


        Nibu thomas Software Developer Faqs by Michael dunn

        A Offline
        A Offline
        Andy Rama
        wrote on last edited by
        #3

        Hi, I know about array size. But when I debug the code, it is skipping the next statement followed by the function call & not giving error. So I am asking that why this is happening? Why it is not showing error? Thanks for reply. Aniket.

        1 Reply Last reply
        0
        • A Andy Rama

          Hi all, #include void fun(void) { int a[2]; a[3] +=7; } void main() { int i = 5; printf("\t %d \n\t",i); fun(); i = 10; printf(" %d \n\t",i); } Output of above program is 5 5 Why output is like this? And it is giving Run-Time error when I use " a[2] +=7; " in the function. Why like this? Thanks in advance. Aniket.

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          what is it a[0]...//used without being defined int a[3]; a[0]=a[1]=a[2]=0; a[2] +=7; and then output is 5 and 10

          A 1 Reply Last reply
          0
          • A Andy Rama

            Hi all, #include void fun(void) { int a[2]; a[3] +=7; } void main() { int i = 5; printf("\t %d \n\t",i); fun(); i = 10; printf(" %d \n\t",i); } Output of above program is 5 5 Why output is like this? And it is giving Run-Time error when I use " a[2] +=7; " in the function. Why like this? Thanks in advance. Aniket.

            L Offline
            L Offline
            ludao518
            wrote on last edited by
            #5

            int a[2]; a[3] +=7; //a[3] is return address(i = 10) //a[3]+=7 return address is (printf(" %d \n\t",i)); i = 10 do not working sorry hello

            1 Reply Last reply
            0
            • H Hamid Taebi

              what is it a[0]...//used without being defined int a[3]; a[0]=a[1]=a[2]=0; a[2] +=7; and then output is 5 and 10

              A Offline
              A Offline
              Andy Rama
              wrote on last edited by
              #6

              Hi, If I write same Program as, #include void main() { int i = 5; printf("\t %d \n\t",i); int a[2]; a[3] +=7; i = 10; printf(" %d \n\t",i); } Than Output is, 5 10 Why? So what is problem in that Program? Thanks, Aniket.

              P 1 Reply Last reply
              0
              • A Andy Rama

                Hi, If I write same Program as, #include void main() { int i = 5; printf("\t %d \n\t",i); int a[2]; a[3] +=7; i = 10; printf(" %d \n\t",i); } Than Output is, 5 10 Why? So what is problem in that Program? Thanks, Aniket.

                P Offline
                P Offline
                Phil C
                wrote on last edited by
                #7

                You are breaking the rules. Do not do this. You say you understand arrays and indexes...then why do you insist on using a[3]? a[3] is pointing to an area of memory that is being used by another part of the program. It might point to the actual memory being used by int i, it might be pointing to something else. It all depends on how the compiler allocates memory on the stack and in what order you have declared your variables and it's going to change everytime you edit your program. You are not supposed to be using a[3] so don't use it! if you create your array: int a[2]; then you can go use a[0] and a[1], and that's it. Period, exclamation point.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups