Visual Studio Code keeps running - c

i'm trying to use visual studio code to run various c programs but the code never outputs as it stays in a perpetual state of running.
Here's an example of the code i'm trying to run
#include <stdio.h>
typedef struct comp
{
float real;
float imag;
} complex;
complex mult(complex a,complex b)
{
complex c;
c.real=a.real*b.real - a.imag*b.imag;
c.imag=a.real*b.imag + a.imag*b.real;
return c;
}
int main()
{
float re,im;
complex a,b;
printf("Please enter the first complex number (re, im): ");
scanf("%f%f", &re, &im);
a.real = re; a.imag = im;
printf("Please enter the second complex number (re, im): ");
scanf("%f%f", &re, &im);
b.real = re; b.imag = im;
a = mult(a,b);
printf("Their product is %f + %fi \n", a.real, a.imag);
return 0;
}
And here is a screenshot of the application
As I said when I run the code it just get's stuck "running".
I've downloaded the necessary extensions for visual studio code that let you run C/C++.
Anyone encountered this problem before?
Thanks

You need to correctly configure your Code Runner extension from the generated JSON config under .vscode folder in your workspace. Also, you might haven't focused you've opened GitHub Authentication output log, not the code runner's if it actually ran successfully, which doesn't seem:
If you find it hard to configure, then switch to the Terminal section and type the following command to compile and execute it manually:
gcc -Wall -o main main.c; ./main # for PowerShell
gcc -Wall -o main main.c && main # for Command Prompt

I was facing the same problem. Follow the below steps and I hope it will work for you too.
CTRL+SHIFT+X
Type in search box Code Runner
Install the extension
Then click on File - Preferences - Settings
Type code run in the search box.
Scroll down until you find Code-runner: Run In Terminal. Check the box Whether to
run code in Integrated Terminal.
Restart VS Code

Related

C-Prog Code::Blocks -- math.h library not linking within IDE ... works fine in command line. Where is the file/folder to link in IDE?

NEWB here: Using Linux Mint 20.x - Code::Blocks - GCC --- I got this code to work on command line. It wasn't working until I linked the math library using <-lm> I'm trying to link math library in Code::Blocks to run code within IDE. I can't find the library folder/file to link within Code::Blocks.
SideNote: I added sqrt just to test the math. sqrt works in IDE yet floor and ceil don't. Seemed bizarre to me that some math.h functions work and others do not.
gcc tut36.c -o tut36 -lm
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(){
double bacon1 = 9.65345;
double bacon2 = 3.3;
printf("bacon1 is %.2lf\n", floor(bacon1));
printf("bacon2 is %.2lf\n", floor(bacon2));
printf("bacon1 is %.2lf\n", ceil(bacon1));
printf("bacon2 is %.2lf\n", ceil(bacon2));
int year1;
int year2;
int age;
printf("Enter a year\n");
scanf(" %d", &year1);
printf("Enter another year\n");
scanf(" %d", &year2);
age = year1 - year2;
age = abs(age);
printf("Your age is: %d %.0lf\n", age, sqrt(16));
return 0;
}
Solution found: in CODE::BLOCKS .... -lm must be inserted into the compiler settings. Settings -> Compiler -> Linker settings (tab) -> other linker options -> -lm
-lm evidently links the math library to the project. Not sure why this is necessary if the math library is part of the standard library, but there you go.
Follow the steps shown in the image
Click on Settings from start menu and then click on Compiler...
Click on Linker settings tab
Click on Add
Type m on the box and click on ok
Now click on ok on the bottom right
And you are done!
Now you can compile any code with math.h header
Thank you!

Bloodshed Dev C++ Source file not compiled

I have written a program in Bloodshed using the C language for homework, but after clicking compile and run, I get an error message saying: Source file not compiled. I have ran compile only with it telling me that it's done, but when I compile and run afterwards, I get the same previous error message. How do I fix this? I am using windows 10 and here is the program:
/*TECH 1211 Computer Programming */
/*Mark Bowman */
/*Program Name "Formatted Output of Table" */
/*Spring 2019 */
#include<stdio.h>
#include<stdlib.h>
int main (void) {
printf("Mark Bowman\n\n");
char airline;
printf("Enter the first letter of the airline:");
scanf("%c",&airline);
int airline_number;
printf("Enter the flight number:");
scanf("%d",&airline_number);
int hour,minute;
char time_of_day;
printf("Enter the departure time as (hh:mm)");
scanf("%d:%d %c",&hour,&minute,&time_of_day);
float ticket_price;
printf("Enter the ticket price:");
scanf("%f",&ticket_price);
printf("\n\n\nAirline Flight\tDeparture\tTicket\n\t\tNumber\tTime\tPrice\n\t %c %d\t%d:%d %cm\t$%.2f\n\n\n--------------------------",airline,airline_number,hour,minute,time_of_day,ticket_price);
return(0);
}
I just came across this issue and it turns out Bloodshed Dev C++ is not compatible with Windows 10. It didn't even detect any errors during compilation if I intentionally added them.
Fortunatelly, there's this open source project called Embarcadero Dev-Cpp which is a port to Windows 10, they even have a portable version that works very well.

