(C language)
Hi.. Can anyone write a function to accept a character and display it 10 times??
Thanks in advance..
(C language)
Hi.. Can anyone write a function to accept a character and display it 10 times??
Thanks in advance..
A few concise, practical points that fill gaps in the thread and give a safe, working pattern.
The original snippet mixes the wrong input/print formats and uses fflush(stdin) (undefined behavior). As recommended, a loop is the right approach; as and noted, the problem is the input/printing formats and how the input buffer is handled. Below are small, robust patterns you can drop into a program.
Simple, portable printer function (prints any char n times):
#include <stdio.h>
void repeat_char(char c, size_t n) {
while (n--) putchar((unsigned char)c);
} Two safe ways to get the character before calling repeat_char:
fgets() and take the first character (avoids scanf quirks).Example using fgets():
char buf[16];
if (fgets(buf, sizeof buf, stdin)) {
repeat_char(buf[0], 10);
putchar('\n');
} Notes and cautions
fflush(stdin) to clear input; it’s undefined. If you must discard leftover input, loop on getchar() until '\n' or EOF. getchar() store its result in an int so EOF can be detected. '\n' for readable output and return a proper int from main. This addresses the issues raised by ’s snippet and ties back to the loop suggestion from and the input/format warnings from and .
Jump to Post— zeroliken 79Sorry but we don't write code here for you, that's the rule
So I'll give a suggestion instead to get you started
...create a loop in the function that will print out the character 10 times
if you have problems with the code then post it here
Jump to Post— zeroliken 79at scanf() use %c for a character variable
plus avoid the fflush(stdin);
->read this article explaining why
Sorry but we don't write code here for you, that's the rule
So I'll give a suggestion instead to get you started
...create a loop in the function that will print out the character 10 times
if you have problems with the code then post it here
(C language)
Hi.. Can anyone write a function to accept a character and display it 10 times??
Thanks in advance..
#include<stdio.h>
main()
{
char a;
int i;
printf("\tEnter a character:\t");
scanf("%d", &a);
fflush(stdin);
for(i=0;i<10;i++)
{
printf("%s", &a);
}
}
Please correct the above program, it displays only once, i want it to display 10 times, what mistake have i done????
at scanf() use %c for a character variable
plus avoid the fflush(stdin);
->read this article explaining why
Use CODE Tags!!!
char a; // define a character
scanf("%d", &a); // read an INTEGER into the character
printf("%s", &a); // print the character as a STRING You tell Us what's wrong with the code...
When you do "scanf("%c", &a);" you save only one character, so you can't use "printf("%s", &a);" to display the character saved in "a", if you execute this code, a segfault might append. To display the caracter in "a", you have to use "printf("%c", a);".
Hey passionated, you should only log in under one username. You should not use the savoie username too, especially in the same thread.
Hey WaltP, I just tryed to help passionated, we are not the same user.
Maybe so, but I was asking passionated so he can learn something, not to have the questions answered by someone else so he doesn't have to think. Just sayin'.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.