How to convert the number 0 with strtol? [duplicate]

Multi tool use
Multi tool use












2
















This question already has an answer here:




  • How to use `strtoul` to parse string where zero may be valid?

    4 answers




I need strtol to convert some numbers from a range of [0 to 255]
how can I check the conversion of 0 if 0 is also a number that i need to convert?



long conv = strtol(argv, &p, 10);
if (conv == 0)
{
perror("Conversion error");
exit(EXIT_FAILURE);
}
if ((conv >= LONG_MAX || conv <= LONG_MIN) && errno == 34);
{
perror("Invalid Range!");
exit(EXIT_FAILURE);
}









share|improve this question















marked as duplicate by Sourav Ghosh c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 4





    Holy crap, that function really does use zero as a signaling value. Whose idea was that?

    – Robert Harvey
    Jan 2 at 16:00






  • 2





    check if p is different from argv

    – Ôrel
    Jan 2 at 16:01











  • Actually, I think zero is just the default return value if it cannot parse the string.

    – Robert Harvey
    Jan 2 at 16:04













  • Unknown, @Ôrel comments correctly. After strtol(argv, &p, 10), if (argv == p) perror("Conversion error");

    – chux
    Jan 2 at 19:20


















2
















This question already has an answer here:




  • How to use `strtoul` to parse string where zero may be valid?

    4 answers




I need strtol to convert some numbers from a range of [0 to 255]
how can I check the conversion of 0 if 0 is also a number that i need to convert?



long conv = strtol(argv, &p, 10);
if (conv == 0)
{
perror("Conversion error");
exit(EXIT_FAILURE);
}
if ((conv >= LONG_MAX || conv <= LONG_MIN) && errno == 34);
{
perror("Invalid Range!");
exit(EXIT_FAILURE);
}









share|improve this question















marked as duplicate by Sourav Ghosh c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 4





    Holy crap, that function really does use zero as a signaling value. Whose idea was that?

    – Robert Harvey
    Jan 2 at 16:00






  • 2





    check if p is different from argv

    – Ôrel
    Jan 2 at 16:01











  • Actually, I think zero is just the default return value if it cannot parse the string.

    – Robert Harvey
    Jan 2 at 16:04













  • Unknown, @Ôrel comments correctly. After strtol(argv, &p, 10), if (argv == p) perror("Conversion error");

    – chux
    Jan 2 at 19:20
















2












2








2


1







This question already has an answer here:




  • How to use `strtoul` to parse string where zero may be valid?

    4 answers




I need strtol to convert some numbers from a range of [0 to 255]
how can I check the conversion of 0 if 0 is also a number that i need to convert?



long conv = strtol(argv, &p, 10);
if (conv == 0)
{
perror("Conversion error");
exit(EXIT_FAILURE);
}
if ((conv >= LONG_MAX || conv <= LONG_MIN) && errno == 34);
{
perror("Invalid Range!");
exit(EXIT_FAILURE);
}









share|improve this question

















This question already has an answer here:




  • How to use `strtoul` to parse string where zero may be valid?

    4 answers




I need strtol to convert some numbers from a range of [0 to 255]
how can I check the conversion of 0 if 0 is also a number that i need to convert?



long conv = strtol(argv, &p, 10);
if (conv == 0)
{
perror("Conversion error");
exit(EXIT_FAILURE);
}
if ((conv >= LONG_MAX || conv <= LONG_MIN) && errno == 34);
{
perror("Invalid Range!");
exit(EXIT_FAILURE);
}




This question already has an answer here:




  • How to use `strtoul` to parse string where zero may be valid?

    4 answers








c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 16:08









4386427

21.8k31846




21.8k31846










asked Jan 2 at 15:58









UnknownUnknown

245




245




