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

# cString()

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

## Declaration

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

## Discussion

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(_:) 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:) if necessary to check whether a string can be losslessly converted to the default C-string encoding. If it can’t, use lossyCString() or data(using:allowLossyConversion:) to get a C-string representation with some loss of information.

## See Also

### Related Documentation

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

### Deprecated

- [string(withCString:)](foundation/nsstring/string(withcstring:).md)
- [init(CString:)](foundation/nsstring/init(cstring:)-vkuo.md)
- [string(withCString:length:)](foundation/nsstring/string(withcstring:length:).md)
- [init(CString:length:)](foundation/nsstring/init(cstring:length:)-5ure3.md)
- [init(CStringNoCopy:length:freeWhenDone:)](foundation/nsstring/init(cstringnocopy:length:freewhendone:)-86dm2.md)
- [string(withContentsOfFile:)](foundation/nsstring/string(withcontentsoffile:).md)
- [init(contentsOfFile:)](foundation/nsstring/init(contentsoffile:).md)
- [string(withContentsOf:)](foundation/nsstring/string(withcontentsof:).md)
- [init(contentsOfURL:)](foundation/nsstring/init(contentsofurl:).md)
- [write(toFile:atomically:)](foundation/nsstring/write(tofile:atomically:).md)
- [write(to:atomically:)](foundation/nsstring/write(to:atomically:).md)
- [getCharacters(_:)](foundation/nsstring/getcharacters(_:).md)
- [lossyCString()](foundation/nsstring/lossycstring().md)
- [cStringLength()](foundation/nsstring/cstringlength().md)
- [getCString(_:)](foundation/nsstring/getcstring(_:).md)
