Reputation: 27
I have this problem as a part of my c-programming project. I read users input to char type array (char*str
) and I need to convert some parts of the string input to integer. The input might be "A smeagol 21 fire 22"
Here is some testing. I try to get x=40. This code's gives x=-4324242. Why this code don't work?
#include <stdio.h>
int main(){
char *uga[1];
uga[0] = "10";
printf("%s\n", uga[0]);
int x = 50 - (int)uga[0];
printf("%d",x);
}
Thank you beforehand.
Upvotes: 1
Views: 748
Reputation: 33
You should use the function atoi()
under stdlib.h
.
For instance,
int val;
char strn1[] = "12546";
val = atoi(strn1);
printf("String value = %s\n", strn1);
printf("Integer value = %d\n", val);
char strn2[] = "12Ge 1 89";
val = atoi(strn2);
printf("String value = %s\n", strn2);
printf("Integer value = %d\n", val);
String value = 12546
Integer value = 12546
String value = 12Ge 1 89
Integer value = 12
Hope it helps! gives the
Upvotes: 0
Reputation: 3699
You can use strtol
, sscanf
or atoi
to convert a string to int.
For example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
char *str[3] = {"10", "20 test of strtol\n", "30"};
int a = atoi(str[0]);
printf("a = %d\n", a);
char *ptr;
long int b = strtol(str[1], &ptr, 10); // you can alse use strtoll for long long int
printf("b = %ld, string: %s", b, ptr);
int c;
sscanf(str[2], "%d", &c);
printf("c = %d\n", c);
return 0;
}
Output:
a = 10
b = 20, string: test of strtol
c = 30
In your code, uga[0]
is a pointer that points to a character. So (int) uga[0]
just cast the address of the char
pointer.
Upvotes: 1
Reputation: 4288
You can use sscanf
also like this for example
#include <stdio.h>
int main(){
char str[] = "A smeagol 21 fire 22";
int a, b;
if (2 != sscanf(str, "%*[^0-9] %d %*[^0-9] %d", &a, &b))
return printf("Error\n"), 1;
printf("%d %d",a, b);
}
Output
21 22
%*[^0-9]
will match any non digit character and it will be discarded (because of the *
). sscanf
return the number of variables which it was able to fill. Which should be 2 here, otherwise something went wrong.
Upvotes: 0
Reputation: 691
There's two problems in your code.
char uga[max_size_of_your_string] = "Initialization if you want to";
(int)uga[0];
won't work as you expect, because you're trying to convert an address to an integer.I advise you to declare properly your string uga
, and to take a look at the different ways of converting a numerical string to integer here : How to convert a string to integer in C?
Upvotes: 0
Reputation: 21542
Type casting a char
pointer or array to int
or another numeric type will not give you the numeric value of the text in the string.
You should use the functions strtol
(signed), strtoul
(unsigned) to accomplish this:
char *str = "123ABC";
char *pStop;
// (Arguments: string to parse, pointer to character that stops the parse, base/radix)
int value = strtol(str, &pStop, 10);
After this, value
will be 123
, and pStop
will point to the 'A' in str
. The second argument can be NULL
if you don't care about it, but it's useful for processing a string with lots of numbers in it between non-numeric text.
Upvotes: 0