<!--
{
  "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/cString()",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)cString"
  },
  "title" : "cString()"
}
-->

# cString()

Returns a representation of the receiver as a C string in the default C-string encoding.

```
func cString() -> UnsafePointer<CChar>?
```

## Discussion

The returned C string will be automatically freed just as a returned object would be released; your code should copy the C string or use [`getCString(_:)`](/documentation/Foundation/NSString/getCString(_:)) if it needs to store the C string outside of the autorelease context in which the C string is created.

Raises an `NSCharacterConversionException` if the receiver can’t be represented in the default C-string encoding without loss of information. Use [`canBeConverted(to:)`](/documentation/Foundation/NSString/canBeConverted(to:)) if necessary 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()) or [`data(using:allowLossyConversion:)`](/documentation/Foundation/NSString/data(using:allowLossyConversion:)) to get a C-string representation with some loss of information.

## See Also

[`-  cStringUsingEncoding:`](/documentation/Foundation/NSString/cString(using:))

Returns a representation of the string as a C string using a given encoding.

[`UTF8String`](/documentation/Foundation/NSString/utf8String)

A null-terminated UTF8 representation of the string.

[`-  getCString:maxLength:encoding:`](/documentation/Foundation/NSString/getCString(_:maxLength:encoding:))

Converts the string to a given encoding and stores it in a buffer.



---

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)