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.
Related
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.
I can't get gdb to redirect a file to the stdin of my program.
Outside of gdb ./prog.x < someinput.txt does exactly what I want.
Inside gdb, when I run (gdb) run < someinput.txt my program doesn't seem to "see" the input file, and instead stdin seems to be reading what I type into the console.
(gdb) run < testinput.txt
Starting program: /Users/alexpatch/Documents/krc/misc/sandbox.x < testinput.txt
[New Thread 0x1603 of process 8308]
My cursor appears on a blank line beneath that, where I can type some text. C-d breaks out of that, then the output of the program run with what I just typed appears, and the process exits normally.
/* trivial code example */
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
return 0;
}
I found a few answers related to similar issues, but the solution that seemed to work for them was what I'm already doing, namely (gdb) run < someinput.txt. In case it's helpful, I'm working on a 64bit MacBook Air running macOS Sierra.
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.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,N;
for(i=0;i<5;i++)
{
printf("Enter The Number \n");
scanf("%d", &N);
printf("HELLO %d \n",N);
}
return 0;
}
When i execute the above code in the NetBeans then the output is not being executed line by line all the output is executed together i.e once when the loop end...The problem is printf and scanf are not working
Your description of the problem is actually quite good:
Many C Runtime libraries can detect whether stdout is connected to an interactive device (console window / terminal) or not.
Depending on that, the default buffering mode is selected.
Execute the program on a terminal / in a console window, and you get the standard buffering for interactive devices instead.
Alternatively, calling
setvbuf(stdout, 0, _IOLBUF, BUFSIZ);
before any other operations on that stream will set the stdout stream to default line-buffered operation.
I am using Eclipse Indigo CDT and just running simple code which is:
#include <stdio.h>
void main()
{
int num;
printf("enter no\n");
scanf("%d",&num);
printf("no is %d\n",num);
}
Opuput:
55
enter no
no is 55
But when I run this code it won't print enter no. Instead of that it waits to enter the number. After pressing some number it is printing enter no. What could be the reason?
That would depend on the flush scheme of standard out.
Traditionally, stdout is line buffered when connected to a terminal and page buffered when it's not connected to a terminal.
Apparantly, when you run your program in eclipse, standard out is page buffered.
You can overcome this by flushing stdout:
#include <stdio.h>
void main()
{
int num;
printf("enter no\n");
fflush(stdout);
scanf("%d",&num);
printf("no is %d\n",num);
}
It is good practice to flush a file handle whenever you expect the recipient of the information to respond. That way you can be sure that the recipient gets everything you have written regardless of the buffering of the file handle.