c - scanf by passing char -


eg have following c code:

printf("please input : \r\n" ) ;   char ogn, subs;  scanf("%s %s",  &ogn, &subs);  printf("the 2 values are: %s %s", &ogn, &subs); 

when running code, eg input "abc def" , exam ogn,subs,

i ogn = "ef" , subs = "def";

can please explain me ? know in 'string' case 'char array' should provided here want know why 'char' variable accepting user input causes such result ?

th result undefined behaviour.

your program packs memory so:

subs | ogn | m | m | m | ...

where m random memory program allocates (don't ask me why it's there if wasn't there would've segfaulted.)

now scanf loads "abc" ogn memory looks this

subs | 'a' | 'b' | 'c' | '\0' | ...

now scanf loads "def" subs :

'd' | 'e' | 'f' | '\0' | '\0' | ..

now tell print string ogn. until first terminating '\0':

"def"

now tell print string sub until '\0':

"ef"

and why results doing, on different compiler might different result.

don't in real program.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -