can someone help me how to compile this program??

Discussion in 'C Programming' started by Anarki, Oct 1, 2008.

  1. Anarki

    Anarki Guest

    The following is the program i am trying to compile

    //restrict.c
    #include <stdio.h>
    int main()
    {
    char arr[10] = "Qualifiers"
    char * restrict p = arr;
    int i = 0;
    for(; i < 10; ++i)
    printf("%c", arr);
    return 0;
    }

    i am using cygwin in window xp SP2, my gcc version is 3.4.4

    how do i compile the above program?

    i tried using this

    gcc -c -std=c99 restrict.c

    but i get an error like this

    restrict.c: In function 'main':
    restrict.c:6: error: parse error before "char"

    can someone help me how to compile this program??
     
    Anarki, Oct 1, 2008
    #1
    1. Advertisements

  2. Anarki

    Ian Collins Guest

    Missing semicolon.
     
    Ian Collins, Oct 1, 2008
    #2
    1. Advertisements

  3. Anarki

    MisterE Guest

    char arr[10] = "Qualifiers"

    you are missing ;
    try:
    char arr[10] = "Qualifiers";
     
    MisterE, Oct 1, 2008
    #3
  4. Anarki

    Anarki Guest

    omg! i didnt see tht *** semicolon srry thread closed!
     
    Anarki, Oct 1, 2008
    #4
  5. Anarki

    Anarki Guest

    Thx to Ian Collins and MisterE
     
    Anarki, Oct 1, 2008
    #5
  6. Better: int main(void)
    Others told you about the missig ; here, but as yet nobody mentioned that
    arr[10] is too short to hold the string "Qualifiers". You need arr[11] to
    have enough space for the terminating '\0'.
    You could automate that by using arr[] = "Qualifiers"; but then may have to
    adjust your for loop.


    Bye, Jojo
     
    Joachim Schmitz, Oct 1, 2008
    #6
  7. Anarki

    James Kuyper Guest

    It's legal to use a string literal to initialize an array that is not
    big enough to hold the terminating '\0' (6.7.8p14). He does not use arr
    in any way that requires it, so why should he provide space to store it?
     
    James Kuyper, Oct 1, 2008
    #7
  8. It may be legal to do so and he might not use it in this program, but it is
    a common enough mistake to forget to allocate space for the terminating '\0'
    to make it worthwhile being mentioned, wouldn't you agree?

    Bye, Jojo
     
    Joachim Schmitz, Oct 1, 2008
    #8
  9. Anarki

    James Kuyper Guest

    Perhaps, but the mention should have been written in a way that
    acknowledges the possibility that the knew precisely what he was doing.
    The way you wrote it left the impression that his code was unambiguously
    wrong. You said "too short" and "you need", when it was precisely big
    enough for everything he was going to do with it, and he therefore did
    not need to make the change that you suggested.
     
    James Kuyper, Oct 1, 2008
    #9
  10. OK, point taken.

    Bye, Jojo
     
    Joachim Schmitz, Oct 1, 2008
    #10


  11. I would write the program as

    #include <stdio.h>

    int main(void)
    {
    char arr[] = "Qualifiers";
    int i;

    for (i = 0; arr != '\0'; i++) {
    printf("%c", arr);
    }
    printf("\n");

    return 0;
    }

    or simply

    #include <stdio.h>

    int main(void)
    {
    puts("Qualifiers");

    return 0;
    }


    August
     
    August Karlstrom, Oct 1, 2008
    #11
  12. ^^^
    missing ';', creating an error before
    ^^^^
    'char'. This is what your diagnostic mesage says:
    Add the semi-colon. No magic invocation of the compiler will make your
    errors disappear.
     
    Martin Ambuhl, Oct 1, 2008
    #12
  13. It is not too short for this initialization. It is true thar arr is an
    array of characters and not a string, but the program in no way assumes
    a string.
    Only if he needs a string, which he does not.
    There is nothing in the loop needing adjusting.


    If you wanted to pick nits, you should have pointed out the failure to
    end the last line of output with an end-of-line character, instead of an
    imaginary "problem".
     
    Martin Ambuhl, Oct 1, 2008
    #13
  14. And all that has been said some 4 hours ago, by James Kuyper...
    Not in this particular case, but I said 'may', didn't I?


    I did not, but apparently you do
    This problem wasn't the OP's, but it certainly isn't imaginary.

    Bye, Jojo
     
    Joachim Schmitz, Oct 1, 2008
    #14


  15. I wouldn't write a program for that at all; I'd type "echo Qualifiers"
    at my shell prompt.

    Any program that illustrates some particular point that's been trimmed
    down enough to post here can (probably) be transformed into a program
    with equivalent behavior that utterly fails to illustrate the original
    point. Which misses the point.
     
    Keith Thompson, Oct 1, 2008
    #15


  16. Of course, the second version was more of a joke. My point was to
    illustrate the automatic calculation of the needed array length and the
    comparison to the sentinel '\0' in the loop.
    Not if it's part of a real program that tries to achieve something. Even
    when I ask about more theoretical issues I usually try to make examples
    "minimal".


    August
     
    August Karlstrom, Oct 1, 2008
    #16
  17. All this illustrates is a declaration using restrict.
    It doesn't show any practical value stemming therefrom.


    You now know about the missing semicolon, but as an
    asside it's worth mentioning that the loop can be
    replaced with printf, e.g.

    printf("%.*s\n", 10, arr);
     
    Peter Nilsson, Oct 1, 2008
    #17
  18. Anarki

    Bartc Guest

    A mention of 'missing semicolon' from the compiler could help though.

    Imagine a few dozen lines of comments and/or conditional code between line 5
    and 6.
     
    Bartc, Oct 2, 2008
    #18
  19. Anarki

    arnuld Guest


    I rewrote his program just like "August" wrote:



    #include <stdio.h>


    int main(void)
    {
    char arr[] = "Qualifiers";
    char* restrict p = arr;
    int i;


    for( i = 0; arr; ++i)
    {
    printf("%c", arr);
    }

    printf("\n");

    return 0;
    }

    ================= COMPILE ===================

    [arnuld@dune ztest]$ gcc -std=c99 -pedantic -Wall -Wextra restrict.c
    restrict.c: In function `main':
    restrict.c:7: warning: unused variable `p'
    [arnuld@dune ztest]$

    [arnuld@dune ztest]$ gcc -ansi -pedantic -Wall -Wextra restrict.c
    restrict.c: In function `main':
    restrict.c:7: warning: ISO C forbids nested functions
    restrict.c:7: error: syntax error before "p"
    [arnuld@dune ztest]$



    Then I wonder at the compilation warning of -std=c99. Why OP used p at
    all?

    2nd, -ansi flag emits warning of nested functions. I don't get it.
     
    arnuld, Oct 2, 2008
    #19
  20. Anarki

    Ian Collins Guest


    It's probably spitting the dummy over "restrict" which is a C99 keyword.
     
    Ian Collins, Oct 2, 2008
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.