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

# Define and write syntax of strlen()

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2072 Chaitra, 1 marks
- Video: [String Library Functions strcpy() and strlen ()](https://www.youtube.com/watch?v=fYQG_NORXDY) — Code to Career
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/syntax-strlen
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Define and write syntax of strlen().

## Short answer

strlen() from <string.h> returns the number of characters in a string, excluding the terminating null character. Its syntax is: size_t strlen(const char *str);

## Answer

`strlen()` is a library function of `<string.h>` that returns the number of characters in a string, not counting the terminating null character `'\0'`.

**Syntax:** `size_t strlen(const char *str);`

Example: `strlen("Nepal")` returns 5.

```c
#include <stdio.h>
#include <string.h>

int main(void)
{
    char s[] = "Nepal";
    size_t n = strlen(s);
    printf("Length of \"%s\" = %zu\n", s, n);
    return 0;
}
```

Output: `Length of "Nepal" = 5`

## Check yourself

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

1. In which header is strlen() declared?
   A. <stdio.h>
   B. <ctype.h>
   C. <stdlib.h>
   D. <string.h>
   Answer: D. strlen is a string-handling function of string.h. (Key point: Header file)

2. What does strlen("Nepal") return?
   A. 6
   B. 5
   C. 4
   D. 0
   Answer: B. It counts the characters excluding the terminating '\0'. (Key point: What it returns)

3. Which is the correct syntax of strlen?
   A. int strlen(char a, char b);
   B. char strlen(int n);
   C. void strlen(char *str, int n);
   D. size_t strlen(const char *str);
   Answer: D. It takes a string pointer and returns its length as size_t. (Key point: Syntax)
