<!--
{
  "availability" : [
    "iOS: 2.0.0 - 2.0.0",
    "iPadOS: 2.0.0 - 2.0.0",
    "tvOS: 9.0.0 - 9.0.0",
    "visionOS: 1.0.0 - 1.0.0",
    "watchOS: 2.0.0 - 2.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSString/cStringLength()",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)cStringLength"
  },
  "title" : "cStringLength()"
}
-->

# cStringLength()

Returns the length in char-sized units of the receiver’s C-string representation in the default C-string encoding.

```
func cStringLength() -> Int
```

## Discussion

Raises if the receiver can’t be represented in the default C-string encoding without loss of information. You can also use [`canBeConverted(to:)`](/documentation/Foundation/NSString/canBeConverted(to:)) to check whether a string can be losslessly converted to the default C-string encoding. If it can’t, use [`lossyCString()`](/documentation/Foundation/NSString/lossyCString()) to get a C-string representation with some loss of information, then check its length explicitly using the ANSI function `strlen()`.

## See Also

[`var utf8String: UnsafePointer<CChar>?`](/documentation/Foundation/NSString/utf8String)

A null-terminated UTF8 representation of the string.

[`func maximumLengthOfBytes(using: UInt) -> Int`](/documentation/Foundation/NSString/maximumLengthOfBytes(using:))

Returns the maximum number of bytes needed to store the receiver in a given encoding.

[`func lengthOfBytes(using: UInt) -> Int`](/documentation/Foundation/NSString/lengthOfBytes(using:))

Returns the number of bytes required to store the receiver in a given encoding.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)