5
char name[2];
scanf("%c",name);
printf("%c",name);

I am just starting to learn C. I'm curious about the above code, what I got from the printf output, is not the same with the character I typed in. Rather the output was some funny looking symbol. Can someone explain this to me?

5
  • name is an array, and %c expects a char (really expects an int argument); to print a char, use printf("%c", name[0]);, for example. Commented Aug 6, 2017 at 17:39
  • If you need to take an character array as input you should use scanf("%s",name), printf("%s",name); rather than using the %c . The %c returns the pointer to a character which cannn't be stored in pointer to character array. Commented Aug 6, 2017 at 17:40
  • scanf("%c",name)==>scanf("%s",name) and printf("%c",name)==>printf("%s",name); Commented Aug 6, 2017 at 17:40
  • name is not a char but a char [2]. Change the printf format Commented Aug 6, 2017 at 17:41
  • @sourabh1024 what? %c returns pointer to what? Commented Aug 6, 2017 at 18:04

4 Answers 4

5

For the %c specifier, scanf needs the address of the location into which the character is to be stored, but printf needs the value of the character, not its address. In C, an array decays into a pointer to the first element of the array when referenced. So, the scanf is being passed the address of the first element of the name array, which is where the character will be stored; however, the printf is also being passed the address, which is wrong. The printf should be like this:

printf("%c", name[0]);

Note that the scanf argument is technically ok, it is a little weird to be passing an array, when a pointer to a single character would suffice. It would be better to declare a single character and pass its address explicitly:

char c;
scanf("%c", &c);
printf("%c", c);

On the other hand, if you were trying to read a string instead of a single character, then you should be using %s instead of %c.

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

Comments

3

Either Read a single char

char name[2];
scanf("%c",name);
printf("%c",name[0]);

Or read a string

char name[2];
scanf("%1s",name);
printf("%s",name);

Comments

0

You need %s since because name contains 2 elements. %c is used for single character so if you want the user to input something for e.g. "as"(without "") and the program to print it out you need %s.

char name[2];

scanf(" %s", name);
printf("%s",name);

2 Comments

The space before %s in scanf(" %s", name); is unnecessary. It cleans off any leading whitespace, but that would happen anyway with the %s format. But with the %c format in the question, it often is needed because in that case whitespace is not cleaned off unless you include that space.
@WeatherVane thats just how i learned to program and it stayed with me :D
-2

if you give your input which contains characters less than or equal to two you will get a correct output just as your input if your input contains characters greater than 3 then it doesn't work

2 Comments

to read a string that contains more no of characters just increase the character array to a[300] or a[no of chars in a string]
This answer doesn't address the OP question, nor is it relevant to the behavior of the question. The question concerns about specifiers not input size.

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.