-1
int main() {

    char letra = 'B';
    int numero = 10;
    float virgulas = 10.5;
    char casa = 'Esquina';

    printf("%c \n",letra);
    printf("%d \n",numero);
    printf("%f \n",virgulas);

    printf("Letra %c, numero %d, em uma %c\n", letra, numero, casa);

    return 0;
}

When I run my code, "Esquina" appears as a random number every time.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Rodrigo H
  • 7
  • 2
  • 1
    `char casa = "Esquina"` should cause the compiler to issue a warning. It doesn't do what you (apparently) think it does. – Some programmer dude Jun 09 '17 at 18:03
  • 1
    `"Esquina"` is a string not a character, you will need to store it in a `char*` and then print it with `%s` – Fanarosss Jun 09 '17 at 18:04
  • 1
    I recommend you read a tutorial on data types, `char` can only hold 1 character – Joshua Jun 09 '17 at 18:04
  • @Fanarosss so %c is only for single words, and %s for multiple words? – Rodrigo H Jun 09 '17 at 18:05
  • @RodrigoH %c is for single characters – Fanarosss Jun 09 '17 at 18:07
  • `char casa = 'Esquina';` is flawed — you can only store a single character in a single character variable. What you get is implementation defined. Multiple character literals are permitted, but the standard doesn't say how they're interpreted. When you print a single character with `%c`, you geta single character output. You probably need `const char *casa = "Esquina";` or `char casa[] = "Esquina";` (we don't have enough information to choose between them) and use `%s` in the format string. And read your C book to work out why. – Jonathan Leffler Jun 09 '17 at 18:07
  • You should probably [find a good beginners book to read](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) instead of asking questions here about really basic stuff. – Some programmer dude Jun 09 '17 at 18:10

2 Answers2

3

"Esquina" is a string. You need to store it in a char* (not a char - most modern compilers should issue an error on this mistake):

char* casa = "Esquina";

You can then print it out with the %s formatter (not %c):

printf("Letra %c, numero %d, em uma %s\n", letra, numero, casa);
/* Here -----------------------------^ */
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Shouldn't he `malloc` memory for the pointer first and then assign `Esquina` with `strcpy` ?? – Fanarosss Jun 09 '17 at 18:08
  • 1
    @Fanarosss: No, not really. You could do that; there is no need to do that (and it is unnecessarily complicated for this example code). You could use `char casa[] = "Eqsuina";` too — it would work. There's not enough information to choose between them. – Jonathan Leffler Jun 09 '17 at 18:09
  • Note that `char casa = 'Esquina';` might generate a warning but is not formally an error. The result is implementation defined — the implementation might define that it is an error, but isn't obliged to do so. – Jonathan Leffler Jun 09 '17 at 18:11
  • I tried what you said and the result is very weird `B 10 10.500000 Letra B, numero 10, em uma #9Zýï├¢³®¤Ó6>N²eìK¦æ kE┬¸í#çjf&il╣³┘o]õ╣ëäç?£=¸ÏÊBw>¤|­ËH¨½be\? Ì»╬¹P3■míÖù<▄dÙ%ù·_yÐ&R┘Þ‗íô Ô{■¹` @Mureinik – Rodrigo H Jun 09 '17 at 18:11
  • @RodrigoH I tried the recommended changes and the last line of the output is `Letra B, numero 10, em uma Esquina`. – Weather Vane Jun 09 '17 at 18:15
  • It's working now, thanks guys. I am reading a book for beginners, and it was very helpful this topic for me. – Rodrigo H Jun 09 '17 at 18:20
2

First, some basics:

In C, a string is a sequence of character values followed by a 0-valued terminator. Strings are stored in arrays of char, not single char objects.

A string literal is delimited by double quotes ("), while character constants are delimited by single quotes (').

In your code

char casa = 'Esquina';

you declare casa as a char, meaning it can only store a single character value, not a string. You initialize it with 'Esquina', which is not a string literal, but a multibyte character constant. Multibyte character constants are mapped on to char objects in an implementation-defined manner. In this particular case, it's not what you want.

casa should be declared as either

const char *casa = "Esquina";

or

char casa[] = "Esquina";

depending on whether or not you will need to modify the contents of the string later on. The first version declares casa as a pointer to the first character in the string literal "Esquina"1. The second version declares casa as a 8-element array of char2 and copies the contents of the string literal to it, giving us

casa[0] == 'E'
casa[1] == 's'
casa[2] == 'q'
casa[3] == 'u'
casa[4] == 'i'
casa[5] == 'n'
casa[6] == 'a'
casa[7] == 0

To print it out, you'd use the %s conversion specifier in printf:

printf("Letra %c, numero %d, em uma %s\n", letra, numero, casa);


  1. String literals are stored as arrays of char such that the array is allocated when the program starts up and released when the program terminates. Attempting to modify the contents of a string literal invokes undefined behavior - it may work, it may cause a segfault, it may do something unexpected. To prevent us from accidentally modifying the string literal through casa, we declare it as const char *; this way, any attempt to change the string will cause the compiler to yak.
  2. The size is taken from the length of the string, plus an additional element to store the string terminator.

John Bode
  • 119,563
  • 19
  • 122
  • 198