Program in c presenting output, strange different from the value of variables
I'm having a problem running a program in E c that when I compile it and run it shows me a value that is not defined in my variable I'm thinking that it is some bug with the ide or something related to the buffer I'll leave a screenshot for you to see
Or Code:
To exit:
1 answers
This error occurs because, you that use a string (text) and used only a char(character). In the C language a string is represented by a NULL-terminated character vector '\0'. But you put only as Char nick . The correct would be for example Char nick[100];
When declaring char the compiler will allocate only 1byte of memory capable of storing a character.
However after putting this char[100] you you will get another error, check:
That is, it is not possible to assign a string field of a struct to a string constant "Assanges". To work you must use the (string copy) function or strcpy (destination, source). So put: strcpy (user.nick, "Assanges"); and it will work!
Remember to do include of string.h
Check out:
See below for the code correct
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct User{
char nick[100];
int id;
}user;
int main(){
strcpy(user.nick, "Assanges");
user.id = 45474;
printf("\nNick:%s\nId:%d \n", user.nick, user.id);
return 0;
}
Now running will work!