marked as duplicate by Sourav Ghosh c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Sourav Ghosh c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 4





    Holy crap, that function really does use zero as a signaling value. Whose idea was that?

    – Robert Harvey
    Jan 2 at 16:00






  • 2





    check if p is different from argv

    – Ôrel
    Jan 2 at 16:01











  • Actually, I think zero is just the default return value if it cannot parse the string.

    – Robert Harvey
    Jan 2 at 16:04













  • Unknown, @Ôrel comments correctly. After strtol(argv, &p, 10), if (argv == p) perror("Conversion error");

    – chux
    Jan 2 at 19:20
















  • 4





    Holy crap, that function really does use zero as a signaling value. Whose idea was that?

    – Robert Harvey
    Jan 2 at 16:00






  • 2





    check if p is different from argv

    – Ôrel
    Jan 2 at 16:01











  • Actually, I think zero is just the default return value if it cannot parse the string.

    – Robert Harvey
    Jan 2 at 16:04













  • Unknown, @Ôrel comments correctly. After strtol(argv, &p, 10), if (argv == p) perror("Conversion error");

    – chux
    Jan 2 at 19:20










4




4





Holy crap, that function really does use zero as a signaling value. Whose idea was that?

– Robert Harvey
Jan 2 at 16:00





Holy crap, that function really does use zero as a signaling value. Whose idea was that?

– Robert Harvey
Jan 2 at 16:00




2




2





check if p is different from argv

– Ôrel
Jan 2 at 16:01





check if p is different from argv

– Ôrel
Jan 2 at 16:01













Actually, I think zero is just the default return value if it cannot parse the string.

– Robert Harvey
Jan 2 at 16:04







Actually, I think zero is just the default return value if it cannot parse the string.

– Robert Harvey
Jan 2 at 16:04















Unknown, @Ôrel comments correctly. After strtol(argv, &p, 10), if (argv == p) perror("Conversion error");

– chux
Jan 2 at 19:20







Unknown, @Ôrel comments correctly. After strtol(argv, &p, 10), if (argv == p) perror("Conversion error");

– chux
Jan 2 at 19:20














1 Answer
1






active

oldest

votes


















4














Prior to calling strtol, set errno to 0.



Then after the call, check the value of errno. If it's 0, you know the call was successful. Addtionally, you'll want to check if *p is 0. If so, that means the entire string was parsed successfully with no extra characters.



errno = 0;
long conv = strtol(argv, &p, 10);
if (errno)
{
perror("Conversion error");
exit(EXIT_FAILURE);
}
else if (*p)
{
perror("Not all characters converted");
exit(EXIT_FAILURE);
}


