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

  • SEARCH
  • Home
  • 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 9147751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:04:16+00:00 2026-06-17T11:04:16+00:00

I am learning C at the moment, and am trying to build a simple

  • 0

I am learning C at the moment, and am trying to build a simple interpreter. It takes one character and one number. The program below only uses 'r' for the char. The 'r' stands for the range (of the natural numbers) and the digit after it specifies the length of the range.

Example Execution:

Enter:
      r 9
 0 1 2 3 4 5 6 7 8

What happens instead:

Enter:
      r 9

And here the program crashes. So I believe the error lies in the printing of the array.

The code in question is here:

#include <stdio.h>
#include <stdlib.h>
int* range(int i) {
    int *a=(int*) malloc(i * sizeof(int));
    int j;
    for(j=0;j<i;j++)
        a[j]=j;
    return a;
}
void printArray(int a[], int length) {

    int i;
    printf("\n");
    for(i=0;i<length;i++)
        printf("%d  ", a[i]);

}

int main() {
    char c;
    int n = 1;
    while(n>=0){
        printf("\nEnter:\n\t");
        scanf("%c %d", c, n);
        if(c='r')
            printArray(range(n), n);
    }
    return 0;
}

So what is causing the program to crash?

  • 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-06-17T11:04:17+00:00Added an answer on June 17, 2026 at 11:04 am

    Your arguments to scanf are wrong, you need

    scanf("%c %d",&c, &n);
    

    Your fundamental problem here is that you have no evidence about where the crash is happening, as it happens I bet it’s in scanf().

    I recommend you adopt two debugging techniques:

    a). Add print statements in your code so you know what’s happening
    b). Use an interactive debugger so you can step through and see what’s going on.

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

Sidebar

Related Questions

I'm learning to program with PyQT4 and Python. I'm trying to code a simple
At the moment I'm using JavaFX only for fun and learning it. What I
So I'm learning Scala at the moment, and I'm trying to create an abstract
I am trying to write a simple input field validation plugin at the moment
I'm using typo3 and learning how it works. I'm trying to build a table
So I'm learning C++ at the moment and I'm trying to work things out
I'm still learning Perl at the moment and have been trying to get a
I'm trying to build a really simple slider. I want to use CSS3 transitions
I'm toying with Android at the moment as a learning experience and one of
Im following a tutorial at the moment and learning how to build something into

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.