Program 47 - CSU1128 - CSE 2026 - Shoolini University

Program to take input of name entered by the user through a function block and output it

Explanation

The program takes the user's name as input through a function and then outputs it.

Header

The code begins with including the standard input-output header file and string manipulation header file using #include <stdio.h> and #include <string.h>.

Function Declaration

The program declares a function getName to get the user's name:

  • void getName(char *name);: Declares the function with a character pointer name as a parameter.

Variable Declaration

The main() function is defined, and a variable is declared:

  • char name[100]: Array to hold the user's name.

Program Description

The program prints a description message for the user to understand its functionality.

Function Call

The program calls the getName function to get the user's name:

  • getName(name);: Calls the function with name as an argument.

Output Result

The program prints the name stored in the array:

  • printf("Your name is: %s\n", name);: Prints the user's name.

Function Definition

The program defines the getName function:

  • void getName(char *name): Defines the function with a character pointer name as a parameter.
  • printf("Enter your full name: ");: Prompts the user to enter their name.
  • fgets(name, 100, stdin);: Reads the name including spaces and stores it in the array name.
  • name[strcspn(name, "\n")] = 0;: Removes the trailing newline character from the string.

Program End

The program ends with return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
#include <string.h>

void getName(char *name);

int main()
{
    printf("\n\n Program to take input of name entered by the user through a function block and output it. \n\n");

    char name[100]; // Declare a string variable to store the name

    getName(name); // Call the function to get the name from the user

    printf("Your name is: %s\n", name); // Print the name back to the user

    return 0;
}
void getName(char *name)
{
    printf("Enter your full name: "); // Prompt the user to enter their name
    fgets(name, 100, stdin);          // Read the name from standard input

    // Remove the trailing newline character from the string
    name[strcspn(name, "\n")] = 0;
}

                

Output

Program to take input of name entered by the user through a function block and output it.

Enter your full name: dmj.one initiative
Your name is: dmj.one initiative