• C

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


akohanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
You need to convert the string to int if you wish to use %d format specifier .. use strtol (prefered) or atoi to achieve that.

Otherwise, you can simply use %s or %c (depending on if its a string or char) as the format specifier
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akohanAuthor Commented:

Yes, I need to convert it to integer.

Thanks!
0
sunnycoderCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sunnycoderCommented:
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 :)
0
ozoCommented:
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);
or
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);
0
akohanAuthor Commented:

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.
0
ozoCommented:
Only if argv strings aren't formated like %d
0
sunnycoderCommented:
using %s instead of converting to int and then using %d should save you some CPU cycles. Output would be the same.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.