how to store Ascii character a to z in any one type of variable using strcpy?
for(i=97;i<122;i++)
{
for(j=97;j<122;j++)
{
printf("%c%c",i,j);
int a = strcpy(i,j);
}
}
how to store Ascii character a to z in any one type of variable using strcpy?
for(i=97;i<122;i++)
{
for(j=97;j<122;j++)
{
printf("%c%c",i,j);
int a = strcpy(i,j);
}
}
char a[3] = {0};
for(i=97;i<122;i++)
{
for(j=97;j<122;j++)
{
printf("%c%c\n", i, j);
a[0] = i;
a[1] = j;
printf("%s\n", a);
}
}
Ηow to store Ascii character in any one type of variable using
strcpy()
?
You don't need it, since for a single character strcpy()
is unnecessary.
Moreover, you try to handle an int
with strcpy()
, whereas this function handles char*
.
What you could do is something like this:
char myarray[3] = {0};
for(i = 97; i < 122; i++)
{
for(j = 97; j < 122; j++)
{
printf("%c%c\n", i, j);
myarray[0] = i;
myarray[1] = j;
printf("%s\n", myarray);
}
}
where I used a char array, since if you store the ASCII character in an int
you cannot expect it to print the same character, as it was stored in a char
. Even if you try Convert char array to a int number in C, you will not be able to succeed in it.
So (assuming ASCII):
char str[27]={0};
char temp[2]={0};
int i;
for (i='a';i<='z';i++) {
temp[0]=i;
strcpy(str,temp);
}
But do you really need to use strcpy
?
you can do it easily using type cast inside your loop
int num = 97;
char a = (char)num; // 'a' = 97 casting int to char
num = (int)a // num = 97 cast 'a' to ASCII
let it try
int main() {
int a = 0;
char c = '\0';
scanf(" %c", &c);
a = c + 0;
printf("%d", a);
}
Output: ASCII value of entered character.
I have provided the the code. Please refere it. Why this is So? Focus on the addition operation which is assigned to the int variable a. During compilation of this operation compiler finds that the variable c is of char type but; the operator + is next to it and hence compiler uses the ascii value of the character stored in c variable.
I hope it will helpful for you. Thank you.