C language > Beginner questions

int variable displayed as a ascii-character

(1/1)

cd:
hi everybody,

i've got a problem with a little thing.


--- Code: ---// the declaration
int a= 32 ;

--- End code ---

how do i  get the ascii-character for the number wich is in the variable a? ???

thx

cd

cane:
In this case the character to display is a blank space. BTW, keep in mind that there are other ASCII characters which are non-printable, incidentally the range 0-31.

cd:
Oh ok didn't knew that. but just in case for another ascii-character like 75
--- Code: --- int a=75;
--- End code ---
and i wanna convert that one to an ascii-character is there a winapi function oder a usual library function wich i can use for it ?

and here just another question to it.

when i use a char array with values like that :

--- Code: ---char *x={ Aa01 , Bb02 } ;

--- End code ---

is a array like that :


--- Code: ---char x[8];

--- End code ---

equeal to that :

--- Code: ---char x[8]={ Aa01, Bb02 }; = =  char x[0]=A , x[1]=a , x[2]=0 , x[3]=1 , x[4]=B , x[5]=b , x[6]=0 , x[7]=2 x[8]='\0';

--- End code ---

or are only the first two arrays with the values declared :

--- Code: ---char x[0]= Aa01 , x[1]= Bb02;

--- End code ---

i only wanna knew or get a correction of my thoughts - help is really appreciated - thx.

later
cd

AlexN:

--- Quote from: cd on June 16, 2007, 11:52:44 AM ---Oh ok didn't knew that. but just in case for another ascii-character like 75
--- Code: --- int a=75;
--- End code ---
and i wanna convert that one to an ascii-character is there a winapi function oder a usual library function wich i can use for it ?

--- End quote ---
If you want to know which character is stored in a you can print it with

--- Code: --- printf("%c", a);
[\code]

[quote author=cd link=topic=2206.msg8286#msg8286 date=1181987564]
and here just another question to it.

when i use a char array with values like that :
[code]
char *x={ Aa01 , Bb02 } ;

--- End code ---
[/quote]
This code will not create an array.
To create an array you will need something like this:

--- Code: ---char x[][] = {"Aa01", "Bb02"};
printf("%s\n", x[0]);         //the output should be Aa01
printf("%s\n", x[1]);         //the output should be Bb02

--- End code ---


--- Quote from: cd on June 16, 2007, 11:52:44 AM ---is a array like that :


--- Code: ---char x[8];

--- End code ---

equeal to that :

--- Code: ---char x[8]={ Aa01, Bb02 }; = =  char x[0]=A , x[1]=a , x[2]=0 , x[3]=1 , x[4]=B , x[5]=b , x[6]=0 , x[7]=2 x[8]='\0';

--- End code ---

--- End quote ---
for this the array-size must be 9, because you have 2 times 4 characters and the endig zero.

if you want to store this kind of text in your array, you need some code like this:

--- Code: ---char x[9];

strcpy(x, "Aa01");          // this will copy the string "Aa01" to x
strcat(x, "Bb02");          // this will merge the string "Bb02" to the string x
printf("%s\n", x);          // You will get the output Aa01Bb01

--- End code ---

I hope this can help you.  ;)

[/code]

cd:
you helped me alot. yes, wright my declaration was just a pointer not a character array and many thanks to solve the question with the integer array.i wasn't sure if i have to cast the type of character or just to use the %c in the printf function , thank you very much.

Navigation

[0] Message Index

Go to full version