enter image description here
#include <stdio.h>
char s[50];
int n,i;
int main(){
scanf("%d",&n);
for (i=0;i<n;i++){
scanf("%s",s);
}
return 0;
}
it shouldn't give the output but it duplicates the input and gives output
for example try to write:4 hello how are you it won't give, but if you copy paste the input as in the example it will give that:(guys I wrote each of them in separate line but site doesn't show it like this. Also, this bug won't happen if you write it one by one you have to copy paste it.)
Input:
4
hello
how
are
you
Output:
hello
how
are
you
Here's a better example from an online compiler:
https://godbolt.org/z/joYdsdc11 As you can see (right window below stdin/input), there is no output.
Here is a fixed program (I also removed global variables since using them is bad practice):
#include <stdio.h>
int main (void){
int n;
char s[50];
scanf("%d",&n);
printf("%d\n", n);
for (int i=0;i<n;i++){
scanf("%s",s);
puts(s);
}
return 0;
}
Output:
4
hello
how
are
you
https://godbolt.org/z/sjcMrq4vb
Related
I'm simply trying to write down something to console using C language before getting user input, but it always asks for input then when I press enter it print what I wanted to print before getting user input.
I'm using Eclipse IDE (version 2020 -09) and my compiler is Cygwin.
this is how I'm to trying to accomplish that:
#include <stdio.h>
int main() {
printf("please enter your name: ");
char a[3];
gets(a);
return 0;
}
Output is usually buffered by default.
You can flush the output buffer explicitly by calling fflush(stdout) after printf(), eg:
#include <stdio.h>
int main() {
printf("please enter your name: ");
fflush(stdout); // <-- here
char a[3];
gets(a);
return 0;
}
Or, you can include \n in the output, eg:
#include <stdio.h>
int main() {
printf("please enter your name: \n");
char a[3];
gets(a);
return 0;
}
I want to display a full name, but I can't enter more than two parts of a name. The program stuck when enter a name which has more characters than the number which array has. How can I solve this?
#include <stdio.h>
#include<stdlib.h>
int main(){
char x[25];
printf("Enter your name");
scanf("%s",x);
printf("Your name is %s", x);
return 0;
}
Thank You
I think this can help you. This program doesnt care how many characters, spaces you entered. It only displays first 24 characters and spaces. (1 for string terminator)
#include <stdio.h>
#include <stdlib.h>
int main(){
char x[25];
char *xx=x;
puts("Input Name");
fgets(xx,25,stdin);
puts(xx);
return 0;
}
Hello this is my solution to to uri 1759
#include <stdio.h>
int main()
{
int i,j;
scanf("%d", &i);
for (j=1;j<=i;j++)
{
printf("Ho ");
while (j==i)
{
printf("\b");
printf("!");
break;
}
}
return 0;
}
I don't know what I got wrong ..can you please help me? the link of the problem https://www.urionlinejudge.com.br/judge/en/problems/view/1759
Your solution may look correct, it isn't identical to the desired output, because of the trailing space that you erase by inserting a backspace. So, the solution is to re-craft your logic to avoid having to erase that trailing space.. One way of doing that, which I don't advise to really use, is like this:
#include <stdio.h>
int main()
{
int i,j;
scanf("%d", &i);
for (j=1;j<=i;j++,printf("Ho%s",(j>i)?"!":" "));
return 0;
}
I'm programming in C and I download code block IDE because it's easier to use. As you know, the minimal code in C is to write: Hello World in a CMD window. When I try to launch the program via code block, it works but when I open the .exe file directly, it open and close quickly. Can someone explain me why?
#include <stdio.h>
int main() {
int age = 0;
printf("how old are you?");
scanf("%d", &age);
printf("You are %d", age);
getchar();
return 0;
}
I am guessing your program looks something like this:
#include <stdio.h>
int main()
{
printf("Hello, world!\n");
}
The program prints Hello, world! to the screen and then ends because it has nothing left to do.
A simple fix to this is to add the function getchar() after the printf statement. This will cause the program to wait for any user input before closing. It stands for get character.
Your new program should look like this:
#include <stdio.h>
int main()
{
printf("Hello, world!\n");
getchar();
}
UPDATE:
#include <stdio.h>
int main() {
int age = 0;
printf("how old are you?\n");
scanf("%d", &age);
getchar();
printf("You are %d.\n", age);
getchar();
}
What I think you're describing is the OS destroying the temporary command window when the program is done executing. Try opening a command window yourself, and then running your .exe from there. Alternatively, use int t; scanf("%d", &t) (or something) to keep your program from finishing, thus holding the window open
Put a getchar() as the last line of main:
int main()
{
// code
getchar();
return 0;
}
Can you tell what wrong here?
#include <stdio.h>
#include <stdlib.h>
int test (void)
{
int i;
printf("Enter a number: ");
scanf("%d",&i);
return i;
}
int main (void)
{
test();
return 0;
}
This is just a simple example but for some reason main doesn't run unless I get rid of the scanf.
Always use a '\n' at the end of your printf string. This makes the output buffer flush and print the string. Add more prints in your program.
You can rewrite your program like following, and the prints will help you understand what is happening with your program.
#include <stdio.h>
#include <stdlib.h>
int test (void)
{
int i;
printf("Enter a number: \n");
scanf("%d",&i);
printf("You just eneterd : %d\n",i);
return i;
}
int main (void)
{
printf("About to call test() \n");
test();
printf("Done calling test() \n");
return 0;
}
Better get a good C programming book for understanding these basic stuff. I suggest The C programming language
I think you must make use of fflush() or making use of a '\n' character at the end of the printf function which will eventually flush the std output buffer. For checking just make use of the printf() for printing the value of the variable just after reading the value.
Hope that helps....