---
title: "cString(using:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsstring/cstring(using:)"
---

# cString(using:)

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

## Declaration

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

## Parameters

- `encoding`: The encoding for the returned C string. For possible values, see doc://com.apple.foundation/documentation/Foundation/NSStringEncoding.

## Return Value

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

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:) if it needs to store the C string beyond this time. You can use canBeConverted(to:) to check whether a string can be losslessly converted to encoding. If it can’t, you can use 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:) 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, NSUTF32StringEncoding, or any of their variants are undefined.

## See Also

### Related Documentation

- [canBeConverted(to:)](foundation/nsstring/canbeconverted(to:).md)
- [getCString(_:)](foundation/nsstring/getcstring(_:).md)
- [cStringLength()](foundation/nsstring/cstringlength().md)
- [defaultCStringEncoding](foundation/nsstring/defaultcstringencoding.md)

### Getting C Strings

- [getCString(_:maxLength:encoding:)](foundation/nsstring/getcstring(_:maxlength:encoding:).md)
- [utf8String](foundation/nsstring/utf8string.md)
