Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3599534
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:22:46+00:00 2026-05-18T20:22:46+00:00

I came across this piece of code today while tutoring some students in a

  • 0

I came across this piece of code today while tutoring some students in a C programming language course.
The exercise asked to implement two functions. The first one scans input from a user and the second displays what has been previously scanned.
The code I came across is the following:


#include <stdio.h>

void myInput(int i,int n)
{
  int cpt;
  int tab[n];   

  for ( cpt=0; cpt<n; cpt++)
  {
    printf("Enter a number :");
    scanf("%d",&i); 
    tab[cpt]=i;
   }
 }



void myDisp (int n)
{
  int tab[n];      
  int cpt;

  for ( cpt=0; cpt <n; cpt++)
  {
    printf("%d ", tab[cpt]); 
  } 
}

int main()
{
  int n; int i;
  printf(" Entrer the numbers of elements you want: \n");
  scanf("%d \n",&n);
  int tab[n];
  myInput(i,n);         
  myDisp(n);
}

Although this code is full of inconsistencies, it does actually work under gcc 4.4.3: it displays the numbers that have been input!!!!!!
Does anyone understands how come these code works?

Thanks very much

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:22:47+00:00Added an answer on May 18, 2026 at 8:22 pm

    If that works, it’s through sheer dumb luck. What is printed in myDisp is uninitialized stack, which may or may not contain the data that was put into similarly named variables in myInput. Related reading

    Here’s an easy way to break it with do-nothing code:

    void myInput(int i,int n)
    {
      // Add some variables to mess up the stack positioning.
      int breaker;
      int cpt;
      int stomper;
      int tab[n];
      int smasher;
    
      for ( cpt=0; cpt<n; cpt++)
      {
        printf("Enter a number :");
        scanf("%d",&i); 
        tab[cpt]=i;
       }
    
      // Trick the compiler into thinking these variables do something.
      breaker = 1;
      smasher = 3 * breaker;
      stomper = smasher + breaker;
      breaker = stomper * smasher;
     }
    

    Another way to break it would be to put a function call (say, to printf) between the calls to myInput and myDisp.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this piece of code today while tutoring some students in a
Today I came across this piece of code internal object UpdatePracownik(object employee) { lock
I came across this piece of code today: File.SetAttributes(excelFileName, File.GetAttributes(excelFileName) & ~ (FileAttributes.Archive |
I came across this piece of code today: public static byte[] ReadContentFromFile(String filePath) {
I was looking at the YUI Compressor and came across this piece of code
I came across this piece of batch code. It should find the path to
I came across this problem when referring a piece of JavaScript code: <a href="javascript:void(0);"><!--
I came across a piece of code that is: for(i=((90.0E1,0x5A)<=(0x158,140.70E1)?(.28,3.45E2,0):(95.30E1,26.40E1)<=1.400E2?(1,this):(108.,0x227));i<length;i++) { // some other
I was going over a piece of code, and i came across this regular
I came across this piece of code where programmer determines uploaded file size like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.