Executing a program
Ok, i'm a newbie in C but wanted to learn it. I bought a book. I wrote a program to raise to a number to a certain power. This is the code:
Then i compiled it. But to test it, i dont know how to do it.
int power (int m, int n);
/* test power function */
for ( i = 0; i < 10; ++i)
printf ("%d %d %d\n", i, power(2,i), power(-3,i));
/* power: raise base to n-th power; n >= 0 */
int power(int base, int n)
int i, p;
p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
I have the a.out so what should i do to execute the command and have a proper answer.
I tried ./a.out ./a.out(3,5) but it is always wrong.
Now, i can't go forward in learning as i don't know how to check a program.
Please help me
./a.out is the right way to do it (./ executes in your current dir). If the output is wrong, then it's because you've made a mistake in your program. This is my output:
And don't forget to declare main to return an int.
0 1 1
1 2 -3
2 4 9
3 8 -27
4 16 81
5 32 -243
6 64 729
7 128 -2187
8 256 6561
9 512 -19683
Perhaps you already knew this, but if you want to have a name for your executable besides a.out, you can use the -o flag like this:
gcc -o prog_name prog_name.c
Ok, I got that. Thanx, but i'm now having a problem with another program i tried. A program to count line
How do i get this one working. When i type ./a.out, it will keep on running, without outputing anything. Should i write a text file in the same folder and then type a different command? I just can't get the programme running.
/* count line in input */
int c, nl;
while((c = getchar()) !=EOF)
if(c == '\n')
Thanx for the help
Get rid of that green '1'
It doesn't change anything. I still don't know how i' supposed to run the program. And with ./a.out, it still keeps un running.
Do you know how to send EOF? (ctrl-d on a UNIX type machine)
No, i don't but i am using Linux
Yeah, typically you would send EOF with a ctrl-d key combination on your Linux machine, or if you were in Windows, I believe it is ctrl-z. When you are done inputting your lines, hit ctrl-d and your output should appear.
OK, but how do i know what file it is reading, and how do i make it read a file?
Ah well that is something different altogether than what your program is doing. getchar() is reading a byte at a time from stdin (in this case it would be your keyboard) - so when you execute your program, you have a blank new line - you type a word (or words), hit enter and then ctrl-d and your output should be 1. If you typed two lines, you would see a two and so on. If you want to read in a file, you could start your program in the console, and copy a file (highlight it to select it) and then paste it into the console - press enter, and then ctrl-d.
Ok, thanx, that was it....
Thanx a lot fore the help.....
Ok cool - are you working with Kernighan and Ritchies, The C Programming Language as a text book?
Yeah, i am, i've been told it was a good one.
It certainly is a decent book.
If you are stuck for answers on the exercises, you may find the following site helpful:
Of course you can always get help here too.