Problem with running and compiling c with Git Bash on Windows - c

screenshot
I'm fairly new to all of this, so if it is possible could help be really dumbed down for me thank you :).
So I've had about 1 semester of coding at Uni, and we were learning C. Wanting to practice during my holiday I've done some research and opted to download Atom, installed the Gpp-compiler packages, MinGw and created a path, installed git bash etc. according to all the online instructions I could find.
However when I finally thought I was done with all the set up, a problem occured. As I was testing things out, I realised I couldn't run things properly like I was doing on the uni terminal and gedit.
Shown in my screenshot, shouldn't "hello world" be printed first after I try to run it, then I could enter a value and it should print the value or something.
However after running it would come up blank, until I enter something random and then it would print all at once.
Not sure what's happening here or what I did wrong, any guidance would be great thank you.
Edit: this only seems to be a problem, when I use scanf. Without it, everything prints out in order.
My code:
#include <stdio.h>
int main (void) {
int value;
printf("hello world\n");
scanf("%d", &value);
printf("%d", value);
return 0;
}
when I try to call, it comes out blank until I input 45, then it prints out "hello world" "45"
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ gcc -o main main.c
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ ./main
45
hello world
45
Another example:
#include <stdio.h>
#include <math.h>
int main(void) {
double sideA, sideB, sideC;
double s, area;
printf("Please enter 3 sides of your triangle: \n");
scanf("%lf %lf %lf", &sideA, &sideB, &sideC);
s = (sideA + sideB + sideC) / 2;
area = sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
printf("%lf\n", area);
return 0;
}
Output: nothing came out until I entered (2 3 4) and (5.31 4.2 7.77)
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ ./heron
2 3 4
Please enter 3 sides of your triangle:
2.904738
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ ./heron
5.31 4.2 7.77
Please enter 3 sides of your triangle:
10.542172

Related

In what situation the output could go wrong like this?

I am trying to do the problem 200B on codeforces. I tested my code, and the output was all right. But when I uploaded it to the online judge system, I failed on the very first test case. It said my output was -0.000000000000, instead of 66.666666666667.
But I have compiled and run on Visual Studio C++ 2010, MacOS clang 13.0.0, and Linux GCC 6.3.0, the outputs were all the same as mine, 66.666666666667. I am very curious and want to figure out in what situation the output could be -0.000000000000.
On my computer,
Input:
3
50 50 100
Output:
66.666666666667
On the online judge system,
Input:
3
50 50 100
Participant's output
-0.000000000000
Jury's answer
66.666666666667
Checker comment
wrong answer 1st numbers differ - expected: '66.66667', found: '-0.00000', error = '1.00000'
#include <stdio.h>
int main(void)
{
int n;
double sumOrange = 0;
double sumDrink = 0;
scanf ("%d", &n);
while (n-- > 0) {
int m;
scanf("%d", &m);
sumOrange += m / 100.0;
sumDrink++;
}
printf("%.12lf\n", (sumOrange / sumDrink) * 100.0);
return 0;
}
I just don't understand why my output could be -0.000000000000. Please help, thanks.
Update: Tested on different versions of GCC (4.9, 5.1, 6.3), the wrong output does not appear. Guess the cause might lie in the specific implementation of printf.
The problem is because printf function in GNU gcc C11 does not support %.12lf format. It should be changed to %.12f For more information, you can read the article below:
Correct format specifier for double in printf

Execution of code stuck on running in VSCode

