How to return a string from a C function
Learn how to return a string from a C function
In one of my C programs, I had the task to return a string from a function:
xxxxx myName() {
return "Flavio";
}
The tricky thing is defining the return value type.
Strings in C are arrays of char elements, so we can’t really return a string - we must return a pointer to the first element of the string.
This is why we need to use const char*:
const char* myName() {
return "Flavio";
}
Here’s an example working program:
#include <stdio.h>
const char* myName() {
return "Flavio";
}
int main(void) {
printf("%s", myName());
}
If you prefer to assign the result to a variable, you can invoke the function like this:
const char* name = myName();
We can write the pointer operator * also in this ways:
const char * myName()
const char *myName()
All forms are perfectly valid.
Note the use of const, because from the function I’m returning a string literal, a string defined in double quotes, which is a constant.
Note that you can’t modify a string literal in C.
Another thing to keep in mind is that you can’t return a string defined as a local variable from a C function, because the variable will be automatically destroyed (released) when the function finished execution, and as such it will not be available, if you for example try do do:
#include <stdio.h>
const char* myName() {
char name[6] = "Flavio";
return name;
}
int main(void) {
printf("%s", myName());
}
you’ll have a warning, and some jibberish, like this:
hello.c:5:10: warning: address of stack memory
associated with local variable 'name'
returned [-Wreturn-stack-address]
return name;
^~~~
1 warning generated.
B��Z> K���⏎
Notice the B��Z> K���⏎ line at the end, which indicates that the memory that was first taken by the string now has been cleared and there’s other random data in that memory. So we don’t get the original string back.
Changing myName() to
const char* myName() {
char *name = "Flavio";
return name;
}
makes the program work fine.
The reason is that variables are allocated on the stack, by default. But declaring a pointer, the value the pointers points to is allocated on the heap, and the heap is not cleared when the function ends.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.