Questions tagged [strlen]

A standard C function that returns the length of a string.

This function calculates the length of the string, excluding the terminating null byte \0. It is defined in the <string.h> standard header.

Documentation for C strlen.

692 questions
304
votes
9 answers

Why does glibc's strlen need to be so complicated to run quickly?

I was looking through the strlen code here and I was wondering if the optimizations used in the code are really needed? For example, why wouldn't something like the following work equally good or better? unsigned long strlen(char s[]) { unsigned…
user11954200
171
votes
12 answers

Add ... if string is too long PHP

I have a description field in my MySQL database, and I access the database on two different pages, one page I display the whole field, but on the other, I just want to display the first 50 characters. If the string in the description field is less…
mais-oui
  • 2,691
  • 6
  • 19
  • 15
113
votes
18 answers

Will strlen be calculated multiple times if used in a loop condition?

I'm not sure if the following code can cause redundant calculations, or is it compiler-specific? for (int i = 0; i < strlen(ss); ++i) { // blabla } Will strlen() be calculated every time when i increases?
daisy
  • 22,498
  • 29
  • 129
  • 265
90
votes
10 answers

Finding the length of a Character Array in C

What is a way in C that someone could find the length of a character array? I will happily accept pseudo-code, but am not averse to someone writing it out if they'd like to :)
not_l33t
  • 1,315
  • 4
  • 15
  • 17
45
votes
3 answers

Sizeof vs Strlen

#include #include int main(int argc, char *argv[]) { char string[] = "october"; // 7 letters strcpy(string, "september"); // 9 letters printf("the size of %s is %d and the length is %d\n\n", string, …
beparas
  • 1,927
  • 7
  • 24
  • 30
38
votes
6 answers

strlen not checking for NULL

Why is strlen() not checking for NULL? if I do strlen(NULL), the program segmentation faults. Trying to understand the rationale behind it (if any).
hari
  • 9,439
  • 27
  • 76
  • 110
36
votes
5 answers

Unexpected optimization of strlen when aliasing 2-d array

Here is my code: #include #include typedef char BUF[8]; typedef struct { BUF b[23]; } S; S s; int main() { int n; memcpy(&s, "1234567812345678", 17); n = strlen((char *)&s.b) / sizeof(BUF); …
M.M
  • 138,810
  • 21
  • 208
  • 365
36
votes
4 answers

strlen in the C preprocessor?

Is it possible to implement strlen() in the C preprocessor? Given: #define MYSTRING "bob" Is there some preprocessor macro, X, which would let me say: #define MYSTRING_LEN X(MYSTRING)
Joby Taffey
  • 1,129
  • 1
  • 11
  • 17
29
votes
5 answers

Determine #defined string length at compile time

I have a C-program (an Apache module, i.e. the program runs often), which is going to write() a 0-terminated string over a socket, so I need to know its length. The string is #defined as: #define POLICY "\n" \ "
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
28
votes
10 answers

Where is the implementation of strlen() in GCC?

Can anyone point me to the definition of strlen() in GCC? I've been grepping release 4.4.2 for about a half hour now (while Googling like crazy) and I can't seem to find where strlen() is actually implemented.
Chris Tonkinson
  • 13,823
  • 14
  • 58
  • 90
27
votes
1 answer

strlen - the length of the string is sometimes increased by 1

I'm doing some C puzzle questions. In most cases, I am able to find the right answer, but with that one I am having problems. I know the right answer by using the compiler, but I don't know the reason. Have a look at the code: char c[] =…
Prz3m3k
  • 605
  • 6
  • 14
25
votes
8 answers

Is using strlen() in the loop condition slower than just checking for the null character?

I have read that use of strlen is more expensive than such testing like this: We have a string x 100 characters long. I think that for (int i = 0; i < strlen(x); i++) is more expensive than this code: for (int i = 0; x[i] != '\0'; i++) Is it true?…
user466534
25
votes
3 answers

REPNZ SCAS Assembly Instruction Specifics

I am trying to reverse engineer a binary and the following instruction is confusing me, can anyone clarify what exactly this does? =>0x804854e: repnz scas al,BYTE PTR es:[edi] 0x8048550: not ecx Where: EAX: 0x0 ECX: 0xffffffff EDI:…
Michael Scott
  • 539
  • 3
  • 8
  • 18
23
votes
4 answers

strlen() php function giving the wrong length of unicode characters

I am trying to get the length of this unicode characters string $text = 'نام سلطان م'; $length = strlen($text); echo $length; output 20 How it determines the length of unicode characters string?
Munib
  • 3,533
  • 9
  • 29
  • 37
23
votes
3 answers

format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ [-Wformat]

I have searched about this warning and everyone had some mistake in their code, but here is something very unexpected I could not figure out . We do expect strlen(x) to be an integer but what does this warning tell me? How couldn't strlen be int? In…
Optimus Prime
  • 6,817
  • 5
  • 32
  • 60
1
2 3
46 47