So I have preinstalled extension like 'Code Runner' and 'C/C++' in Vscode.
The problem I am facing is in running a code which gets stuck on running :
#include <stdio.h>
#include <stdlib.h>
int main()
{
//program to convert minutes into years and days
int min = 0;
double yrs = 0.0;
double days = 0.0;
double min_in_yr = 0;
printf("Enter the nmuber of minutes: ");
//get input from the user
scanf("%d", &min);
//calculation
min_in_yr = (60 * 24 * 365);
yrs = (min / min_in_yr);
days = (yrs * 365);
printf("%d minutes is approx %f years and %f days\n", min, yrs, days);
return 0;
}
There is no problem with the code as it seems to be running fine on CodeBlocks.
Also I tried executing a simple hello world program which seemed to be executed perfectly on Vscode in 1.8 s.
What could be the problem?
So I looked up and found a solution for this problem.May it helps other beginners who get stuck doing this on Vscode.
So the problem is actually with the scanf function which seems to be not compatible with OUTPUT section of the output panel.I don't know the reason for this but I found a alternate way to do this.
Just make your code run in the terminal instead.
To do this:
1.Make sure you have code runner extension installed
2.Goto File>Preferences>Settings>Extensions>Run Code configuration and under that scroll down to search for Code Runner:Run in Terminal and check that option.If you are not able to find that search in settings for text in bold and you find
3.That's it.Run your code and it will automatically be executed in the terminal

Strange output when using system("clear") command in C program

