what is this number for ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what is this number for ?

#include <stdio.h> int main() { char a[ this number for?]; gets(a); printf("You entered: %s", a); return 0; }

16th Oct 2018, 7:06 PM
moatassim yaboub
moatassim yaboub - avatar
4 Answers
+ 3
char a[x] is array of characters and x determines size of that array try x = 5 and type some longer string, in array will be stored just 5 characters from that string
16th Oct 2018, 7:35 PM
Matúš Semančík
Matúš Semančík - avatar
+ 2
It is probably caused by gets() function, may it use next memory blocks and save whole string, and then ends it with \0 and function printf is printing all characters in memory until \0 (end of string character).
16th Oct 2018, 7:53 PM
Matúš Semančík
Matúš Semančík - avatar
+ 1
I wanted to write the same answer as Matus. Which theoretically is right When I tried to write some sample code it does not work that way. https://code.sololearn.com/cGGIj5G88kg0 Does anybody know why the output is elephant ? and not ele
16th Oct 2018, 7:41 PM
sneeze
sneeze - avatar
0
it is use to declare the length of the string, i.e the number of characters strings will store. for example :- char a[5] = {'R', 'A', 'H', 'U', 'L'}; here it's size is 5 if we define one more characters in then it will provide me some error.
16th Oct 2018, 7:41 PM
Rahul Roy
Rahul Roy - avatar