How to put a whole word inside a char?

0

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

char a = ABC1234;
char b = DFG0000;   

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

Give code error. How do I proceed?

    
asked by anonymous 15.10.2017 / 23:36

3 answers

3

Do you know the difference between character and string? The type char is a character, so it only accepts scalar information. You want a vector information, you should use a string for that case. In 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 will get strange results if your application does not roughly close with a segmentation fault for the use you have given.

Another point: strings in C are declared in quotation marks. So if I want to initialize the variable named 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:

15.10.2017 / 23:59
1
Basically you can not do this with a char "normal", a variable of this type is made to store only one character, so for you to store a string in a variable you need to use a string, which would be equivalent to an 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 you only declare the variable equal you would declare a vector, only using char of course, it is very important to point out that at the end of all the string will characterize '\ 0', this symbolizes that your string has reached the end, so when it is to count how many letters the word you want to store has, in order to create the string, remember to always add one year to the final result, which will be reserved for '\ 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: link

    
16.10.2017 / 00:07
0

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

    
20.10.2017 / 00:32