Hello, Danilo! Welcome!
The solution to your problem involves some important concepts of structured C programming.
A variable is a space in memory. It has name, address and can store a value.
When you declare a variable, you must specify the type of variable you want.
-
int
: integer values (-2, 0, 1)
-
float
: floating values (-3.5, 1.0, 5.33)
-
double
: large floating values (3.1415926535897932384626433832795)
-
char
: characters ('a', 'b', 'c')
-
bool
: Boolean values (true, false)
When you want to store a word (string, string ), you must use a vector. Vector is a sequence of spaces in memory. Each space is called the index, which starts from 0 (zero). The zero index is the default address of the vector.
char nome[4];
nome[0] = 'A';
nome[1] = 'N';
nome[2] = 'A';
nome[3] = 'char nome[] = "ANA";
char nome[4] = "ANA";
char nome[4] = {'A','N','A'};
char nome[10] = "ANA";
';
The value '''
'
is used to indicate the end of the string , so it is necessary to reserve a space in the vector.
There are other ways to initialize a string .
int num = 0;
scanf("%d", &num);
printf("numero: %d\n", num);
char nome[10];
scanf("%s", nome);
printf("nome: %s", nome);
Solved characters are assigned between single quotation marks ""
, whereas strings are assigned double quotation marks stdio.h
.
The scanf(arg_1, arg_2)
library has read (keyboard input) printf(arg_1, arg_3)
and write (output on the screen) &num
functions.
Each function has two arguments, but there are 3 types of arguments, in this case.
- arg_1: formatting the value
- arg_2: memory space address
- arg_3: name of memory space
To read or write a value, you must specify the format of that value.
- % d: integers and booleans (1 - true, 0 - false)
- % f: floating
- % fl: large floats
- % c: character
- % s: string
Note that boolean variables return 1 or 0, so they share the same format specifier as integers. For floats, in particular, it is possible to specify the number of houses before and after the point, for example:% 2.3f for numbers 0.333, 25.587, 4.444.
#include <stdio.h>
#include <string.h>
int main(){
//Escolhe entre nome do personagem e classe
char p[20];
char c[20];
printf("RPG teste\n");
printf("Digite um nome para o seu(a) personagem:");
scanf("%s",p);
printf("Escolha uma classe, digitando entre Guerreiro ou Monge:");
scanf("%s",c);
if (strcmp(c,"Guerreiro") == 0){
printf("Guerreiro");
}
else
{
printf("Monge");
}
return 0;
}
Note that to read from the keyboard an integer value, the address entered was nome
("e" followed by the variable name). Already to read a string , the address entered was strcmp(string_1, string_2)
(just the vector name). This is done because the vector address is bound to the vector name itself, pointing to the vector zero index.
Now we are going to solve the problem in question.
To compare if two strings are the same, it is necessary to use the string string.h
function in the strcpy(Nome, "nome de alguém")
library.
char nome[4];
nome[0] = 'A';
nome[1] = 'N';
nome[2] = 'A';
nome[3] = 'char nome[] = "ANA";
char nome[4] = "ANA";
char nome[4] = {'A','N','A'};
char nome[10] = "ANA";
';
Bonus: Another function of the string.h library that may be useful is %code% .