C scanf program executes 2 times - c

I'm new to C. I was learning scanf() function, and the error that occured is that when I run the .exe file, it executes, gives correct output, and closes; but it then again executes on its own , asks for input and gives correct output and closes. Here's my code :
#include <stdio.h>
int main() {
int a;
printf("Enter a num\n");
scanf("%d", &a);
int b = 1601;
if (a == b) {
printf("Found a match!!");
} else {
printf("Match not found!");
}
return 0;
}

The code works well in Ubuntu 14.04. At MS Windows + MinGW there are some problem sometimes. If you want to develop a program with gcc you should use Unix Like Operating System (Linux, BSD, etc) If you want to still use MS Windows, then you should use MS Visual Studio for the best result.

Related

Eclipse and MinGW seem to be executing out of sequence

Working on a simple C program using basic I/O. printf and scanf to get started.
Using Visual Studio 2019 and Eclipse with MinGW.
The program has several printf statements followed by scanf_s to get an input. I was expecting that printf statements would be executed first and then scanf_s would read the keyboard input.
How in Eclipse that's not what happens. Seems that scanf_s is executed first and then printf statements.
#include <stdio.h>
int main(void)
{
int s;
printf("** Service Ticket Center **\n");
printf(" Choose from the following options.\n");
printf(" 1. Create new ticket.\n");
printf(" 2. Update ticket status.\n");
printf(" 3. Delete ticket.\n");
printf(" 4. Find ticket.");
scanf_s("%d", &s);
return 0;
Ran same code in Visual Studio and printf statements were executed first followed by scanf_s as expected. It seems like Eclipse is the issue.

I am facing an issue in c

I have recently started to learn programming in c language. I installed Mingw and vista studio code for the same. However, when I wrote my first program it just keeps showing error and won't run. I made a program to calculate simple interest. I have checked the syntax multiple times. Please help in pointing out the error.
#include <stdio.h>
int main()
{
int p, n;
float r, si;
printf("Enter principal\n Enter no. of years\n Enter rate\n");
scanf("%d, %d, %f", &p, &n, &r);
si = p * r * n / 100;
printf("%f\n", si);
return 0;
}
Picture of the error
The gcc executable is not on your PATH, and so the system can't find it (and thus not execute it)
Update the PATH environment variable so that the directory where gcc is installed is part of it.
See here for how to do that.

.Exe file won't execute correctly

I'm new to the programming world, recently I began my programming path with C, and because of that I made a program that determines if a number is whether perfect or not. I use Code::Blocks IDE, and it works just fine, the problem is when I click the option "Build and run", the IDE executes the program and works perfectly, but when I select the .exe file from my desktop, it opens up, but doesn't show any output, the window just closes suddenly. Does someone have any idea on how to solve this issue?
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main() {
int N;
int j;
int Sum = 0;
printf("Input a number.\n");
scanf("%d",&N);
for (j = 1; j < N; j++)
{
if (N%j==0)
{
Sum+=j;
}
}
if (Sum==N)
{
printf("The number is perfect.\n");
} else {
printf("The number is not perfect.\n");
}
return 0;
}
Running program with Code::Blocks Build and run option
The only part of the Desktop located .exe that I can reach
If someone can suggest a solution, I will be very thankful!
As it was said, the program exits immediatly after it's completion. If you want to run the program, executing it by double click, you can place a pause condition in the program, for instance if you put getchar(); just before the return 0; statement, it will only exit after it receives an input from the keyboard, that is after you enter a key.

Run C program on unix not in command line

It might be my code, but I can compile and run this program via command line. In windows the exe file is also runnable. However, I cannot run the compiled code outside of the terminal on any unix systems (ubuntu or osx) I am pretty new to C so any help would be appreciated. Thank you!
Clarification:
In Ubuntu I run
gcc game.c -o game
./game
And then it runs perfectly. But if I go through the GUI to the game file and double click it, it doesn't do anything.
I'm used to on Windows it would bring up a command and run the program as if you ran it from cmd.
Code (it is a simple number guessing game):
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int guessed = 0;
int guesses = 0;
int total_guesses = 0;
int games_played = 0;
int range_top = 10;
int generate_random_number()
{
srand(time(NULL));
return (rand() % range_top);
}
void take_guess(int num)
{
int guess;
printf("what is your guess: ");
scanf(" %i",&guess);
if(guess == num)
{
guessed = 1;
}
else if(guess>num)
{
printf("Your guess was too high,\n");
}
else
{
printf("Your guess was too low.\n");
}
}
void print_stats()
{
printf("\n\n\nGames Played: %i\nTotal Guesses: %i\nAverage Guesses Per Game: %f\n\n\n\n",games_played,total_guesses,(double)total_guesses/games_played);
int i = 5;
while(i>0)
{
printf("exiting in %is\n",i);
i--;
sleep(1);
}
}
int main(void)
{
printf("This is a game in C\n");
printf("Would you like to play? (y/n): ");
char play;
scanf("%c",&play);
while(play == 'y')
{
printf("I am thinking of a number between 0 and %i\n",range_top);
int num = generate_random_number();
while(guessed ==0)
{
take_guess(num);
guesses++;
}
games_played++;
total_guesses+=guesses;
printf("It took you %i guesses to win.\n",guesses);
printf("Would you like to play again? (y/n): ");
scanf(" %c",&play);
guessed = 0;
guesses = 0;
}
print_stats();
}
But if I go through the GUI to the game file and double click it, it
doesn't do anything
How do you know that it doesn't run? I bet it does run, but as it is not run in context of any terminal (command line window), you just can't see its output. That's how Linux (and Unix in general) work.
Windows differentiates GUI and commandline applications, and in the latter case automatically brings a console window. Unfortunately (fortunately?), that's not the case in Unix.
If you want to achieve Windows behavior, you could:
Create a launcher for your application. That will allow you to specify custom icon etc.
Create a shell script that will invoke teminal and your application inside of it:
game.sh:
#!/bin/bash
gnome-terminal -e "./game"
Please note that not everyone will have gnome-temrinal installed, so you may have to adjust your script to support more terminal emulators (xterm, rxvt, maybe konsole for KDE users).

run c in xcode shows nothing

I am trying to run c in xcode 6,however I run the ⌘R it shows build Succeeded but nothing in console,but I can run it in ubuntu Linux's gcc,here is my c code in ubuntu Linux
#include <stdio.h>
int main()
{
int a[100],i,j,t,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(a[j]<a[j+1])
{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }
}
}
for(i=1;i<=n;i++)
printf("%d ",a[i]);
getchar();getchar();
return 0;
}
when I run it in gcc it's ok,
ubuntu#vi ac9.c
ubuntu#gcc -o ac9 ac9.c
ubuntu#./ac9
the file name is ac9.c
but move it to mac's xcode ,I run the ⌘R it shows build Succeeded but nothing in console,
and here is my code in xcode 6.4
You probably need to signal to the Operating System to dump what is in the output buffer to the screen before the calls to getchar().
The easiest way to do that is to print a newline.
// ...
// print a newline; force OS to dump output buffer
printf("\n"); // or puts("");
getchar(); getchar();
// ...
One other way is to call fflush()
// ...
// force OS to dump output buffer
fflush(stdout);
getchar(); getchar();
// ...
From Xcode not showing anything in console with C++,
Your image doesn't show that you ran the program, only that you built
it. Look at the Log Navigator (the last one, ⌘7) and see if there are
any logs for 'Debug one' after 'Build one'. To run the program use
Product > Run or ⌘R.

Resources