c - Printing elements of a char array with numbes and letters -
i trying print out elements of char array consists of both numbers , letters. code is:
char *receiveinput(char *s) {     scanf("%99s", s);     return s; }  int main() {     char str[100], inp[50] = "";     printf("enter string");     receiveinput(str);     char ctostr[3];     int num = 3;     char c = (char)(num);     ctostr[0] = c;     ctostr[1] = str[0];     ctostr[2] = '\0';     strcat(inp, ctostr);     printf("%s\n", inp);     return 0; } lets str in "hey", inp should contain , print "3h" instead prints 'h' when ctostr[0] = c (which char 3). 
how print elements contain both numbers , letters?
number 3 not same character '3'. example in ascii table character '3' has integer code in hex 0x33 while in ebcdic table has code 0xf3.
you can write
  int num = 3;   char c = num + '0';   ctostr[0] = c; provided num less 10.
Comments
Post a Comment