The man page also mentions this in the "Notes" section:




Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN
(LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure,
the calling
program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a nonzero
value after the
call.







share|improve this answer


























  • thank you. that helps

    – Unknown
    Jan 2 at 16:15






  • 1





    I'm not sure it is required that errno is set when no conversion is done. The man page says: "The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned)." and the standard only mentions ERANGE as errno in case of over/underflow. This example doesn't set errno ideone.com/T3ddyH . I think you need to compare argv and p to catch the case with no conversion

    – 4386427
    Jan 2 at 16:36













  • The C standard has no requirements about errno when conversion fails (only range issues). The Linux man page is inconsistent with the C spec on this point. Code should check if (argv == p) for the no conversion condition.

    – chux
    Jan 2 at 19:25













  • @chux The code I've posted should account for all error cases, whether or not errno is set to EINVAL. It both checks errno` and check that p points to the end of the string to test for a complete conversion.

    – dbush
    Jan 2 at 19:30











  • If errno was not set by strtol() on a non-conversion (which is compliant with the C standard), this code would errantly not complain about strtol("", &p, 10);

    – chux
    Jan 2 at 22:02




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Prior to calling strtol, set errno to 0.



Then after the call, check the value of errno. If it's 0, you know the call was successful. Addtionally, you'll want to check if *p is 0. If so, that means the entire string was parsed successfully with no extra characters.



errno = 0;
long conv = strtol(argv, &p, 10);
if (errno)
{
perror("Conversion error");
exit(EXIT_FAILURE);
}
else if (*p)
{
perror("Not all characters converted");
exit(EXIT_FAILURE);
}


The man page also mentions this in the "Notes" section:




Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN
(LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure,
the calling
program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a nonzero
value after the
call.







share|improve this answer


























  • thank you. that helps

    – Unknown
    Jan 2 at 16:15






  • 1





    I'm not sure it is required that errno is set when no conversion is done. The man page says: "The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned)." and the standard only mentions ERANGE as errno in case of over/underflow. This example doesn't set errno ideone.com/T3ddyH . I think you need to compare argv and p to catch the case with no conversion

    – 4386427
    Jan 2 at 16:36













  • The C standard has no requirements about errno when conversion fails (only range issues). The Linux man page is inconsistent with the C spec on this point. Code should check if (argv == p) for the no conversion condition.

    – chux
    Jan 2 at 19:25













  • @chux The code I've posted should account for all error cases, whether or not errno is set to EINVAL. It both checks errno` and check that p points to the end of the string to test for a complete conversion.

    – dbush
    Jan 2 at 19:30











  • If errno was not set by strtol() on a non-conversion (which is compliant with the C standard), this code would errantly not complain about strtol("", &p, 10);

    – chux
    Jan 2 at 22:02


















4














Prior to calling strtol, set errno to 0.



Then after the call, check the value of errno. If it's 0, you know the call was successful. Addtionally, you'll want to check if *p is 0. If so, that means the entire string was parsed successfully with no extra characters.



errno = 0;
long conv = strtol(argv, &p, 10);
if (errno)
{
perror("Conversion error");
exit(EXIT_FAILURE);
}
else if (*p)
{
perror("Not all characters converted");
exit(EXIT_FAILURE);
}


The man page also mentions this in the "Notes" section:




Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN
(LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure,
the calling
program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a nonzero
value after the
call.







share|improve this answer


























  • thank you. that helps

    – Unknown
    Jan 2 at 16:15






  • 1





    I'm not sure it is required that errno is set when no conversion is done. The man page says: "The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned)." and the standard only mentions ERANGE as errno in case of over/underflow. This example doesn't set errno ideone.com/T3ddyH . I think you need to compare argv and p to catch the case with no conversion

    – 4386427
    Jan 2 at 16:36













  • The C standard has no requirements about errno when conversion fails (only range issues). The Linux man page is inconsistent with the C spec on this point. Code should check if (argv == p) for the no conversion condition.

    – chux
    Jan 2 at 19:25













  • @chux The code I've posted should account for all error cases, whether or not errno is set to EINVAL. It both checks errno` and check that p points to the end of the string to test for a complete conversion.

    – dbush
    Jan 2 at 19:30











  • If errno was not set by strtol() on a non-conversion (which is compliant with the C standard), this code would errantly not complain about strtol("", &p, 10);

    – chux
    Jan 2 at 22:02
















4












4








4







Prior to calling strtol, set errno to 0.



Then after the call, check the value of errno. If it's 0, you know the call was successful. Addtionally, you'll want to check if *p is 0. If so, that means the entire string was parsed successfully with no extra characters.



errno = 0;
long conv = strtol(argv, &p, 10);
if (errno)
{
perror("Conversion error");
exit(EXIT_FAILURE);
}
else if (*p)
{
perror("Not all characters converted");
exit(EXIT_FAILURE);
}


The man page also mentions this in the "Notes" section:




Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN
(LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure,
the calling
program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a nonzero
value after the
call.







share|improve this answer















Prior to calling strtol, set errno to 0.



Then after the call, check the value of errno. If it's 0, you know the call was successful. Addtionally, you'll want to check if *p is 0. If so, that means the entire string was parsed successfully with no extra characters.



errno = 0;
long conv = strtol(argv, &p, 10);
if (errno)
{
perror("Conversion error");
exit(EXIT_FAILURE);
}
else if (*p)
{
perror("Not all characters converted");
exit(EXIT_FAILURE);
}


The man page also mentions this in the "Notes" section:




Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN
(LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure,
the calling
program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a nonzero
value after the
call.








share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 17:32

























answered Jan 2 at 16:00









dbushdbush

102k13108144




102k13108144













  • thank you. that helps

    – Unknown
    Jan 2 at 16:15






  • 1





    I'm not sure it is required that errno is set when no conversion is done. The man page says: "The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned)." and the standard only mentions ERANGE as errno in case of over/underflow. This example doesn't set errno ideone.com/T3ddyH . I think you need to compare argv and p to catch the case with no conversion

    – 4386427
    Jan 2 at 16:36













  • The C standard has no requirements about errno when conversion fails (only range issues). The Linux man page is inconsistent with the C spec on this point. Code should check if (argv == p) for the no conversion condition.

    – chux
    Jan 2 at 19:25













  • @chux The code I've posted should account for all error cases, whether or not errno is set to EINVAL. It both checks errno` and check that p points to the end of the string to test for a complete conversion.

    – dbush
    Jan 2 at 19:30











  • If errno was not set by strtol() on a non-conversion (which is compliant with the C standard), this code would errantly not complain about strtol("", &p, 10);

    – chux
    Jan 2 at 22:02





















  • thank you. that helps

    – Unknown
    Jan 2 at 16:15






  • 1





    I'm not sure it is required that errno is set when no conversion is done. The man page says: "The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned)." and the standard only mentions ERANGE as errno in case of over/underflow. This example doesn't set errno ideone.com/T3ddyH . I think you need to compare argv and p to catch the case with no conversion

    – 4386427
    Jan 2 at 16:36













  • The C standard has no requirements about errno when conversion fails (only range issues). The Linux man page is inconsistent with the C spec on this point. Code should check if (argv == p) for the no conversion condition.

    – chux
    Jan 2 at 19:25













  • @chux The code I've posted should account for all error cases, whether or not errno is set to EINVAL. It both checks errno` and check that p points to the end of the string to test for a complete conversion.

    – dbush
    Jan 2 at 19:30











  • If errno was not set by strtol() on a non-conversion (which is compliant with the C standard), this code would errantly not complain about strtol("", &p, 10);

    – chux
    Jan 2 at 22:02



















thank you. that helps

– Unknown
Jan 2 at 16:15





thank you. that helps

– Unknown
Jan 2 at 16:15




1




1





I'm not sure it is required that errno is set when no conversion is done. The man page says: "The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned)." and the standard only mentions ERANGE as errno in case of over/underflow. This example doesn't set errno ideone.com/T3ddyH . I think you need to compare argv and p to catch the case with no conversion

– 4386427
Jan 2 at 16:36







I'm not sure it is required that errno is set when no conversion is done. The man page says: "The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned)." and the standard only mentions ERANGE as errno in case of over/underflow. This example doesn't set errno ideone.com/T3ddyH . I think you need to compare argv and p to catch the case with no conversion

– 4386427
Jan 2 at 16:36















The C standard has no requirements about errno when conversion fails (only range issues). The Linux man page is inconsistent with the C spec on this point. Code should check if (argv == p) for the no conversion condition.

– chux
Jan 2 at 19:25







The C standard has no requirements about errno when conversion fails (only range issues). The Linux man page is inconsistent with the C spec on this point. Code should check if (argv == p) for the no conversion condition.

– chux
Jan 2 at 19:25















@chux The code I've posted should account for all error cases, whether or not errno is set to EINVAL. It both checks errno` and check that p points to the end of the string to test for a complete conversion.

– dbush
Jan 2 at 19:30





@chux The code I've posted should account for all error cases, whether or not errno is set to EINVAL. It both checks errno` and check that p points to the end of the string to test for a complete conversion.

– dbush
Jan 2 at 19:30













If errno was not set by strtol() on a non-conversion (which is compliant with the C standard), this code would errantly not complain about strtol("", &p, 10);

– chux
Jan 2 at 22:02







If errno was not set by strtol() on a non-conversion (which is compliant with the C standard), this code would errantly not complain about strtol("", &p, 10);

– chux
Jan 2 at 22:02







ZiIoDkve0uuXJc n8vZa 6gq
e,fwwASlOBq,YYEPH3U,8qs85yuxvB 2fS0sp2Wj9117uGn,h Do,mmfNTPeUCQy,k4PIsHwGJedGFwEqI1gOo fSjiMPd f1PM XULG

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas