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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:11:20+00:00 2026-06-08T06:11:20+00:00

hey this code is working. the code is about taking name input and doing

  • 0

hey this code is working. the code is about taking name input and doing some change with it.

#include <stdio.h>
#include <stdlib.h>

/*
 *  An example of how to use strtol() to read a number
 *  and validate that one was entered correctly.
 *
 */

int main(void)
{
  char buf[BUFSIZ];
  char *p;
  long int i;

  printf ("Enter a number: ");

  if (fgets(buf, sizeof(buf), stdin) != NULL)
  {
    i = strtol(buf, &p, 10);

    /*
     *  If the first character of the buffer is \n, the user
     *  pressed [Enter] with entering any text at all, which
     *  is therefore invalid.
     *
     *  The pointer p has been updated by strtol() to point to
     *  the first invalid character after the number.
     *  If this character is \0 it means we reached the end of
     *    the array successfully, so we received a good number.
     *  If this character is \n it also means we reached the
     *    end of the input successfully.  This is a symptom of
     *    using fgets() to obtain the string, but does not
     *    represent a problem.
     *  If this character is anything else, it means there was
     *    some additional characters entered after the number.
     *    In this sample program, I have deemed this situation
     *    to be invalid, however, in your program it may be
     *    valid, depending on what you're expecting from the user.
     *
     */

    if (buf[0] != '\n' && (*p == '\n' || *p == '\0'))
      printf ("Valid number of %ld entered\n", i);
    else  printf ("Invalid number entered\n");
  }

  return(0);
}

this code is also working! it takes a string and converts into an integer!

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int m;
    char name[m],rename[m],c=0,j=0;
    puts("enter your name\n");
    gets(name);
    m=strlen(name);
    if(m>20){
        while(m>20){
            puts("shorter name pls\n");
            gets(name);
            m=strlen(name);
        }
    }
    while(name[c]==' '){
        c++;}
    while(c<strlen(name)){
        if(name[c]==' '&& name[c+1]==' ')
            {c++;}
        else{
            rename[j]=name[c];
            j++;
            c++;}
    }
    rename[j]='\0';
    puts(rename);
    return 0;
}

but while typing them together in the same program, the program is crashing!

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int m;
    char name[m],rename[m],c=0,j=0;
    puts("enter your name\n");
    gets(name);
    m=strlen(name);
    if(m>20){
        while(m>20){
            puts("shorter name pls\n");
            gets(name);
            m=strlen(name);
        }
    }
    while(name[c]==' '){
        c++;}
    while(c<strlen(name)){
        if(name[c]==' '&& name[c+1]==' ')
            {c++;}
        else{
            rename[j]=name[c];
            j++;
            c++;}
    }
    rename[j]='\0';
    puts(rename);
      char buf[BUFSIZ];
  char *p;
  long int i;

  printf ("Enter a number: ");

  if (fgets(buf, sizeof(buf), stdin) != NULL)
  {
    i = strtol(buf, &p, 10);

    /*
     *  If the first character of the buffer is \n, the user
     *  pressed [Enter] with entering any text at all, which
     *  is therefore invalid.
     *
     *  The pointer p has been updated by strtol() to point to
     *  the first invalid character after the number.
     *  If this character is \0 it means we reached the end of
     *    the array successfully, so we received a good number.
     *  If this character is \n it also means we reached the
     *    end of the input successfully.  This is a symptom of
     *    using fgets() to obtain the string, but does not
     *    represent a problem.
     *  If this character is anything else, it means there was
     *    some additional characters entered after the number.
     *    In this sample program, I have deemed this situation
     *    to be invalid, however, in your program it may be
     *    valid, depending on what you're expecting from the user.
     *
     */

    if (buf[0] != '\n' && (*p == '\n' || *p == '\0'))
      printf ("Valid number of %ld entered\n", i);
    else  printf ("Invalid number entered\n");
  }
    return 0;
}

why??

  • 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-08T06:11:21+00:00Added an answer on June 8, 2026 at 6:11 am

    There are several things that don’t look good in this program, even before you merged the two.

    • line 7: you declare an array with undefined size.
    • line 18: ‘name’ subscript index, c, is a char – limited to 0..127. int would be better.
    • line 24: same for c and j.

    Then, the gets() function should be avoided because it is possible to feed to it more data than it is prepared to accept, thereby crashing your program. If m were initialized to zero, any input of any length would be liable to trigger a crash.

    Changing the first lines makes it work:

    int m = 100;
    char name[m],rename[m];
    int c=0,j=0;
    

    …but if you entered a name longer than m, you would again have a crash.

    Just setting the value of m will cause a crash if the name is longer than the smaller of m and 127. Say you have m = 200 and enter a name containing 130 spaces: then this line

    while(name[c]==’ ‘){
    c++;}

    would increase c until it is 126, then 127, then finding that name[127] is a space would increase c again. But c is a char, and c+1 is therefore not 128, but -128. When converted to memory address to index ‘name’, that -128 points somewhere in outer space, and the program goes down in flames.

    HINT: when you compile the program and are not yet an expert, keep all compiler warnings to their highest settings. When you’re an expert, you’ll be able to turn them off — and will have learned not to want to :-).

    GCC ran on your first source says:

    In function ‘main’:
    18:5: warning: array subscript has type ‘char’ [-Wchar-subscripts]
    20:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    21:9: warning: array subscript has type ‘char’ [-Wchar-subscripts]
    24:13: warning: array subscript has type ‘char’ [-Wchar-subscripts]
    24:13: warning: array subscript has type ‘char’ [-Wchar-subscripts]
    28:5: warning: array subscript has type ‘char’ [-Wchar-subscripts]
    7:5: warning: ‘m’ is used uninitialized in this function [-Wuninitialized]
    warning: the `gets' function is dangerous and should not be used.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was working on some code earlier today, when I realized, Hey! This code
Hey, So im working on this code and I need to cast a findViewById(R.id.foo)
Hey all, I'm using this code to change my sIFR (version 3) H1 and
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
Hey I have this code but it doesn't work because it is expecting a
Hey im having this code so far - (id)initWithDictionary:(NSDictionary *)aDictionary { self = [super
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
Hey friends any one please help me for this issue.in this javascript code i
Hey all, this is the code i have to check for a day thats
This is my code : DataClassesDataContext data = new DataClassesDataContext(); var hey = from

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.