so my question is why do some people use int variables as char variables. exemple
int main()
{
int i;
scanf("%c",&i);
printf ("%c",i);
}
thank's in advance
C's char is an integer type. Its (numeric) values are most often used to represent characters, but that's a separate matter.
You can safely use a variable of type int to hold a value in the range of type char. You can assign a char to an int or pass a char to a function expecting an int, both without a cast and without altering the value.
In certain places, int is used intentionally instead of char to represent characters. The canonical case is probably the standard library's getc(), fgetc(), and getchar() functions, which need the range of an int to be able to represent EOF in addition to every possible char value. Also, for historical reasons, some other functions declare int arguments to accept data expected to be of type char; memset() is one of the better known of these.
On the other hand, pointers to int and to char are not interchangeable. As others pointed out, your scanf() call produces undefined behavior for that reason.
Generally speaking, you should use char rather than int to represent character data, unless there is a good, externally-driven reason to do otherwise (such as needing to handle the return value of getchar()). Even more generally speaking, match data types correctly, being deliberate about where you allow type conversions to be performed.
This -
scanf("%c",&i);
Wrong argument is passed (%c expects address of char ).It invokes undefined behaviour .
-Wall -Werror and see what you get .char is guaranteed to be less or equal to int (mostly less), why is it UB? You'll get a warning, but you'll have more than enough memory space to store it. It's wrong from the scanf point of view, which is waiting a char * and is getting an int *.scanf only . Wrong argument if passed will cause it .printf() the matter is open to interpretation, but see this answer and similar questions and answers for a discussion of some of the issues.
scanf("%c",&i);is wrong. Soprintf ("%c",i);Portable results can not be obtained.-Werrorflag set.