473,320 Members | 1,909 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,320 software developers and data experts.

int & char

Hello everybody,
I have an array of numbers that I want to put them in a string. I have
an example that you could complete it if you could or just let me know
how I can do that.
Thank you very much,
MJ

#include <stdio.h>

#define I 10
#define C 10

int main()
{
int II[i];
char CC[C];
int i;
int c;

for(i=0;i<10;i++)
{
II[i]=i+1;
printf("%d %d\n",i,II[i]);
}

/* the following part is not working? */
for(c=0;c<10;c++)
{
CC[c]=char(II[c]);
printf("%d %c\n",II[c],CC[c]);
}
}

Jul 20 '06 #1
5 2228
c language wrote:
#include <stdio.h>

#define I 10
#define C 10

int main()
{
int II[i];
char CC[C];
int i;
int c;

for(i=0;i<10;i++)
{
II[i]=i+1;
printf("%d %d\n",i,II[i]);
}

/* the following part is not working? */
for(c=0;c<10;c++)
{
CC[c]=char(II[c]);
This is a syntax error. You can't use char as a function. I assume you
wanted to convert II[c] to char. That conversion will happen
automatically when you write
CC[c] = II[c];
There is no need for an explicit cast.
printf("%d %c\n",II[c],CC[c]);
CC[c] contains a number, from 1 to 10, not a character. By using the %c
specifier in printf you are asking it to print a character. If you try
to interpret the numbers as a character, you will get strange results.

These days most C implementations are based on ASCII, where characters
numbered 1 to 10 are control characters (SOH, STX, ETX, EOT, ENQ, ACK,
BEL, BS, TAB, LF).

You should print CC[c] using %d just like any other integer.
}
Missing
return 0;
}
--
Simon.
Jul 20 '06 #2
c language wrote:
Hello everybody,
I have an array of numbers that I want to put them in a string. I have
an example that you could complete it if you could or just let me know
how I can do that.
Thank you very much,
MJ

#include <stdio.h>

#define I 10
#define C 10

int main()
{
int II[i];
char CC[C];
int i;
int c;

for(i=0;i<10;i++)
{
II[i]=i+1;
printf("%d %d\n",i,II[i]);
}

/* the following part is not working? */
for(c=0;c<10;c++)
{
CC[c]=char(II[c]);
printf("%d %c\n",II[c],CC[c]);
}
}

You want the character representation of the number, not the number
itself. Do this instead:

CC[c] = II[c] + '0';
This will ONLY work for values in II that are less than 10. If you need
a more general solution, then sprintf() is what you need to look at.


Brian
Jul 20 '06 #3
Simon Biber wrote:
c language wrote:
for(c=0;c<10;c++)
{
CC[c]=char(II[c]);

This is a syntax error. You can't use char as a function.
Looks like he (guess) is doing a C++ style cast. He's also multi-posted
to comp.lang.c++.

Brian
Jul 20 '06 #4
"Default User" <de***********@yahoo.comwrites:
[...]
You want the character representation of the number, not the number
itself. Do this instead:

CC[c] = II[c] + '0';
This will ONLY work for values in II that are less than 10. If you need
a more general solution, then sprintf() is what you need to look at.
Correction: This will only work for values in II that are in the range
0 to 9 (i.e., it will fail for negative values as well).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 20 '06 #5
Keith Thompson wrote:
"Default User" <de***********@yahoo.comwrites:
[...]
You want the character representation of the number, not the number
itself. Do this instead:

CC[c] = II[c] + '0';
This will ONLY work for values in II that are less than 10. If you
need a more general solution, then sprintf() is what you need to
look at.

Correction: This will only work for values in II that are in the range
0 to 9 (i.e., it will fail for negative values as well).
Yep.
Brian
Jul 20 '06 #6

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

Similar topics

4
by: josef angermeier | last post by:
i wrote a display driver for a lcd segment display which doesnt recognize ascii character so that each character of an output string needs to be converted byte by byte by looking in a const...
2
by: Claudio | last post by:
hi all i put in this email source code so u can copy and paste to verify strange first : in this example bit size is a BYTE ?! second : in the last printf output is wrong ? ? best...
8
by: aling | last post by:
Given following code snippets: int a; int* p=(int*)((&a)+1); when I debuged the code using IDE like VC7.1, I found that: a = 0x0012fc50 (int ) p = 0x0012fc78 (int *) (&a)+1 = 0x0012fc51...
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
2
by: TonyM | last post by:
Q: Is there a good way to overcome this apparent bug without modifying the mfc code? ___________________________________ Info: Although it is NOT a good idea, I seemed to have perhaps located a...
0
by: Sushma | last post by:
CSiFtpConnection error LNK2001: unresolved external symbol "public: virtual class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > > __thiscall...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
6
by: cj | last post by:
I'm receiving an xml formatted string that I pull data from by reading it into an xml document like this: Dim doc As New Xml.XmlDocument doc.LoadXml(respstr) Dim co_name As Xml.XmlNodeList =...
16
by: arnuld | last post by:
i am not able to make it work. it compiles without any error but does not work: what i WANTED: 1.) 1st we will take the input in to an array. (calling "getline" in "main") 2.) we will print...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.