I have the following code
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#define dimensions 5
int RandomNumInRange(int M, int N)
{
return M + rand() / (RAND_MAX / (N - M + 1) + 1);
}
char ** CreateWorld(int dim)
{
int i,j;
char **world = malloc(dim *sizeof(char*));
for(i=0;i<dim;i++)
world[i]=malloc(dim*sizeof(char));
for(i=0;i<dim;i++)
for(j=0;j<dim;j++)
world[i][j]=42;
return world;
}
void CreateCastle(char **world)
{
//assuming world is big enough
//to hold a match of 2
int randRow,randCol;
//1 to dimension -2 so we can spawn a 3x3 castle
randRow = RandomNumInRange(1,dimensions-2);
randCol = RandomNumInRange(1,dimensions-2);
printf("position: %d %d\n", randRow, randCol);
world[randRow][randCol]='c';
//fill the rest so castle is 3x3
//assuming there is enough space for that
world[randRow-1][randCol-1]=35;
world[randRow-1][randCol]=35;
world[randRow-1][randCol+1]=35;
world[randRow][randCol-1]=35;
world[randRow][randCol+1]=35;
world[randRow+1][randCol-1]=35;
world[randRow+1][randCol]=35;
world[randRow+1][randCol+1]=35;
}
void DisplayWorld(char** world)
{
int i,j;
for(i=0;i<dimensions;i++)
{
for(j=0;j<dimensions;j++)
{
printf("%c",world[i][j]);
}
printf("\n");
}
}
int main(void){
system("clear");
int i,j;
srand (time(NULL));
char **world = CreateWorld(dimensions);
DisplayWorld(world);
CreateCastle(world);
printf("Castle Positions:\n");
DisplayWorld(world);
//free allocated memory
free(world);
//3 star strats
char ***world1 = malloc(3 *sizeof(char**));
for(i=0;i<3;i++)
world1[i]=malloc(3*sizeof(char*));
for(i=0;i<3;i++)
for(j=0;j<3;j++)
world1[i][j]="\u254B";
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%s",world1[i][j]);
puts("");
}
free(world1);
//end
return 0 ;
}
If I use the system("clear") command, I get a line consisting of "[3;J"
followed by an expected output. If I run the program again, I get the same gibberish, then many blank newlines, then the expected output. If I put the system("clear") command in comments then both the "[3;J" and the blank newlines don't show and the output is expected.
Edit: it seems the error is not in the code, but rather in the way the terminal on my system is (not) set. Thank you all for your input, I definitely have a lot of interesting stuff to read and learn now.
The codes being sent by your clear command from don't seem to be compatible with the Gnome terminal emulator, which I believe is what you would be using.
The normal control codes to clear a console are CSI H CSI J. (CSI is the Control Sequence Initializer: an escape character \033 followed by a [). CSI H sends the cursor to the home position, and CSI J clears from the cursor position to the end of the screen. You could also use CSI 2 J which clears the entire screen.
On Linux consoles and some terminal emulators, you can use CSI 3 J to clear both the entire screen and the scrollback. I would consider it unfriendly to do this (and the clear command installed on my system doesn't.)
CSI sequences can typically contain semicolons to separate numeric arguments. However, the J command doesn't accept more than one numeric argument and the semicolon seems to cause Gnome terminal to fail to recognize the control sequence. In any event, I don't believe Gnome terminal supports CSI 3 J.
The clear command normally uses the terminfo database to find the correct control sequences for the terminal. It identifies the terminal by using the value of the TERM environment variable, which suggests that you have to wrong value for that variable. Try setting export TERM=xterm and see if you get different results. If that works, you'll have to figure out where Linux Mint configures environment variables and fix it.
On the whole, you shouldn't need to use system("clear") to clear your screen; it's entirely too much overhead for such a simple task. You would be better off using tputs from the ncurses package. However, that also uses the terminfo database, so you will have to fix your TERM setting in any case.

Do not understand message received after running temperature convert program

I am new to C and trying to write a program using Xcode that takes the temperature in Fahrenheit and converts it to Celsius, and vise versa. My code so far is below.
#include <stdio.h>
#include "hw2.h"
void convert_temp(int degree1, char scale1, int* degree2, char* scale2){
if (scale1 == 'F') {
*degree2 = ((degree1 - 32) * 5) / 9;
*scale2 = 'C';
}
else {
*degree2 = ((degree1 * 9) / 5) + 32;
*scale2 = 'F';
}
}
int main() {
int degree1, degree2;
char scale1, scale2;
printf("Enter a temperature and a scale\n");
scanf("%d %c", &degree1, &scale1);
convert_temp(degree1, scale1, &degree2, &scale2);
printf("%d %c = %d %c\n", degree1, scale1, degree2, scale2);
return 0;
}
Here is an example of correct i/o:
Enter a temperature and a scale
32 F
32 F = 0 C
However, when I run the code, this is what I get:
Enter a temperature and a scale
32 F
hw2 was compiled with optimization - stepping may behave oddly; variables may not be available.
(lldb)
I cannot understand the output I am getting. Can anybody tell me why I do not get 32 F = 0 C on my output? Everything in my code seems fine to me.
Assuming hw2 is the name of your program, then the debugger is complaining that it was compiled with optimisations turned on, which isn't normal during development, as the optimizer does all sorts of clever things to get the program running faster.
You need to do the following in Xcode:
Ensure you are debugging using the Debug build configuration (Check your Schemes).
Ensure you haven't turned on Optimizations for the Debug build configuration (Check your Build Settings).

gcc:error in input

i am enthusiast and new in programming trying this simple c language code and compile it with gnu compiler. here is my code:
/*simple program:trying to printf string and get a letter*/
#include "stdio.h"
int main()
{
int i=0;
char c;
while(i++<100){
if(i % 2)
printf("%C this is even number",i);
if(i==50){
c=getchar();
printf("you enter %c letter",c);
}
}
return 0;
}
however when i compile it with gcc and run in terminal, it doesnt show anything,no error and no warning. i tried to figure it out by changing the way my code behave but still not work. is it the code that wrong or gcc have bug or may be i miss about something?
Try to put a newline at the end of printf text:
printf("%C this is even number\n",i);
Also i is an int so use the %d format:
printf("%d this is even number\n",i);
Edit: I just tested this with the changes and it works for me.
You should call fflush(stdout) after your display.
It ran for me using cygwin under Windows Vista.
I pasted your code into a file called test.c and ran gcc test.c that output a file called a.exe which I then ran.
johnma#johnma-PC ~
$ ./a.exe
this is even number♥ this is even number♣ this is even number this is even numb
this is even numbern this is even number◄ this is even number this is even numb
er this is even number this is even number↓ this is even numberthis is even numb
er this is even number this is even number! this is even number# this is even nu
mber% this is even number' this is even number) this is even number+ this is eve
n number- this is even number/ this is even number1 this is even number
you enter
letter3 this is even number5 this is even number7 this is even number9 this is
even number; this is even number= this is even number? this is even numberA this
is even numberC this is even numberE this is even numberG this is even numberI
this is even numberK this is even numberM this is even numberO this is even numb
erQ this is even numberS this is even numberU this is even numberW this is even
numberY this is even number[ this is even number] this is even number_ this is e
ven numbera this is even numberc this is even number
johnma#johnma-PC ~

Resources