Question

I'm using Pelles C ide and I'm learning C, I thought I learned that %c was the format specifier for char in printf(), but it thinks I'm trying to use int? How can I correct this problem.

Here is the code:

#include <stdio.h>

int main(void)
{
char z[] = "       *\n";
char m[] = "      *\n";
char k[] = "     *\n";
char o[] = "*   *\n";
char l[] = " * *\n";
char e[] = "  * ";



printf(" %c", z);
printf(" %c", m);
printf(" %c", k);
printf(" %c", o);
printf(" %c", l);
printf(" %c", e);



return 0;

}

Here is the errors: enter image description here

Was it helpful?

Solution

You should be using a string specifier to output character strings.

printf("%s", z);

OTHER TIPS

%c means that the int argument is converted to an unsigned char, and the resulting character is written. For other types than int (including types promoted to int), the behavior is undefined.

For printing strings, use %s:

printf(" %s", z);

printf(" %c", z); z is a string no a char (string is an array of chars) to I think you're trying to do this printf(" %c", '\n'); or this printf(" %s", z); (%s is the proper format specifier to output strings)

Your variable z by definition is a pointer to a char array.

try

printf(" %c", *z);

to print the first character pointed to by z.

If you want to print all of the characters, treat it as a string (pointer to array of characters with a terminating NUL character '\0'. The NUL character is added automatically by the fact that your string is enclosed in double quotes.

try

printf(" %s", z);

A 'char' is passed as an 'int' when you call a function. But you're passing a pointer to 'char', and that is not the same thing at all. The error message is complaining that it's seeing a pointer. In your case, since you want to print a string rather than a single 'char', your format is wrong; use %s instead.

Actually if you want %c, define the variables as character constants:

char c = ' ';
char d = '\n';

and so on and so forth.

When you create an array, then the name of the array variable (as in your code z,m,k,o,l,e) actually holds the base address of the array (that is the address of the 0th index of your array). So, when you try to print the value of z,m,k,o,l,e, they will return the base address of the respective array. You can check this by the following code :

#include <stdio.h>

int main(void)
{
char z[] = "       *\n";
char m[] = "      *\n";
char k[] = "     *\n";
char o[] = "*   *\n";
char l[] = " * *\n";
char e[] = "  * ";



printf(" %d", z);
printf(" %d", m);
printf(" %d", k);
printf(" %d", o);
printf(" %d", l);
printf(" %d", e);



return 0;
}

The above code will not show any error and will print the base address of the respective array.

To make it more clear, you can try the following code :

#include <stdio.h>

int main(void)
{
char z[] = "       *\n";
char m[] = "      *\n";
char k[] = "     *\n";
char o[] = "*   *\n";
char l[] = " * *\n";
char e[] = "  * ";



printf(" %c", &z);
printf(" %c", &m);
printf(" %c", &k);
printf(" %c", &o);
printf(" %c", &l);
printf(" %c", &e);



return 0;
}

The above code again will not show any error and will print the 1st character of your respective arrays. I hope it makes you clear that why your code is showing error.

Now, when you use %s and the array name, then it will take the base address of your array and will print the whole string from that address till it finds null as follows. :

#include <stdio.h>

int main(void)
{
char z[] = "       *\n";
char m[] = "      *\n";
char k[] = "     *\n";
char o[] = "*   *\n";
char l[] = " * *\n";
char e[] = "  * ";



printf(" %s", z);
printf(" %s", m);
printf(" %s", k);
printf(" %s", o);
printf(" %s", l);
printf(" %s", e);



return 0;
}

And, if you want to print a particular character at a particular index of your char array, then you can try with the following code :

#include

int main(void)
{
char z[] = "       *\n";
char m[] = "      *\n";
char k[] = "     *\n";
char o[] = "*   *\n";
char l[] = " * *\n";
char e[] = "  * ";



printf(" %c", z[indexValue]);
printf(" %c", m[indexValue]);
printf(" %c", k[indexValue]);
printf(" %c", o[indexValue]);
printf(" %c", l[indexValue]);
printf(" %c", e[indexValue]);



return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top