C program to pass a string to a function

In this C program, we are going to learn how to pass a string (character array) to a function? Here, we are passing a string to function and printing in the function.
Submitted by IncludeHelp, on March 20, 2018

Given a string and we have to print the string by passing it to the user define function in C.

Here is the function that we have used in the program,

void Strfun(char *ptr)

Here,

  • void is the returns type of the function i.e. it will return nothing.
  • Strfun is the name of the function.
  • char *ptr is the character pointer that will store the base address of the character array (string) which is going to be passed through main() function.

Function calling statement,

Strfun(buff);

Here,

  1. buff is the character array (string).

Program to pass string to a function and print in C

/*  
C program to pass a string to a function
*/

#include <stdio.h>

void Strfun(char *ptr)
{
    printf("The String is : %s",ptr);
}

// main function
int main()
{
	// Declare a buffer of type "char"
	char buff[20]="Hello Function";
	
	// Passing string to Function
	Strfun(buff);
	
	return 0;
}

Output

The String is : Hello Function

C User-defined Functions Programs »






Comments and Discussions!

Load comments ↻






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