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 6933351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:49:18+00:00 2026-05-27T11:49:18+00:00

I don’t know how to work with scanf and get the input of it

  • 0

I don’t know how to work with scanf and get the input of it for the entry of the function readBigNum I want to make array until the user entered the Enter and also I want to write a function for assigning it into an array and return the size of the large number
I want readBigNum to exactly have the char *n but I can not relate it in my function

#include <stdio.h>

int readBigNum(char *n)
{
    char msg[100],ch;
    int i=0;
    while((ch=getchar())!='\n')
    {
        if(ch!='0'||ch!='1'||ch!='2'||ch!='3'||ch!='4'||ch!='5'||ch!='6'||ch!='7'||ch!='8'||ch!='9')
            return -1;
        msg[i++]=ch;
    }
    msg[i]='\0';
    i=0;
    return i;
}

int main()       
{
    const char x;
    const char n;
    n=scanf("%d",x);
    int h=readBigNum(&n);
    printf(h);
}
  • 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-27T11:49:19+00:00Added an answer on May 27, 2026 at 11:49 am

    If I understand your question correctly, you want to implement a function that will read numbers from stdin storing them in a buffer. If a non-number is encountered, you want to return -1. If a new-line is encountered, you want to return the number of characters that were read. If that’s correct, you’ll probably want your code to look something like the following:

    #include <stdio.h>
    
    int readBigNum(char* n)
    {
        char ch;
        int i=0;
        while ((ch = getchar()) != '\n') {
        if (ch < '0' || ch > '9') {
            return -1;
        }
            n[i++] = ch;
        }
        n[i] = '\0';
        return i;
    }
    
    int main(void) {
        char buf[100];
        int bytes = readBigNum(buf);
        printf("%s\n", buf);
        printf("%d\n", bytes);
    };
    

    The main differences from your implementation

    • The array to be populated is initialized in main and passed to the readBigNum function. This is a little simpler than having the function control the memory, in which case you would need likely need to deal with malloc and free. Even with this, you run the risk of a buffer overrun and will likely want to take additional precautions to prevent that.
    • The function does not set i to 0 before returning it. The original code could never return a value other than -1 (on error) or 0, which didn’t appear to be the intent.
    • This code doesn’t use scanf. Given your description of what you wanted to accomplish, using scanf didn’t appear to be a good fit, however if you provide more information on why you were calling it might help to inform this answer.
    • The printf call was incorrect, it has been updated to print the number of bytes returned, and an additional printf call was added to print the updated buffer.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know much about running a function on every item in an array, still
don't know better title for this, but here's my code. I have class user
Don't know if I worded the question right, but basically what I want to
Don't know how to explain it better but i'm trying to get a response
Don't know a whole lot about streams. Why does the first version work using
Don't get me wrong, I want them to get saved. But I always thought
I don't have much PHP experience and I want to know how to best
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query

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.