?
Solved

how to cast  char to int?

Posted on 2008-11-03
8
Medium Priority
?
1,752 Views
Last Modified: 2012-05-05
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


0
Comment
Question by:akohan
  • 4
  • 2
  • 2
8 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 600 total points
ID: 22874207
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
 

Author Comment

by:akohan
ID: 22874210

Yes, I need to convert it to integer.

Thanks!
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22874218
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 22874223
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
 
LVL 85

Assisted Solution

by:ozo
ozo earned 400 total points
ID: 22874241
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
 

Author Comment

by:akohan
ID: 22874311

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
 
LVL 85

Expert Comment

by:ozo
ID: 22874346
Only if argv strings aren't formated like %d
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22874347
using %s instead of converting to int and then using %d should save you some CPU cycles. Output would be the same.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question