---
title: "Define and write syntax of gets()"
subject: "Computer Programming"
chapter: 4
key: "semester_1/computer_programming:syntax-gets"
source: https://www.bctengineer.com/q/semester_1/computer_programming/syntax-gets
site: BCT Engineering
---

# Define and write syntax of gets()

Computer Programming · Chapter 4: Input and Output

- Asked in: 2072 Chaitra, 1 marks
- Video: [Reading Strings using scanf and gets Functions](https://www.youtube.com/watch?v=2HasEQe5VR0) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/syntax-gets
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-4

## Question

Define and write syntax of gets().

## Short answer

gets(str) reads a whole line including spaces from stdin into str, replacing newline with '\0'. It is unsafe because it lacks bounds checking and has been removed from modern C standards; use fgets instead.

## Answer

`gets()` reads a whole line (a string, including spaces) from the keyboard into a character array and replaces the new line with `'\0'`. It is declared in `<stdio.h>`.

**Syntax:** `gets(string_variable);`

```c
#include <stdio.h>
int main(void)
{
    char name[30];
    printf("Enter your full name: ");
    gets(name);
    printf("Hello, %s\n", name);
    return 0;
}
```
Output (for the input `Ram Bahadur Thapa`):
```text
Enter your full name: Hello, Ram Bahadur Thapa
```

`gets()` does not check the array size, so a long input overflows the array. It was removed in C11, and gcc warns that it is deprecated. The safe alternative is `fgets(name, sizeof(name), stdin);`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. gets() reads:
   A. Only one char
   B. A whole line including spaces
   C. Only a number
   D. A file
   Answer: B. gets reads a line into a char array. (Key point: What gets does)

2. What replaces the new line?
   A. '\n'
   B. ' '
   C. '\0'
   D. EOF
   Answer: C. The new line is replaced by '\0'. (Key point: Terminator)

3. Why is gets() unsafe?
   A. It is slow
   B. It does not check array size so input can overflow
   C. It needs &
   D. It echoes
   Answer: B. No size check; removed in C11. Use fgets. (Key point: Danger)
