Program in c showing output, strange different from the value of the variables

0

I have a problem executing a program in ec and when I compile it and execute it it shows me a value that is not defined in my variable I'm thinking it's some bug with ide or something related to the buffer I'm going to leave a screenshot for you to see

the code:

theoutput:

    
asked by anonymous 26.08.2017 / 14:39

1 answer

1

This error occurs because you use a string (text) and used only one char (character). In C language a string is represented by a character vector ending with a null character '\ 0'. But you just put char nick . The correct would be char nick ;

When declaring char the compiler will allocate only 1byte of memory capable of storing a character.

Howeverafterputtingthischar[100]youwillgetanothererror,checkout:

Thatis,itisnotpossibletoassignastringfieldofastructtoaconstantstring"Assanges". To work you must use the function of (string copy) or strcpy (destination, source). So put: strcpy (user.nick, "Assanges"); and it will work!

Remember to include string.h

Check out:

Seebelowthecorrectcode

#include<stdio.h>#include<stdlib.h>#include<string.h>structUser{charnick[100];intid;}user;intmain(){strcpy(user.nick,"Assanges");
   user.id = 45474;
   printf("\nNick:%s\nId:%d \n", user.nick, user.id);

return 0;
}

Now when running it will work!

    
26.08.2017 / 18:49