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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:50:04+00:00 2026-05-26T15:50:04+00:00

#include<limits.h> #include<errno.h> long output; errno = 0; output = strtol(input,NULL,10); printf(long max = %ld\n,LONG_MAX);

  • 0
#include<limits.h>
#include<errno.h>

long output;

errno = 0;
output = strtol(input,NULL,10);
printf("long max = %ld\n",LONG_MAX);
printf("input = %s\n",input);
printf("output = %ld\n",output);
printf("direct call = %ld\n",strtol(input,NULL,10));
if(errno || output >= INT_MAX || output <= INT_MIN) {
    printf("Input was out of range of int, INT_MIN = %d, INT_MAX = %d\n",INT_MIN,INT_MAX);
    printf("Please input an integer within the allowed range:\n");
}

when the code above is fed with an input array of {‘1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’0′,’1’}

i get an output of:

long max = 9223372036854775807
input = 12345678901
output = -539222987
direct call = 3755744309

what is going on… strtol appears to be suffering from overflow but not setting errno

  • 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-26T15:50:05+00:00Added an answer on May 26, 2026 at 3:50 pm

    You’re most likely not including the required <stdio.h> and/or <stdlib.h> headers.

    Your code works fine (GCC in 64bit mode) once you include those:

    $ cat t.c
    #include<limits.h>
    #include<errno.h>
    #include<stdlib.h>
    #include<stdio.h>
    
    int main (void)
    {
        long output;
        char input[] = "12345678901";
        errno = 0;
        output = strtol(input,NULL,10);
        printf("long max = %ld\n",LONG_MAX);
        printf("input = %s\n",input);
        printf("output = %ld\n",output);
        printf("direct call = %ld\n",strtol(input,NULL,10));
        if(errno || output >= INT_MAX || output <= INT_MIN) {
            printf("Input was out of range of int, INT_MIN = %d, INT_MAX = %d\n",INT_MIN,INT_MAX);
            printf("Please input an integer within the allowed range:\n");
        }
        return 0;
    }
    
    $ gcc -Wall -Wextra -pedantic t.c
    $ ./a.out
    long max = 9223372036854775807
    input = 12345678901
    output = 12345678901
    direct call = 12345678901
    Input was out of range of int, INT_MIN = -2147483648, INT_MAX = 2147483647
    Please input an integer within the allowed range:
    

    BTW, you should be saving errno right after the strtol call, the library functions you call between strtol and your conditional could alter its value.

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

Sidebar

Related Questions

#include <stdio.h> #include <limits.h> int main(void){ printf("Type Size Min Max\n----------------------------------------------------------------------\n"); printf("%-20s%-10d%-20ld%-20ld\n", "long", sizeof(long), LONG_MIN,
#include <iostream> #include <cmath> #include <limits.h> #include <stdlib.h> using namespace std ; long long
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
#include <vector> std::vector<long int> as; long int a(size_t n){ if(n==1) return 1; if(n==2) return
How does the following work? #include <limits> int main() { const int* const foo
Functions.h #include <iostream> #include <windows.h> #include <conio.h> #include <limits> #include <stdexcept> #include <stdlib.h> #include
i want to to calculate minimum sum in given two dimensional array #include<iostream> #include<limits.h>
I am trying to read an unsigned int using cin as follows: #include <limits.h>
Here's my code (created just to test fork()): #include <stdio.h> #include <ctype.h> #include <limits.h>
Consider the following programs: // http://ideone.com/4I0dT #include <limits> #include <iostream> int main() { int

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.