Home:ALL Converter>C input of char returns a different char

C input of char returns a different char

Ask Time:2015-12-08T17:06:15         Author:SpartanHero

Json Formatter

I am learning C and I've stumbled upon a really weird problem. When I input a single char, it returns 1-2 different chars. I don't really get why it returns the different chars instead of the original. could someone please explain me why it doesn't work?

the goal of this code is to accept 4 char inputs and return the input given.

code:

#include <stdio.h>

int main() {

 char c;

 c = getchar();
 getchar();
 c = c + "/0";
 printf("%c\n", c);

 c = getchar();
 getchar();
 c = c + "/0";
 printf("%c\n", c);

 c = getchar();
 getchar();
 c = c + "/0";
 printf("%c\n", c);

 c = getchar();
 getchar();
 c = c + "/0";
 printf("%c\n", c);

 return 0;
}

Author:SpartanHero,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34151807/c-input-of-char-returns-a-different-char
ilent2 :

In C, characters and strings have different data types: a character is an integer (typically 8-bits long with value from -128 to 128), and a string is an array of characters (either a C-style string where the end of the string is represented by a null character \\0, or some other form of array).\n\nCharacters\n\nIf you want to store a character in a variable of type char you could use any of the following:\n\nchar c = 'c'; //< Set c to the character 'c'\nchar c; c = 'h'; //< Set c to the character 'h'\nchar c = '\\65'; //< Set c to the character 'A' (see ASCII codes)\nchar c = getchar(); //< Store a character from a function returning char\nchar c = 10; //< Not always considered good practice (but still valid)\n\n\nStrings\n\nIf you want a C-style string in C you could use:\n\nchar szMyString[] = \"This is my string\"; //< Array of 18 characters\nchar szAnotherString[5] = \"asdf\"; //< Array of 5 characters\nchar szBuf[5]; sprintf(szBuf, \"hi\"); //< Array len: 5, String len: 2\nchar szBuf[128]; scanf(\"%s\", szBuf); //< Ask the user for a string\nchar szBuf[5]; szBuf[0] = 'a'; szBuf[1] = '\\0'; //< String with length 1\nchar szBuf[5]; szBuf[0] = 'a'; //< Not a string (just an array of type char)\n\n\nIt is important to note that characters are written in C using the single quotes ' and strings using the double quotes \". In other languages (like Python) you can use double or single quotes mostly interchangeably but in C there is a difference.\n\nJoining Strings\n\nIf you want to join two strings you need to make sure you have enough space to contain both strings. You can join two strings by adding the second to the end of the first or copy both original strings into a new array.\n\n// Append to the first string\nchar szFirst[128] = \"The first string...\";\nchar szSecond[] = \"and the second string.\";\nstrcat(szFirst, szSecond);\n\n// Copy strings into new array\nchar szJoined[128];\nchar szFirst[] = \"Hello\";\nchar szSecond[] = \"World\";\nsprintf(szJoined, \"%s %s\", szFirst, szSecond);\n\n\nMixing strings and characters\n\nIf you want to add a character to the end of a string you need replace the null character at the end of the string with your new character and add a new null character following your string. You should first check your target string has enough space to hold the new character.\n\n// By hand\nchar c = 'A';\nchar szString[128] = \"The capital of a is \";\nnOldLength = length(szString);\nszString[nOldLength] = c;\nszString[nOldLength+1] = '\\0';\n\n// Using sprintf\nchar c = 'H';\nchar szString[] = \"ello\";\nchar szOutput[128];\nsprintf(szOutput, \"%c%s\", c, szString);\n",
2015-12-08T09:43:10
Magisch :

You are attempting to store multiple chars in a single char variable, that doesnt work. your char c stores a numerical value from either 0 to 255 (unsigned) or -127 to +128 (signed). If you want a string, one thats properly terminated, you have to change your code:\n\nint main(void) {\n\n char c[2];\n\n c[0] = getchar();\n getchar();\n c[1] = '\\0';\n printf(\"%s\\n\", c);\n\n return 0;\n}\n",
2015-12-08T09:10:27
Sumsuddin Shojib :

If your goal is to accept 4 char inputs and print the input given, then just remove c = c + \"/0\"; from you code. It should work",
2015-12-08T09:11:24
Ely :

A char represents only one character. It does not make sense to add/concatenate a char (here c) with a string (here \"\\n\").\n\nIn C a string is delimited with \". A char is delimited with '.\n\nIf you want to do arithmetic with chars you both operands need to be chars.\n\nYou want to return the original input char and therefore simply remove\n\nc = c + \"\\n\";\n",
2015-12-08T09:11:31
Werner Henze :

There is no need to end the input. If you are reading one char, then you can directly output it.\n\nprintf(\"%c\\n\", c); // Note: %c prints one char\n\n\nIf you want to build a string from that char, then you need a terminating null byte.\n\nchar c[2];\nc[0] = getchar();\nc[1] = '\\0';\nprintf(\"%s\\n\", c); // Note: %s prints a string (chars until terminating null byte)\n",
2015-12-08T09:12:55
Jeegar Patel :

first \n\nThe null character is '\\0', not '/0'.\n\nsecond\n\n c = c + \"/0\";\n\n\nIn C as above you can not append strings.\n\n\n\nFor printing it you do not need to convert it in array and use printf\n\nfor printing character you just need \n\nprintf(\"%c\\n\", c); \n",
2015-12-08T09:15:12
yy