VS Code does not display output when debugging C program - c

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?

Related

scanf keeps running nonstop

I have been trying to run the scanf() on my mac, but it never worked (took forever to run the program). Everything else like the printf works fine!
#include <stdio.h>
#include <stdlib.h>
int main(){
char name[20];
printf("What is your name? \n");
scanf("%19s", name);
return 0;
}
terminal
[Running] cd "/Users/yiminghuang/Documents/C/" && gcc scanf4.c -o scanf4 && "/Users/yiminghuang/Documents/C/"scanf4
[Done] exited with code=null in 8.611 seconds
[Running] cd "/Users/yiminghuang/Documents/C/" && gcc scanf4.c -o scanf4 && "/Users/yiminghuang/Documents/C/"scanf4
[Done] exited with code=null in 10.384 seconds
[Running] cd "/Users/yiminghuang/Documents/C/" && gcc scanf3.c -o scanf3 && "/Users/yiminghuang/Documents/C/"scanf3
[Done] exited with code=null in 15.999 seconds
I am new to C programming, so please excuse me if I fail to answer your question correctly!
Now, the program you wrote works perfectly fine but as soon as you enter a string separated by a space, it only takes the characters before the space.
To solve this problem, you can use fgets() function of C.
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main(){
char name[MAX];
printf("What is your name? \n");
fgets(name, MAX, stdin);
printf("String = %s", name);
return 0;
}
//i have used fgets() here for getting the input
//it is a better option than gets()
//since it bounds your array and prevents buffer overflow
I have also added a printf() to check if the string is stored correctly.
IMPORTANT: fgets() also consider the newline character i.e. '\n' as a part of your string. So better to keep that in mind.
Check this question to know more about fgets()
Regards!

Certain C code not displaying output in VS Code

So basically, I have this simple Hello World program and when executed, it outputs correctly.
#include <stdio.h>
int main(){
printf("Hello");
}
Output: Hello
But for some reason adding a scanf command will cause the program to not output anything. It will display that it's running but it won't display anything.
#include <stdio.h>
int num1;
int main(){
printf("Enter number: \n");
scanf("%d", &num1);
printf("%d", num1);
}
Output:
[Running] cd "d:\programming\" && gcc main.c -o main && "d:\programming\"main
I know my code is okay since I tried it on other IDEs and it worked correctly. I even copied code from the internet and tried to run in VSCode but it still didn't work. It still works perfectly fine yesterday but this problem just pops in nowhere.
I use Visual Studio Code 1.62.3 with C/C++, C/C++ Compile Run, and Code Runner extensions.
It's a buffering problem. When standard output (stdout, where printf writes) is connected to an actual terminal then it's line-buffered, which means output is actually written to the terminal when there's a newline.
However, VSCode probably uses its own terminal emulation and uses pipes to connect stdout to that terminal. That means the output will be fully buffered, and you need to explicitly flush it.
So modify the code as such:
printf("Enter number: \n");
fflush(stdout); // Actually write the output to standard output
scanf("%d", &num1);
printf("%d", num1);
That it works for the first example is because then the process terminates, and all output is flushed automatically.

MinGW gcc C compiled program detected as virus by avast

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.

C program in Git Bash does not accept input properly

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:

Cannot enter value from netbeans but working in cmd

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

Resources