int & char

C

c language

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;
char CC[C];
int i;
int c;

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

/* 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]);
}
}
 
S

Simon Biber

c said:
#include <stdio.h>

#define I 10
#define C 10

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

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

/* 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;
 
D

Default User

c said:
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;
char CC[C];
int i;
int c;

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

/* 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
 
D

Default User

Simon said:
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
 
K

Keith Thompson

Default User said:
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).
 
D

Default User

Keith said:
Default User said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top