473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

type cast from integer to char array

Jay
How can I cast Integer value 450 to a char[10] array "450 "? in a c program?

thanks in advance for any help.
Nov 13 '05 #1
10 53742
sprintf, sscanf

function will help you

if your environment is linux(unix), man page will give about those functions
to you.

"Jay" <ja*****@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
How can I cast Integer value 450 to a char[10] array "450 "? in a c program?
thanks in advance for any help.

Nov 13 '05 #2
Park Sung Jae <da*****@kornet.net> wrote in message
news:bg**********@news1.kornet.net...
sprintf, sscanf

function will help you


How is sscanf going to help?

-Mike

Nov 13 '05 #3

"Jay" <ja*****@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
How can I cast Integer value 450 to a char[10] array "450 "? in a c program?
thanks in advance for any help.


int number = 450;
char string[10];

sprintf(string, "%9d", number);

HTH
Allan
Nov 13 '05 #4
Jay <ja*****@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
How can I cast Integer value 450 to a char[10] array "450 "? in a c

program?

You don't. Are you sure you understand what a cast is?
It converts one type to another.

What you're really asking is how to store the textual
representation of a numeric type into an array of
characters. Use the 'sprintf()' function, which
works just like 'printf()' except its output goes
to a char array instead of stdout.

#include <stdio.h>

int main()
{
char array[10] = {0};
int i = 450;
sprintf(array, "%d", i);
printf("%s\n", array);
return 0;
}

-Mike

Nov 13 '05 #5
In <bg**********@slb6.atl.mindspring.net> "Mike Wahler" <mk******@mkwahler.net> writes:
Park Sung Jae <da*****@kornet.net> wrote in message
news:bg**********@news1.kornet.net...
sprintf, sscanf

function will help you


How is sscanf going to help?


Maybe he wants to check the result of the conversion ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
In <6d**************************@posting.google.com > ja*****@hotmail.com (Jay) writes:
How can I cast Integer value 450 to a char[10] array "450 "? in a c program?


Please read the FAQ *before* posting questions to this newsgroup.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7
In <bg**********@slb5.atl.mindspring.net> "Mike Wahler" <mk******@mkwahler.net> writes:
Jay <ja*****@hotmail.com> wrote in message
news:6d**************************@posting.google. com...
How can I cast Integer value 450 to a char[10] array "450 "? in a c
^^^^^^^^^^program?

#include <stdio.h>

int main()
{
char array[10] = {0};
int i = 450;
sprintf(array, "%d", i);
printf("%s\n", array);
return 0;
}


You're not solving the OP's problem: he wants the digits 450 followed by
7 spaces, i.e. not a string. The solution is a bit more complicated:

char array[10], buff[sizeof array + 1];
sprintf(buff, "%-10d", 450);
memcpy(array, buff, sizeof array);

Then again, maybe the OP actually wanted your solution, but didn't
formulate his question properly.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8
In <bg***********@news.hgc.com.hk> "Jeff" <no*****@notexist.com> writes:

"Jay" <ja*****@hotmail.com> wrote in message
news:6d**************************@posting.google. com...
How can I cast Integer value 450 to a char[10] array "450 "? in a c

program?

thanks in advance for any help.


Do you mean a char[10] array *including* the tailing null character ?


What "tailing null character"? The word "string" does not appear in OP's
request, does it?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #9
Jay
"Jeff" <no*****@notexist.com> wrote in message news:<bg***********@news.hgc.com.hk>...
"Jay" <ja*****@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
How can I cast Integer value 450 to a char[10] array "450 "? in a c

program?

thanks in advance for any help.


Do you mean a char[10] array *including* the tailing null character ?

char array[10];
sprintf(array, "%-9d", 450);
array[9] = '\0';


This answers my question, thank you all for your help.
Nov 13 '05 #10
In <6d**************************@posting.google.com > ja*****@hotmail.com (Jay) writes:
"Jeff" <no*****@notexist.com> wrote in message news:<bg***********@news.hgc.com.hk>...
"Jay" <ja*****@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
> How can I cast Integer value 450 to a char[10] array "450 "? in a c

program?
>
> thanks in advance for any help.


Do you mean a char[10] array *including* the tailing null character ?

char array[10];
sprintf(array, "%-9d", 450);
array[9] = '\0';


This answers my question, thank you all for your help.


Then, your question was extremely poorly phrased (your example shows 3
digits followed by 7 spaces). BTW, there is no point in overwriting
the null character appended by the sprintf call with array[9] = '\0'.
sprintf is quite good at generating strings...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Voronkov Konstantin | last post by:
Thank you for answer, but I still did not got *how* to make serialization of enum type. Can you provide more instructions or hint, please? My task is to serialize enum to something like byte...
3
by: Steffen Vulpius | last post by:
Hi everybody, VARCHAR has a higher precedence than CHAR. If you have a query SELECT COALESCE(c1,c2) FROM T1 where c1 is CHAR(5) and c2 is VARCHAR(10), I would expect the return type to be...
14
by: tweak | last post by:
I'm struggling with the concept of typecasting when setting up a TCP client. Here's a code snip (modified from W. Richard Stevens Unix Programming book) to demonstrate where I am struggling: ...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.