user3493439
user3493439

Reputation: 9

How to insert NULL at the end of string in c

I am talking input using gets() function. So, I want to check where string ends. But NULL is not inserted at the end of the string as it is inserted by using scanf(). So how can I do this?

Upvotes: 0

Views: 2956

Answers (1)

John Zwinck
John Zwinck

Reputation: 249582

gets() absolutely does insert a null terminator. However, please note that gets() is deprecated and should not be used at all. Use fgets() instead, as it avoids buffer overrun vulnerabilities.

To use fgets on standard input:

char buffer[256]
fgets(buffer, sizeof(buffer), stdin);

Upvotes: 8

Related Questions