I am trying to run my C program in Git Bash but it doesn't seem to accept inputs as expected and as it does in Windows Command Prompt.
This is my simple program:
#include <stdio.h>
int main() {
char nom[10];
printf("Enter you name: ");
gets(nom);
printf("Your name is %s.", nom);
return 0;
}
This is a screenshot from Git Bash:
This is a screenshot form CMD:
Related
I have readed a lot of people with the same problem but the most of them are Windows Defender, in my case it only happens with avast(free version) and it detects this simple hello program compiled version:
#include <stdio.h>
int main()
{
printf("Name: ");
char name[20];
scanf("%s", name);
printf("Hello, %s\n", name);
return 0;
}
as "Win32:TrojanX-gen[Trj]" but some days ago i made another program and ran it into notepad++ console using cmd and nothing happened, is it a problem with avast or is it my pc?
Thank you.
I've written a simple C program:
#include <stdio.h>
int main(void){
char first[20];
char last[20];
printf("Enter first name: ");
scanf("%s", first);
printf("Enter last name: ");
scanf("%s", last);
printf("Hello %c %c. \n", first, last);
return 0;
}
Then I built the program with compiler from GCC using gcc.exe build active file command and debugged it with the GDB configuration. There is no output in the debug console unless I set 'externalConsole' = true in launch.json, however if I do so I will not be able to examine if my program works or not, because the console window closes immediately after I have entered the strings.
How should I solve this problem and make sure I can debug the program properly?
I'm using gcc compiler for c programming.I'm trying to run "for loop" but in command prompt when I execute the program it gives "The syntax of the command is incorrect".
The program is:
#include <stdio.h>
int main()
{
int x;
for (x=1; x<=10; x++)
{
printf("%d\n",x);
}
return 0;
}
enter image description here
Your computer does not understand that for is the name of your program because the shell has a builtin command named for. Give the executable another name, like for1, and it should work.
I have installed MinGW on netbeans for C and C++ programming.
Here is a simple code that I am trying to run on netbeans :
#include <stdio.h>
int main(int argc, char** argv) {
printf("Inside Main...\n");
int n;
printf("Enter : ");
scanf("%d", &n); // When I remove this line, it is working.
printf("You have entered %d.", n);
return (1);
}
Whenever I try to access any value from netbeans console, I don't see anything.
Output with scanf(...)
Output without scanf(...)
And if I try to run these code from cmd, all are working
for scanf() you must use Netbeans External Terminal !
Normal Run
You can also use Netbeans Standard Output !
But this is more misleading.
While you see an empty Terminal do input 123
after hit enter , you get the output all at once .
I had the same issue while running a CPP program.External output didn't helped me. I set the console type to Standard output and it solved the issue.
Right Click cpp Application-->properties-->run--->Consoletype to standard output
Say I have the following program that simply outputs "Hello World":
//DEMO.c
#include<stdio.h>
int main()
{
printf("HELLO World");
}
Now I want to display it both to the screen and to a file output.txt.So I enter the following command in the command prompt(I use CodeBlocks on Windows XP and have configured it to work on command prompt as well):
demo.exe>>output.txt>>stdout
It doesn't work!!! Please tell me how to do it,ie how to output the same thing that I see on my screen(When i run the program),simultaneously to a text file?
You will need to download a tee command for Windows. tee is a UNIX/Linux command that copies the standard input to standard output and also outputs to a file. Then, you can do this:
demo.exe | tee output.txt
Here is one port of tee for Windows.
#include <stdio.h>
#define my_fprintf(fp,...) do{fprintf(fp, __VA_ARGS__);fprintf(stdout, __VA_ARGS__);}while(0)
int main(int argc, char **argv){
FILE *fp;
fp=fopen("output.txt","w");//or filename from argv[1]
my_fprintf(fp, "hello world by %s\n", argv[0]);
fclose(fp);
return 0;
}