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.       
        




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.