I have a pointer lpBegin pointing to a string "1234". Now i want this string compare to an uint how can i make this string to unsigned integer without using scanf? I know the string number is 4 characters long.
-
use `atoi(lpBegin)` function. It returns an integer... – shan Sep 14 '12 at 08:09
-
I wouldn't go so far as to down vote answers recommending atoi, but please note that it is considered unsafe. [More info here](http://stackoverflow.com/questions/8871711/atoi-how-to-identify-the-diffrence-between-zero-and-error). – Lundin Sep 14 '12 at 08:29
8 Answers
You will have to use the atoi
function. This takes a pointer to a char
and returns an int
.
const char *str = "1234";
int res = atoi(str); //do something with res
As said by others and something I didn't know, is that atoi
is not recommended because it is undefined what happens when a formatting error occurs. So better use strtoul
as others have suggested.

- 69,818
- 15
- 125
- 179

- 61,704
- 67
- 242
- 415
You can use the strtoul()
function. strtoul stands for "String to unsigned long":
#include <stdio.h>
#include <stdlib.h>
int main()
{
char lpBegin[] = "1234";
unsigned long val = strtoul(lpBegin, NULL, 10);
printf("The integer is %ul.", val);
return 0;
}
You can find more information here: http://www.cplusplus.com/reference/clibrary/cstdlib/strtoul/

- 1,129
- 1
- 10
- 16
-
Since the function is called "String to unsigned long", why do you assign the result to and unsigned int? If you fix that, please also fix the printf conversion specifier. Oh, and there's a
missing and a void in the main arglist. What about a semicolon after the string literal? :-) – Jens Sep 14 '12 at 09:06 -
@Jens Lol, that comes from being too lazy to compile your example code :-) Thanks, I fixed it now. About the missing `void`: that's a religious question and my religion says it's stupid to write "void" for something that's empty. – fat-lobyte Sep 14 '12 at 09:12
-
Thanks, but still: the empty parens in `main()` are not allowed in C (while in C++ they are equivalent to void). Please take another look at the conversion specifier. What's wrong with it? – Jens Sep 14 '12 at 09:24
-
@fat-lobyte: it's a common misconception (including by me), that the C standard says that an empty arg list in a function definition provides a prototype for the function specifying that it takes no arguments. Actually it doesn't (and there's a defect report that concludes this). It specifies no arguments for the purposes of the definition of the function, but doesn't provide a prototype. So `int main()` is *not* exactly equivalent to `int main(void)`, and it's the latter that the standard endorses in 5.1.2.2.1. In practice every implementation allows `int main()`, as it is entitled to do. – Steve Jessop Sep 14 '12 at 09:53
-
Note that in the next para, it gives explicit permission to rename the parameters `argc/argv`. It doesn't give explicit permission to define `main` with no prototype. The perils of defining something by example -- the standard doesn't really say how far you can stray from the example. You could still argue that `int main()` does "define it with no parameters" as required, though. I haven't seen a DR on that, but there might be one. – Steve Jessop Sep 14 '12 at 09:57
-
@SteveJessop thanks for the information. I did not know that since I mostly use C++. – fat-lobyte Sep 14 '12 at 10:02
You could use strtoul(lpBegin)
, but this only works with zero-terminated strings.
If you don't want to use stdlib for whatever reason and you're absolutely sure about the target system(s), you could do the number conversion manually. This one should work on most systems as long as they are using single byte encoding (e.g. Latin, ISO-8859-1, EBCDIC). To make it work with UTF-16, just replace the 'char' with 'wchar_t' (or whatever you need).
unsigned long sum = (lpbegin[0] - '0') * 1000 +
(lpbegin[1] - '0') * 100 +
(lpbegin[2] - '0') * 10 +
(lpbegin[3] - '0');
or for numbers with unknown length:
char* c = lpBegin;
unsigned long sum = 0;
while (*c >= '0' && *c <= '9') {
sum *= 10;
sum += *c - '0';
++c;
}

- 6,033
- 1
- 19
- 14
-
3Why use the magic number `0x30` when you just as well might use `'0'`? Would it even work for EBCDIC then? – Jens Sep 14 '12 at 08:24
-
1@Jens: yes, it should work for EBCDIC then. The standard requires that the digits have consecutive `char` values in order from `0`, and both ASCII and EBCDIC satisfy that. – Steve Jessop Sep 14 '12 at 08:28
-
@unwind: I agree what the complaint was. I read the second question as, "if you made my suggested change, would it then work even for EBCDIC?". Regardless of whether that's what he meant, that's what I was answering "yes" to :-) – Steve Jessop Sep 14 '12 at 09:48
-
Yes, this would work for EBCDIC, too. It will however not work for wide-character strings (UTF-16). – Mark Sep 14 '12 at 22:47
-
Really? There's enough info to be considered as an *answer* (albeit not being elaborate). I don't think your downvote is justified. – P.P Sep 14 '12 at 08:33
-
The question: "how can i make this string to unsigned integer without using scanf?". Your answer: "strtol is better than atoi". How is a downvote _not_ justified? As far as the OP is concerned, you could as well have answered "x is better than y". – Lundin Sep 14 '12 at 09:10
-
Because there are already answers suggesting `atoi`. I don't have to explain it again my answer and added `strtol` is a better option. – P.P Sep 14 '12 at 09:23
-
Other answers may be removed or edited, also the sorting of answers on this site is a bit obscure, it is not fixed, so you cannot count on yours remaining before or after another answer. – Lundin Sep 14 '12 at 09:43
-
You should use the strtoul
function, "string to unsigned long". It is found in stdlib.h and has the following prototype:
unsigned long int strtoul (const char * restrict nptr,
char ** restrict endptr,
int base);
nptr
is the character string.endptr
is an optional parameter giving the location of where the function stopped reading valid numbers. If you aren't interested of this, pass NULL in this parameter.base
is the number format you expect the string to be in. In other words, 10 for decimal, 16 for hex, 2 for binary and so on.
Example:
#include <stdlib.h>
#include <stdio.h>
int main()
{
const char str[] = "1234random rubbish";
unsigned int i;
const char* endptr;
i = strtoul(str,
(char**)&endptr,
10);
printf("Integer: %u\n", i);
printf("Function stopped when it found: %s\n", endptr);
getchar();
return 0;
}
Regarding atoi().
atoi() internally just calls strtoul with base 10. atoi() is however not recommended, since the C standard does not define what happens when atoi() encounters a format error: atoi() can then possibly crash. It is therefore better practice to always use strtoul() (and the other similar strto... functions).

- 195,001
- 40
- 254
- 396
-
@user1670792 Because 'r' is not a valid digit in the base 10 number system, only '0' to '9' are valid. Had we set base 16, it would have accepted letters 'A' to 'F' as well. – Lundin Sep 14 '12 at 09:45
If you're really certain the string is 4 digits long, and don't want to use any library function (for whatever reason), you can hardcode it:
const char *lpBegin = "1234";
const unsigned int lpInt = 1000 * (lpBegin[0] - '0') +
100 * (lpBegin[1] - '0') +
10 * (lpBegin[2] - '0') +
1 (lpBegin[3] - '0');
Of course, using e.g. strtoul()
is vastly superior so if you have the library available, use it.

- 391,730
- 64
- 469
- 606