Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Vaxuun

macrumors member
Original poster
Feb 6, 2010
35
0
Denmark
Whenever I am trying to build and run this program, the log just keeps saying "Running..." and does not print out anything

The program
Code:
#include <stdio.h>

/* count digits, white space, others */
main()
{
	int c, i, nwhite, nother;
	int ndigit[10];
	
	nwhite = nother = 0;
	for (i = 0; i < 10; ++i)
		ndigit[i] = 0;
	
	while ((c = getchar()) != EOF)
		if (c >= '0' && c <= '9')
			++ndigit[c-'0'];
		else if (c == ' ' || c == '\n' || c == '\t')
			++nwhite;
		else
			++nother;
	
	printf("digits =");
	for (i = 0; i < 10; ++i)
		printf(" %d", ndigit[i]);
	printf(", white space = %d, other = %d\n",
		   nwhite, nother);
}

The log after Build and Run
Code:
[Session started at 2010-02-07 17:49:54 +0100.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1346) (Fri Sep 18 20:40:51 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".
tty /dev/ttys001
Loading program into debugger…
Program loaded.
run
[Switching to process 78644]
Running…

2up9qn6.png


28jv6km.png


Could someone also try to Build and Run the code? If this is a bug/error, how to correct it? Should I reinstall Xcode?

P.S. For project, I used Command Line Tool, under Application
 
You are, of course, typing something for getchar to return somehow? I don't know if you can do that on the XCode console. I think you may need to run the executable in Terminal...
 
I think you may need to run the executable in Terminal...

What should I type for doing it in Terminal?

Code:
gcc io.c -o io
doesn't work...

I heard that gcc comes with Xcode and I have that installed.. I also said yes to get all packages when I had to install it
 
You need to be more descriptive. What doesn't work? What is the error. And I said run the executable: not compile in Terminal (which brings me to your thread title: I see no issue with XCodes compiling here at all).
 
You need to be more descriptive. What doesn't work? What is the error. And I said run the executable: not compile in Terminal (which brings me to your thread title: I see no issue with XCodes compiling here at all).

When I drag and drop the main.c into Terminal and hit Enter, I get the following:

3522gxh.png


Doing something wrong? How can I get permission?
 
You are trying to tell Terminal to execute a .c file. That's the source code. It can't be executed. You need to compile as release in XCode and the execute the output file in Terminal
 
You are trying to tell Terminal to execute a .c file. That's the source code. It can't be executed. You need to compile as release in XCode and the execute the output file in Terminal

What's the output file? Where to find it? How should I run it?

Am I overseeing something, am I just stupid or is this more advanced?
 
The output file is the executable created by the compiler. Where it is will depend on your configuration. What it's called will depend on your project configuration. You could compile it using gcc on the command line and specify the output there.
 
The output file is the executable created by the compiler. Where it is will depend on your configuration. What it's called will depend on your project configuration. You could compile it using gcc on the command line and specify the output there.

Is it possible you could do new project, copy the project/program and show me how to launch in Terminal? Because I have no idea about all of this.. I don't know what the output file is?

I would really appreciate if you would do that for me

EDIT:
I may have found the executable:
2akif6r.png


but my Terminal says this:
bgxugi.png
 
Have you considered reading the documentation? It would tell you most of these things. Anyway expand Targets on the source list, double click the target that you are building and look under the Build tab. It will tell you where Build Products are put.
 
Just copied it.

I think you might find that getchar() doesn't work too well on OS X for some reason. I had to get use a carriage return for it to pick up the characters.

Also need to type ^D for the EOF character

Code:
$ gcc cTest.c -o cTest
$ ./cTest
c

digits = 0 0 0 0 0 0 0 0 0 0, white space = 0, other = 1
 
I think I finally compiled the main.c through Terminal!

It did create another file, but I can't execute it.

This is what I did in the Terminal:
Code:
$ gcc main.c -o main
$ ./main
c

If I try to execute directly by double-tap the new icon:
Code:
$ /Users/Lauritz/main ; exit;
 
Doing something wrong?

The two main things you're doing wrong are:

1. You're randomly flailing around trying things without knowing what they are.

2. You're not answering specific questions that have been asked of you by the people who are trying to help you.

The way to fix this is:

1. Stop randomly flailing.

2. Answer the questions.


In post #4, you were asked this:

What doesn't work? What is the error.

You then proceeded to quote that post in your response, and completely ignore the question, instead trying some other random thing that also didn't work because you didn't understand what it meant.

This approach won't work.

You must be systematic.

Programming is all about being systematic.
 
This is what I did in the Terminal:
Code:
$ gcc main.c -o main
$ ./main
c

If I try to execute directly by double-tap the new icon:
Code:
$ /Users/Lauritz/main ; exit;

Double-clicking the new icon won't work.

The ./main command you executed is probably waiting for you to type things. Type them, then hold down the Control key and press D, which is the special key that means EOF in Terminal (control-D).
 
Type them, then hold down the Control key and press D, which is the special key that means EOF in Terminal (control-D).

:bow: Thanks! Finally it executed and ran the code! :bow:

I get the result
Code:
digits = 0 0 0 0 0 0 0 0 0 0, white space = 0, other = 2

but in the book, it is
Code:
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345

but thanks! That's what needed. The Control-D
 
I get the result
Code:
digits = 0 0 0 0 0 0 0 0 0 0, white space = 0, other = 2

but in the book, it is
Code:
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345

What do you think your output is telling you about what you typed?

What do you think the book's output is telling you about what the author typed?
 
What do you think your output is telling you about what you typed?

What do you think the book's output is telling you about what the author typed?

I don't know, I copied from book, line for line (it should be said it is an e-book, not paperback) but you can see pilotError got other = 1, I got other = 2..

Code:
$ gcc cTest.c -o cTest
$ ./cTest
c

digits = 0 0 0 0 0 0 0 0 0 0, white space = 0, other = 1
 
I don't know, I copied from book, line for line (it should be said it is an e-book, not paperback) but you can see pilotError got other = 1, I got other = 2..

And this is why I always end up saying copy and pasting is not programming and does not teach anyone anything. Before you try and run the code you should understand what it says and know what you expect to happen (not by reading sample output in a book but by logically following the code). Go back, read the bits of the book before the code and ensure you understand every single statement in the code before you try and run it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.