0

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

9
  • 5
    scanf("%c",&i); is wrong. So printf ("%c",i); Portable results can not be obtained. Commented Sep 22, 2015 at 12:50
  • did you even try your code? Online compiler says it is not okay: ideone.com/clSfwc Commented Sep 22, 2015 at 12:52
  • @mch This code compile with warning, if not -Werror flag set. Commented Sep 22, 2015 at 12:59
  • 1
    Always enable compiler warnings! Commented Sep 22, 2015 at 13:15
  • 2
    "but it worked for me" does not mean it will under all circumstances, or even on a second call. Please think about the implications of undefined behaviour Commented Sep 22, 2015 at 13:18

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

This -

scanf("%c",&i);

Wrong argument is passed (%c expects address of char ).It invokes undefined behaviour .

5 Comments

@lol just compile it with flags -Wall -Werror and see what you get .
If 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 *.
@EnzoFerber I said UB in context of scanf only . Wrong argument if passed will cause it .
@lol And of correct output , you know you can get anything if there is UB.
@EnzoFerber, for printf() the matter is open to interpretation, but see this answer and similar questions and answers for a discussion of some of the issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.