---
title: "scanf with %s and no ampersand"
subject: "Computer Programming"
chapter: 4
key: "semester_1/computer_programming:scanf-string-no-ampersand"
source: https://www.bctengineer.com/q/semester_1/computer_programming/scanf-string-no-ampersand
site: BCT Engineering
---

# scanf with %s and no ampersand

Computer Programming · Chapter 4: Input and Output

- Asked in: 2069 Asar, 4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/scanf-string-no-ampersand
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-4

## Question

Consider a statement
```
scanf("%s",str);
```
Where str is a string variable. In above statement, why '&' symbol is not used? Can we input string with space in this statement? If not, why?

## Short answer

In scanf, the array name str is already the address of its first element, so & is unnecessary. The %s specifier stops reading at whitespace, so strings with spaces cannot be entered; use %[^\n] or fgets instead.

## Answer

#### Why `&` is not used
In `scanf("%s", str)`, `str` is the name of a character array, and the name of an array is already the address of its first element. `scanf` needs the address of the place where the value is to be stored, so the array name alone is enough. Writing `&str` is not needed (it has the same address but a different type). For ordinary variables like `int n`, the address must be given explicitly as `&n`.

#### Can a string with spaces be entered?
No. `%s` reads characters until the first white-space (space, tab or new line) and stores only that word. The rest stays in the input buffer.

```c
#include <stdio.h>
int main(void)
{
    char str[40];
    printf("Enter a string: ");
    scanf("%s", str);          /* no & : str is already an address */
    printf("Stored: %s\n", str);
    return 0;
}
```
Input `Kathmandu Valley` gives:
```text
Enter a string: Stored: Kathmandu
```

To read a string with spaces use a scan set `%[^\n]`, or `fgets`:
```c
#include <stdio.h>
int main(void)
{
    char str[40];
    scanf("%39[^\n]", str);    /* reads a line including spaces */
    printf("Stored: %s\n", str);
    return 0;
}
```
Output for the same input:
```text
Stored: Kathmandu Valley
```

## Check yourself

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

1. Why is & not used in scanf("%s", str)?
   A. Strings cannot be read
   B. The array name is already the address of its first element
   C. & is only for floats
   D. scanf ignores strings
   Answer: B. An array name gives the address of the first element. (Key point: Why no &)

2. Can scanf("%s") read Kathmandu Valley fully?
   A. Yes
   B. No, it stops at the first white space
   C. Only with &
   D. Only 40 characters
   Answer: B. %s stops at the first white space, storing only Kathmandu. (Key point: Spaces)

3. What happens to the rest of the line?
   A. It is lost
   B. It stays in the input buffer
   C. It is stored in the next array
   D. It causes error
   Answer: B. The rest remains in the buffer. (Key point: Remaining input)

4. How can a line with spaces be read?
   A. %s with &
   B. %[^\n] scan set or fgets
   C. %c
   D. %d
   Answer: B. Use %[^\n] or fgets. (Key point: Reading a line)

## Related in Computer Programming

- [Insert a string into another string](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Ainsert-string-into-string) — Computer Programming, chapter 7
- [Employees with salary above 10,000](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-employee-salary-above-10000) — Computer Programming, chapter 8
- [Nested if with flowchart](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Anested-if-flowchart) — Computer Programming, chapter 5
- [Conditional operator vs if-else; digit sum](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aconditional-operator-vs-ifelse) — Computer Programming, chapter 5
- [Switch statement in decision making](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-decision-making) — Computer Programming, chapter 5
- [Syntax of if...else](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-if-else) — Computer Programming, chapter 5

## Related in other subjects

- [Space switch and why TST is used](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3Aspace-switch-why-tst) — Telecommunications, chapter 4
- [RA: employee-projects with no part used](https://www.bctengineer.com/question.md?key=semester_6%2Fdatabase_management_system%3Ara-emp-project-no-part) — Database Management System, chapter 3
- [Random process; LTI output is WSS if input is WSS](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Arandom-process-lti-wssp) — Communication Systems, chapter 9
- [Voltage across string discs is not uniform](https://www.bctengineer.com/question.md?key=semester_4%2Fpower_system_analysis_i%3Astring-voltage-not-uniform) — Power System Analysis I, chapter 2, asked 3×
