akohan
asked on
how to cast char to int?
Hello group,
Since I'm passing some number to main() using char* argv[]
I need to assign them to some format specifiers (%d) in sprintf(), now having
sprintf(cmdstring, "/usr/user1/prog grp=%d id=%d yy=%d mm=%d dd=%d hh=%d > %s ",
current_item->grp_id, current_item->id, argv[1], argv[2], argv[3], argv[4], outfile);
causes warning as
warning: int format, pointer arg (arg 3)
warning: int format, pointer arg (arg 4)
warning: int format, pointer arg (arg 5)
warning: int format, pointer arg (arg 6)
warning: int format, pointer arg (arg 7)
warning: int format, pointer arg (arg 8)
How can I fix this? should I go with ctoi() or there is a better approach?
Thanks in advnace.
ak
Since I'm passing some number to main() using char* argv[]
I need to assign them to some format specifiers (%d) in sprintf(), now having
sprintf(cmdstring, "/usr/user1/prog grp=%d id=%d yy=%d mm=%d dd=%d hh=%d > %s ",
current_item->grp_id, current_item->id, argv[1], argv[2], argv[3], argv[4], outfile);
causes warning as
warning: int format, pointer arg (arg 3)
warning: int format, pointer arg (arg 4)
warning: int format, pointer arg (arg 5)
warning: int format, pointer arg (arg 6)
warning: int format, pointer arg (arg 7)
warning: int format, pointer arg (arg 8)
How can I fix this? should I go with ctoi() or there is a better approach?
Thanks in advnace.
ak
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
argv[1] -- you would want to use %s as format specifier ...
If the argument was say character '9' .. then in argv[n] would contain "9" .. that is string 9 - a string of length 1 terminated by \0 ...
%s would simply get this string copied over to cmdstring which in effect is same as converting it to integer 9 and then printing it to cmdstring with %d
If the argument was say character '9' .. then in argv[n] would contain "9" .. that is string 9 - a string of length 1 terminated by \0 ...
%s would simply get this string copied over to cmdstring which in effect is same as converting it to integer 9 and then printing it to cmdstring with %d
If you still wish to convert to integer then use
strtol or atoi
http://linux.die.net/man/3/strtol
http://linux.die.net/man/3/atoi
Also note - char does not need to be converted to int ... char is held internally as an int ... You would be converting a string here :)
strtol or atoi
http://linux.die.net/man/3/strtol
http://linux.die.net/man/3/atoi
Also note - char does not need to be converted to int ... char is held internally as an int ... You would be converting a string here :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what would be the difference between
sprintf(cmdstring, "/usr/user1/prog grp=%d id=%d yy=%d mm=%d dd=%d hh=%d > %s ",
current_item->grp_id, current_item->id, (int)atol(argv[1]), (int)atol(argv[2]), (int)atol(argv[3]), (int)atol(argv[4]), outfile);
and
sprintf(cmdstring, "/usr/user1/prog grp=%d id=%d yy=%s mm=%s dd=%s hh=%s > %s ",
current_item->grp_id, current_item->id, argv[1], argv[2], argv[3], argv[4], outfile);
I mean is there any advantage of using one over the other one?
Thanks.
Only if argv strings aren't formated like %d
using %s instead of converting to int and then using %d should save you some CPU cycles. Output would be the same.
ASKER
Yes, I need to convert it to integer.
Thanks!