Errors when Compiling my First C Program

I am running Ubuntu on a virtual machine with gcc downloaded. I wrote up a code in gedit which contains:
#include <stdio.h>
/* This is a comment. */
int main(int argc, char *argv[])
{
int distance = 100;
// this is also a comment
printf("You are %d miles away.\n", distance);
return 0;
}
When I do the make Ex1.c it says that my file is 'up to date.' so I type in ./Ex1.c and it gives me these errors:
./Ex1.c: line 3: /bin: Is a directory
./Ex1.c: line 4: syntax error near unexpected token '('
./Ex1.c: line 4: 'int main(int arc, char*argv[])'
I don't understand this, I thought it might be how I am typing the code in but then I pasted the code in from the 'Learn C the Hard Way' GitHub and I still get these errors! I just want to run my dang code!
Do it this way.
Open terminal write gedit ex1.c
In the new gedit window write the code.
Close the gedit window.
In the terminal write gcc ex1.c
In the terminal write ./a.out
You're trying to run the source code file.
You have to run the compiled binary.
The compiled binary is often called a.out.
Try doing ./a.out

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

Cilk++ cilkexample.c = 200 lines in stderr

I installed Cilk successfully on my home computer, a 32-bit machine running Ubuntu. I replicated the process to the best of my knowledge on my 64-bit Ubuntu netbook, excepting, of course, that I downloaded the 64-bit version instead of the 32-bit version. When attempting to run the very simple cilkexample.c copied below, however, I get very very many errors, all seeming related to it not having access to library files:
In file included from /usr/include/stdio.h:28,
from cilkexample.c:1:
/usr/include/features.h:323:26: error: bits/predefs.h: No such file or director\
y
/usr/include/features.h:356:25: error: sys/cdefs.h: No such file or directory
/usr/include/features.h:388:23: error: gnu/stubs.h: No such file or directory
In file included from cilkexample.c:1:
/usr/include/stdio.h:36:25: error: bits/types.h: No such file or directory
/usr/include/stdio.h:161:28: error: bits/stdio_lim.h: No such file or directory
/usr/include/stdio.h:846:30: error: bits/sys_errlist.h: No such file or directo\
ry
In file included from /usr/include/stdio.h:34,
from cilkexample.c:1:
/usr/local/cilk/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.4/include/stddef.h:\
214: error: expected constructor, destructor, or type conversion before ‘typede\
f’
In file included from cilkexample.c:1:
/usr/include/stdio.h:49: error: expected constructor, destructor, or type conve\
rsion before ‘typedef’
et cetera, et cetera, et cetera.
Here is the file I attempted to compile with the command cilk++ -o cilkexample cilkexample.c:
#include <stdio.h>
#include <cilk.h>
int foo() {
return 100;
}
int bar() {
return 50;
}
int cilk_main(int argc, char **argv) {
int x, y;
x = cilk_spawn foo();
y = cilk_spawn bar();
cilk_sync;
printf("Got %d %d, expecting %d %d\n", x, y, 100, 50);
return 0;
}
Again, I think this has to be a configuration problem. The code is unmodified from the working version our professor distributed, which I tested on a separate machine.
Last bit of information I can think of is the PATH.
******#********:~/Path/To/Cilk$ echo $PATH
/usr/local/cilk/bin/:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Thanks for your help!
Looks like you are missing some headers. You say you are on Ubuntu in which headers are distributed in xxx-dev packages.
Googling for some of the headers in your error I found that bits/types.h are part of libc6-dev on Debian, you should check that you have that package at least.
You should check with your distro in what package the files appear, I don't have a Debian or Ubuntu machine available to check on right now.
Edit: I found myself an Ubuntu box and it looks like libc6-dev contains all the files listed at least. dpkg-query -S [file] gives you package name

Resources