C – Passing String as Parameter

Passing String as an argument to the function:

  • Like primitive types, we can pass String value as a parameter to the function
  • We collect these values into character type argument variables only.
#include<stdio.h>
void display(char[], int);
int main()
{
            char name[20];
            int age;
            printf(“Enter your name : “);
            gets(name);
            printf(“Enter your age : “);
            scanf(“%d”, &age);
            display(name, age);
            return 0;
}
void display(char name[], int age)
{
            printf(“Name is : %s \n”, name);
            printf(“Age is : %d \n”, age);
}

Program to display the String character by character:

int main()
{
            char name[20];
            int i;
            printf(“Enter your name : “);
            gets(name);
            i=0;
            while(name[i] != ‘\0’){
                        printf(“%c \n”, name[i]);
                        i++;
            }
            return 0;
}
Scroll to Top