Explanation
The program stores a specified number of names entered by the user in an array and displays them.
Header
The code begins with including the standard input-output header file using #include <stdio.h>
.
Variable Declaration
The main()
function is defined, and several variables are declared:
int n
: Stores the number of names the user wants to provide.char names[n][100]
: Declares a 2D array of characters to store the names. Each name can have up to 99 characters plus the null terminator.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter the number of names and the names themselves:
printf("Enter the number of names you want to provide: ");
: Prompts the user to enter the number of names.scanf("%d", &n);
: Reads the number of names and stores it inn
.char names[n][100];
: Declares a 2D array withn
rows and 100 columns.for (int i = 0; i < n; i++)
: Loop to read each name.printf("Enter full name %d: ", i + 1);
: Prompts the user to enter a name.fgets(names[i], sizeof(names[i]), stdin);
: Reads each name including spaces and stores it in the array.
Display Names
The program prints the names stored in the array:
for (int i = 0; i < n; i++)
: Loop to print each name.printf("Full name %d: %s", i + 1, names[i]);
: Prints each name with its corresponding number.
Program End
The program ends with return 0;
to indicate successful execution.
Note
There is a potential issue with reading the names due to the leftover newline character from the previous scanf
call. Find a way to fix this! You can add getchar();
after scanf
to consume the newline character but will it help and work in vscode? Thats for you to decide!
scanf("%d", &n);
getchar(); // Consume the newline character
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>
int main()
{
printf("\n\n Program to store 5 names entered by the user in an array and display them. \n\n");
int n;
printf("Enter the number of names you want to provide: ");
scanf("%d", &n);
// Declare an array of strings
char names[n][100];
// Read the full names from the user
for (int i = 0; i < n; i++)
{
printf("Enter full name %d: ", i + 1);
fgets(names[i], sizeof(names[i]), stdin); // Read the full name, including spaces
}
// Print the full names
for (int i = 0; i < n; i++)
{
printf("Full name %d: %s", i + 1, names[i]);
}
return 0;
}
Output
Program to store 5 names entered by the user in an array and display them.
Enter the number of names you want to provide: 3
Enter full name 1: Enter full name 2: test name
Enter full name 3: test 2
Full name 1:
Full name 2: test name
Full name 3: test 2