How to put a whole word inside a char?

Asked

Viewed 1,680 times

0

How do I place a word with letters and numbers inside a char? For example put on it the license plate of an ABC-1234 car.

char a = ABC1234;
char b = DFG0000;   

printf("1ª placa %s\n",a);  
printf("2ª placa %s\n",b);  

Error in code. How I proceed?

  • blz I’ll change the question

  • From what you’re saying, it seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

3 answers

4

Do you know the difference between character and string? The type char is a character, so it only accepts scalar information. You want vector information, you should use a string for this case. In this case, in C, this would be something like char placa_a[] = "ABC0123";

Also taking advantage of the parameter %s of printf does not expect to receive a scalar variable, but a character pointer. You’ll get strange results if your application doesn’t crudely close with a segmentation failure for the use you gave.


Another point: constant strings in C are declared in quotes. Therefore, if I want to initialize the variable called placa_a with a word, I need to do:

placa_a = "ABC1234";

This initialization can be done for the definition of placa_a as a pointer as well as being a vector.

So the options would be:

// inicializando um vetor
char placa_a_vetor[] = "ABC0123";
// inicializando o ponteiro junto da declaração 
char *placa_a_ptr = "ABC0123";

// primeiro declarando, depois inicializando
char *placa_a_ptr_delayed;
placa_a_ptr_delayed = "ABC0123";

Recommended reading:

  • thank you so much for your help

1

Dude basically you can’t do that with a char "normal", a variable of this type is made to store only one character, so you can store a string (string) in a variable it is necessary that you use a string, this would be equivalent to an array (array) of type char in C.

To declare a string is very easy, you first need to keep in mind how many letters the word you want to store has, after that just declare the variable equal you would declare an array, only using char of course, it is very important to note that at the end of all the string will characterize you ' 0', this symbolizes that your string has reached the end, just when you count how many letters the word you want to store has, to be able to create the string, remember to always add an al to the final result, this will be reserved to ' 0'.

ex:

#include <stdio.h>

int main ()
{
    char strNumPlaca[50] = "ABC - 03257";

    printf("%s", strNumPlaca); /*   use %s ao invés de %c, pois isso simboliza
                                    que você esta utilizando uma string.    */

    return 0;
}

To help in your understanding here is a link: http://linguagemc.com.br/string-em-c-vetor-de-caracteres/

  • You wouldn’t need to declare the string size strNumPlaca for that particular case. The C compiler will automatically adjust it for you so that it is 11 in size (3 letters, 4 numbers, 3 formatting characters and the null terminator). Of course, if it is the idea to reuse this string in a context that requires 49 characters and the terminator makes sense to declare the size...

  • 1

    ss, but I thought it would get too much to explain to him, so at first it was enough

0

On your question have 2 points to understand, come on. 1º For the declaration of a char with some characters, be they special, numbers, what you find best, it is necessary to use keys [] and a number, or variable (if they are N values, and if the value of N has already been chosen), it varies according to the need, and still need to put your characters inside double quotes, so char word[3] = "yes";. 2º If you want to concatenate a string, we use the strcat function, which works with the string.h. I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.