Code Snippets C/C++ Code Snippets

C program to get a string from user define function.

By: IncludeHelp, On 04 OCT 2016

In this program we are going to learn how we can get a string from a user define function?

This example will return a constant string value to the calling function main, here we are creating a function getString() which has const char* return type; it means value that will return from the string should be const char* (A constant character array/ constant string).

In calling function (main) we will assign this value to the const char* (A constant character pointer) and print.

C program (Code Snippet) – To get String from a function

Let’s consider the following example:

/*C program to get a string from user define function.*/

#include <stdio.h>

//function which will return a string
const char* getString(void){
	const char *str="Hello World - String value.";
	return str;
}

int main(){
	const char *myString=NULL;
	
	myString= getString();
	
	printf("String is: %s\n",myString);
	return 0;	
}

Output

            String is: Hello World - String value.       
        




Copyright © 2024 www.includehelp.com. All rights reserved.