home · Posts · Archive · Tags

20180124-gets-puts

#心得 #緩衝區溢位 #gets_puts #string

語法:

char *gets(char *string);

int puts(const char *string);

--------我是分隔線--------

其實puts(string),就是fputs(string, stdout)更簡潔的語法。

但是fgets(string, 128, stdin),不等於gets,因為gets沒有參數可以限制讀進string的字元數,因此在現在的compiler會建議不要使用gets函式,因為可能會發生buffer overrun(緩衝區溢位)的問題。

另外,C語言中也有一些標準函式庫裡函式的使用也需要特別注意,如:gets, scanf, strtok, strcpy。

然後後來我找到一篇講解概念的:

https://medium.com/.../%E7%B7%A9%E8%A1%9D%E5%8D%80%E6%BA...

https://zh.wikipedia.org/.../C%E6%A8%99%E6%BA%96%E5%87%BD...

Ref

👈Go Back

@alanhc