Executing a program

This is a discussion on Executing a program within the C Programming forums, part of the General Programming Boards category; Ok, i'm a newbie in C but wanted to learn it. I bought a book. I wrote a program to ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    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:

    Code:
    #include <stdio.h>
    
    int power (int m, int n);
    /* test power function */
    main()
    {
    	int i;
    
    	for ( i = 0; i < 10; ++i)
    		printf ("%d %d %d\n", i, power(2,i), power(-3,i));
    	return 0;
    }
    
    /* 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;
    	return p;
    }
    Then i compiled it. But to test it, i dont know how to do it.
    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
    Thanx
    Trovoada

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    ./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:
    Code:
    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
    And don't forget to declare main to return an int.
    Code:
    int main(void)

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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:

    Code:
    gcc -o prog_name prog_name.c

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Ok, I got that. Thanx, but i'm now having a problem with another program i tried. A program to count line

    Code:
    #include <stdio.h>
    
    /* count line in input */
    main()
    {
    	int c, nl;
    
    	nl=0;
    	while((c = getchar()) !=EOF)
    		if(c == '\n')
    			++nl;
    	printf("%d1\n", nl);
    }
    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.
    Thanx for the help
    Trovoada

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
    printf("%d1\n", nl);
    Get rid of that green '1'

    ~/

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    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.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Do you know how to send EOF? (ctrl-d on a UNIX type machine)
    Last edited by kermit; 07-04-2005 at 05:35 AM.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    No, i don't but i am using Linux

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    OK, but how do i know what file it is reading, and how do i make it read a file?

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Ok, thanx, that was it....
    Thanx a lot fore the help.....

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Ok cool - are you working with Kernighan and Ritchies, The C Programming Language as a text book?

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Yeah, i am, i've been told it was a good one.

  15. #15
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    It certainly is a decent book.

    If you are stuck for answers on the exercises, you may find the following site helpful:

    http://users.powernet.co.uk/eton/kandr2/

    Of course you can always get help here too.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. Executing a program
    By trancedeejay in forum C Programming
    Replies: 7
    Last Post: 03-06-2006, 07:55 AM
  4. Problem executing sample program:
    By mrabiu in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2004, 05:44 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21