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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:35:39+00:00 2026-05-10T17:35:39+00:00

I have a string(char*), and i need to find its underlying datatype such as

  • 0

I have a string(char*), and i need to find its underlying datatype such as int, float, double, short, long, or just a character array containing alphabets with or with out digits(like varchar in SQL). For ex:

    char* str1 = '12312'     char* str2 = '231.342'     char* str3 = '234234243234'     char* str4 = '4323434.2432342'     char* str5 = 'i contain only alphabets' 

Given these strings, i need to find that the first string is of type int and typecast it to an int, and so on ex:

int no1 = atoi(str1) float no2 = atof(str2) long no3 = atol(str3) double no4 = strtod(str4) char* varchar1 = strdup(str5) 

Clarifying a bit more…

I have a string and its contents could be alphabets and/or digits and/or special characters. Right now, I am able to parse string and

  1. Identify if it contains only digits,
    Here i convert the string into short or int or long, based on best fit. ( How do i know if the string can be converted to an short int or long?)
  2. Only alphabets, leave it as a string.
  3. Digits with a single decimal point.
    Here i need to convert the string into float or double ( Same question here)
  4. other. leave it as a string
  • 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. 2026-05-10T17:35:40+00:00Added an answer on May 10, 2026 at 5:35 pm

    In C (not in C++), I would use a combination of strtod/strol and max values from <limits.h> and <float.h>:

    #include <stdlib.h> #include <stdio.h> #include <limits.h> #include <float.h>  /*    Now, we know the following values:       INT_MAX, INT_MIN, SHRT_MAX, SHRT_MIN, CHAR_MAX, CHAR_MIN, etc.    */  typedef union tagMyUnion {    char TChar_ ; short TShort_ ; long TLong_ ; double TDouble_ ; } MyUnion ;  typedef enum tagMyEnum {    TChar, TShort, TLong, TDouble, TNaN } MyEnum ;  void whatIsTheValue(const char * string_, MyEnum * enum_, MyUnion * union_) {    char * endptr ;    long lValue ;    double dValue ;     *enum_ = TNaN ;     /* integer value */    lValue = strtol(string_, &endptr, 10) ;     if(*endptr == 0) /* It is an integer value ! */    {       if((lValue >= CHAR_MIN) && (lValue <= CHAR_MAX)) /* is it a char ? */       {          *enum_ = TChar ;          union_->TChar_ = (char) lValue ;       }       else if((lValue >= SHRT_MIN) && (lValue <= SHRT_MAX)) /* is it a short ? */       {          *enum_ = TShort ;          union_->TShort_ = (short) lValue ;       }       else if((lValue >= LONG_MIN) && (lValue <= LONG_MAX)) /* is it a long ? */       {          *enum_ = TLong ;          union_->TLong_ = (long) lValue ;       }        return ;    }     /* real value */    dValue = strtod(string_, &endptr) ;     if(*endptr == 0) /* It is an real value ! */    {       if((dValue >= -DBL_MAX) && (dValue <= DBL_MAX)) /* is it a double ? */       {          *enum_ = TDouble ;          union_->TDouble_ = (double) dValue ;       }        return ;    }     return ; }  void studyValue(const char * string_) {    MyEnum enum_ ;    MyUnion union_ ;     whatIsTheValue(string_, &enum_, &union_) ;     switch(enum_)    {       case TChar    : printf('It is a char : %li\n', (long) union_.TChar_) ; break ;       case TShort   : printf('It is a short : %li\n', (long) union_.TShort_) ; break ;       case TLong    : printf('It is a long : %li\n', (long) union_.TLong_) ; break ;       case TDouble  : printf('It is a double : %f\n', (double) union_.TDouble_) ; break ;       case TNaN     : printf('It is a not a number : %s\n', string_) ; break ;       default       : printf('I really don't know : %s\n', string_) ; break ;    } }  int main(int argc, char **argv) {    studyValue('25') ;    studyValue('-25') ;    studyValue('30000') ;    studyValue('-30000') ;    studyValue('300000') ;    studyValue('-300000') ;    studyValue('25.5') ;    studyValue('-25.5') ;    studyValue('25555555.55555555') ;    studyValue('-25555555.55555555') ;    studyValue('Hello World') ;    studyValue('555-55-55') ;     return 0; } 

    Which results in the following:

    [25] is a char : 25 [-25] is a char : -25 [30000] is a short : 30000 [-30000] is a short : -30000 [300000] is a long : 300000 [-300000] is a long : -300000 [25.5] is a double : 25.500000 [-25.5] is a double : -25.500000 [25555555.55555555] is a double : 25555555.555556 [-25555555.55555555] is a double : -25555555.555556 [Hello World] is a not a number [555-55-55] is a not a number 

    Sorry for my rusty C.

    🙂

    So, in substance, you after the call of whatIsTheValue, you retrieve the type through the MyEnum enum, and then, according to the value in this enum, retrieve the right value, correctly typed, from the union MyUnion.

    Note that finding if the number is a double or a float is a bit more complicated because the difference seems to be in the precision, i.e. is your number representable in a double, or in float. A most ‘decimal real’ numbers are not exactly representable into a double, I would not bother.

    Note, too, that there is a catch, as 25.0 could be both real and an integer number. My comparing ‘dValue == (double)(long)dValue’, I guess you should know if is an integer, again, not taking into account the usual precision problems coming witb binary real numbers used by computers.

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

Sidebar

Related Questions

No related questions found

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer This should do it for you: var g_maxNum = 1;… May 11, 2026 at 8:52 am
  • added an answer Your project build configuration is probably set to Release for… May 11, 2026 at 8:52 am
  • added an answer SOrry this question was asked way too fast I found… May 11, 2026 at 8:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.