C program to print the name using array:
In this c program, we will learn how to print the name of a user using an array. I will show you different ways to print the content of an array.
You will learn:
- How to take a name as input from the user.
- How to read and keep the name in an array.
- How to print the array contents, i.e. the name entered by the user.
I will also show you how to read name with blank spaces as well.
Example 1: Assign the string directly to an array:
We can directly assign a string to an array directly as like below:
#include <stdio.h>
int main()
{
char arr[40] = "Albert";
printf("Name: %s\n", arr);
return 0;
}
If you run this program, it will print:
Name: Albert
We have created a character array arr and assign the string Albert to it. By using the %s format specifier, we are printing the content of the array.
It will work with strings with blank spaces as well. For example:
#include <stdio.h>
int main()
{
char arr[40] = "Albert Einstein";
printf("Name: %s\n", arr);
return 0;
}
It will print:
Name: Albert Einstein
Example 2: Print the array content by using a loop:
We can use a for loop or while loop to iterate over the array characters one by one and print these characters on the console.
For example:
By using for loop:
#include <stdio.h>
int main()
{
char arr[40] = "Albert Einstein";
printf("Name: ");
int i = 0;
for (i = 0; arr[i] != '\0'; i++)
{
printf("%c", arr[i]);
}
return 0;
}
Here,
- We are using a for loop to print the content of the string.
- The loop starts from i = 0 and it stops if the character in arr for a specific value of i become \0.
- Inside the loop, we are printing the current iterating character.
If you run this program, it will print the same output.
Name: Albert Einstein
By using while loop:
The same program can be written by using a while loop as well.
#include <stdio.h>
int main()
{
char arr[40] = "Albert Einstein";
printf("Name: ");
int i = 0;
while (arr[i] != '\0')
{
printf("%c", arr[i]);
i++;
}
return 0;
}
It is working in a similar way. The while loop will run and print all characters till the end of the string.
Example 2: Read the string as input from the user using scanf:
You might want to read the string as input and store it in a character array. We can use scanf to do that.
#include <stdio.h>
int main()
{
char arr[40];
printf("Enter a name: ");
scanf("%s", arr);
printf("You have entered: %s\n", arr);
return 0;
}
In this example, we are using scanf to read the user-input and storing it in arr. The printf is printing the content of arr.
It will work as like below:
Enter a name: Albert
You have entered: Albert
But, it will not work for string with blank spaces. For example:
Enter a name: Albert Einstein
You have entered: Albert
It stops the reading if it finds any blank space.
Read strings by using gets():
If you want to read strings with spaces, you can use gets. The gets method reads the user input until a newline character is found. It reads the user-input and puts the content in an array.
#include <stdio.h>
int main()
{
char arr[40];
printf("Enter a name: ");
gets(arr);
printf("You have entered: %s\n", arr);
return 0;
}
It can read strings with spaces:
Enter a name: Albert Einstein
You have entered: Albert Einstein
If you are using gets, you might see this warning on compiling the program:
warning: this program uses gets(), which is unsafe.
This is because gets doesn’t check for array bounds and it might throw buffer overflow error.
Better alternative: use fgets:
You can use fgets instead. This is safe to use because we can pass the maximum characters to read as the second argument.
#include <stdio.h>
int main()
{
char arr[40];
printf("Enter a name: ");
fgets(arr, 40, stdin);
printf("You have entered: %s", arr);
return 0;
}
It will give similar output and it will not show any warning message. If you are reading a string as user-input, fgets is the best way to do that.
You might also like:
- C program to read an unsigned integer value using scanf
- C program to convert decimal to binary
- C program to get the integer and fraction or decimal part
- C program to print all perfect numbers in a range
- C fsetpos method explanation with example
- C program to print a two-digit number to word
- How to find the income tax of an employee in C
- C program to multiply two numbers using plus or addition operator