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

# cString(using:)

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

```
func cString(using encoding: UInt) -> UnsafePointer<CChar>?
```

## Parameters

`encoding`

The encoding for the returned C string. For possible values, see [`NSStringEncoding`](/documentation/Foundation/NSStringEncoding).

## Return Value

A C string representation of the receiver using the encoding specified by `encoding`. Returns `NULL` if the receiver cannot be losslessly converted to `encoding`.

## Discussion

The returned C string is guaranteed to be valid only until either the receiver is freed, or until the current memory is emptied, whichever occurs first. You should copy the C string or use [`getCString(_:maxLength:encoding:)`](/documentation/Foundation/NSString/getCString(_:maxLength:encoding:)) if it needs to store the C string beyond this time.

You can use [`canBeConverted(to:)`](/documentation/Foundation/NSString/canBeConverted(to:)) to check whether a string can be losslessly converted to `encoding`. If it can’t, you can use [`data(using:allowLossyConversion:)`](/documentation/Foundation/NSString/data(using:allowLossyConversion:)) to get a C-string representation using `encoding`, allowing some loss of information (note that the data returned by [`data(using:allowLossyConversion:)`](/documentation/Foundation/NSString/data(using:allowLossyConversion:)) is not a strict C-string since it does not have a `NULL` terminator).

### Special Considerations

UTF-16 and UTF-32 are not considered to be C string encodings, and should not be used with this method—the results of passing [`NSUTF16StringEncoding`](/documentation/Foundation/NSUTF16StringEncoding), [`NSUTF32StringEncoding`](/documentation/Foundation/NSUTF32StringEncoding), or any of their variants are undefined.

## See Also

[`canBeConverted(to:)`](/documentation/Foundation/NSString/canBeConverted(to:))

Returns a Boolean value that indicates whether the receiver can be converted to a given encoding without loss of information.

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

Invokes [`getCString(_:maxLength:range:remaining:)`](/documentation/Foundation/NSString/getCString(_:maxLength:range:remaining:)) with `NSMaximumStringLength` as the maximum length, the receiver’s entire extent as the range, and `NULL` for the remaining range.

[`cStringLength()`](/documentation/Foundation/NSString/cStringLength())

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

[`defaultCStringEncoding`](/documentation/Foundation/NSString/defaultCStringEncoding)

Returns the C-string encoding assumed for any method accepting a C string as an argument.



---

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)