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. Advertising

  2. Anarki

    Ian Collins Guest

    Anarki wrote:
    > The following is the program i am trying to compile
    >
    > //restrict.c
    > #include <stdio.h>
    > int main()
    > {
    > char arr[10] = "Qualifiers"


    Missing semicolon.

    --
    Ian Collins.
     
    Ian Collins, Oct 1, 2008
    #2
    1. Advertising

  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

    On Oct 1, 1:31 pm, MisterE <> wrote:
    > >    char arr[10] = "Qualifiers"

    >
    > you are missing ;
    > try:
    > char arr[10] = "Qualifiers";


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

    Anarki Guest

    On Oct 1, 1:31 pm, Ian Collins <> wrote:
    > Anarki wrote:
    > > The following is the program i am trying to compile

    >
    > > //restrict.c
    > > #include <stdio.h>
    > > int main()
    > > {
    > >    char arr[10] = "Qualifiers"

    >
    > Missing semicolon.
    >
    > --
    > Ian Collins.


    Thx to Ian Collins and MisterE
     
    Anarki, Oct 1, 2008
    #5
  6. Anarki wrote:
    > The following is the program i am trying to compile
    >
    > //restrict.c
    > #include <stdio.h>
    > int main()


    Better: int main(void)

    > {
    > char arr[10] = "Qualifiers"


    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.

    > 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??


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

    James Kuyper Guest

    Joachim Schmitz wrote:
    > Anarki wrote:
    >> The following is the program i am trying to compile
    >>
    >> //restrict.c
    >> #include <stdio.h>
    >> int main()

    >
    > Better: int main(void)
    >
    >> {
    >> char arr[10] = "Qualifiers"

    >
    > 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'.


    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?

    > You could automate that by using arr[] = "Qualifiers"; but then may have to
    > adjust your for loop.
    >
    >> char * restrict p = arr;
    >> int i = 0;
    >> for(; i < 10; ++i)
    >> printf("%c", arr);
    >> return 0;
    >> }
     
    James Kuyper, Oct 1, 2008
    #7
  8. James Kuyper wrote:
    > Joachim Schmitz wrote:
    >> Anarki wrote:

    <snip>
    >>> char arr[10] = "Qualifiers"

    >>
    >> 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'.

    >
    > 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?


    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

    Joachim Schmitz wrote:
    > James Kuyper wrote:
    >> Joachim Schmitz wrote:
    >>> Anarki wrote:

    > <snip>
    >>>> char arr[10] = "Qualifiers"
    >>> 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'.

    >> 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?

    >
    > 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?


    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. James Kuyper wrote:
    > Joachim Schmitz wrote:
    >> James Kuyper wrote:
    >>> Joachim Schmitz wrote:
    >>>> Anarki wrote:

    >> <snip>
    >>>>> char arr[10] = "Qualifiers"
    >>>> 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'.
    >>> 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?

    >>
    >> 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?

    >
    > 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.


    OK, point taken.

    Bye, Jojo
     
    Joachim Schmitz, Oct 1, 2008
    #10
  11. Anarki wrote:
    > 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??


    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. Anarki wrote:

    > char arr[10] = "Qualifiers"

    ^^^
    missing ';', creating an error before
    > char * restrict p = arr;

    ^^^^
    'char'. This is what your diagnostic mesage says:

    > restrict.c:6: error: parse error before "char"


    > can someone help me how to compile this program??


    Add the semi-colon. No magic invocation of the compiler will make your
    errors disappear.
     
    Martin Ambuhl, Oct 1, 2008
    #12
  13. Joachim Schmitz wrote:
    > Anarki wrote:
    >> The following is the program i am trying to compile
    >>
    >> //restrict.c
    >> #include <stdio.h>
    >> int main()

    >
    > Better: int main(void)
    >
    >> {
    >> char arr[10] = "Qualifiers"

    >
    > Others told you about the missig ; here, but as yet nobody mentioned that
    > arr[10] is too short to hold the string "Qualifiers".


    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.

    > You need arr[11] to
    > have enough space for the terminating '\0'.


    Only if he needs a string, which he does not.

    > You could automate that by using arr[] = "Qualifiers"; but then may have to
    > adjust your for loop.


    There is nothing in the loop needing adjusting.

    >> char * restrict p = arr;
    >> int i = 0;
    >> for(; i < 10; ++i)
    >> printf("%c", arr);
    >> return 0;
    >> }


    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. Martin Ambuhl wrote:
    > Joachim Schmitz wrote:
    >> Anarki wrote:
    >>> The following is the program i am trying to compile
    >>>
    >>> //restrict.c
    >>> #include <stdio.h>
    >>> int main()

    >>
    >> Better: int main(void)
    >>
    >>> {
    >>> char arr[10] = "Qualifiers"

    >>
    >> Others told you about the missig ; here, but as yet nobody mentioned
    >> that arr[10] is too short to hold the string "Qualifiers".

    >
    > 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.
    >
    >> You need arr[11] to
    >> have enough space for the terminating '\0'.

    >
    > Only if he needs a string, which he does not.


    And all that has been said some 4 hours ago, by James Kuyper...

    >> You could automate that by using arr[] = "Qualifiers"; but then may
    >> have to adjust your for loop.

    >
    > There is nothing in the loop needing adjusting.


    Not in this particular case, but I said 'may', didn't I?

    >>> char * restrict p = arr;
    >>> int i = 0;
    >>> for(; i < 10; ++i)
    >>> printf("%c", arr);
    >>> return 0;
    >>> }

    >
    > If you wanted to pick nits,


    I did not, but apparently you do

    > 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".


    This problem wasn't the OP's, but it certainly isn't imaginary.

    Bye, Jojo
     
    Joachim Schmitz, Oct 1, 2008
    #14
  15. August Karlstrom <> writes:
    > Anarki wrote:
    >> 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??

    >
    > 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;
    > }


    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 (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 1, 2008
    #15
  16. Keith Thompson wrote:
    > August Karlstrom <> writes:

    [...]
    >> 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;
    >> }

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


    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.

    > 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.


    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. Anarki <> wrote:
    >
    > //restrict.c


    All this illustrates is a declaration using restrict.
    It doesn't show any practical value stemming therefrom.

    > #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;
    > }


    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
     
    Peter Nilsson, Oct 1, 2008
    #17
  18. Anarki

    Bartc Guest

    "Martin Ambuhl" <> wrote in message
    news:gc03oe$jcm$...
    > Anarki wrote:
    >
    >> char arr[10] = "Qualifiers"

    > ^^^
    > missing ';', creating an error before
    >> char * restrict p = arr;

    > ^^^^
    > 'char'. This is what your diagnostic mesage says:
    >
    >> restrict.c:6: error: parse error before "char"

    >
    >> can someone help me how to compile this program??

    >
    > Add the semi-colon. No magic invocation of the compiler will make your
    > errors disappear.


    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
     
    Bartc, Oct 2, 2008
    #18
  19. Anarki

    arnuld Guest

    > On Wed, 01 Oct 2008 21:31:11 +1300, Ian Collins wrote:

    >> Anarki wrote:
    >> The following is the program i am trying to compile


    >> //restrict.c
    >> #include <stdio.h>
    >> int main()
    >> {
    >> char arr[10] = "Qualifiers"



    > Missing semicolon.



    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.







    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
    Gooogle Groups is Blocked. Reason: Excessive Spamming
     
    arnuld, Oct 2, 2008
    #19
  20. Anarki

    Ian Collins Guest

    arnuld 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]$
    >
    > 2nd, -ansi flag emits warning of nested functions. I don't get it.
    >

    It's probably spitting the dummy over "restrict" which is a C99 keyword.

    --
    Ian Collins.
     
    Ian Collins, Oct 2, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CPERL520335
    Replies:
    1
    Views:
    375
    Sam Holden
    Jul 8, 2003
  2. Janne Naukkarinen
    Replies:
    0
    Views:
    384
    Janne Naukkarinen
    Jan 14, 2004
  3. Berehem
    Replies:
    4
    Views:
    592
    Lawrence Kirby
    Apr 28, 2005
  4. Replies:
    5
    Views:
    316
    Michael DOUBEZ
    Dec 18, 2006
  5. Nagaraj
    Replies:
    1
    Views:
    913
    Lionel B
    Mar 1, 2007
Loading...

Share This Page