can someone help me how to compile this program??

A

Anarki

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

Ian Collins

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

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

Missing semicolon.
 
M

MisterE

char arr[10] = "Qualifiers"

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

Joachim Schmitz

Anarki said:
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
 
J

James Kuyper

Joachim said:
Anarki said:
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;
}
 
J

Joachim Schmitz

James said:
Joachim said:
Anarki wrote:
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
 
J

James Kuyper

Joachim said:
James said:
Joachim said:
Anarki wrote:
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.
 
J

Joachim Schmitz

James said:
Joachim said:
James said:
Joachim Schmitz wrote:
Anarki wrote:
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
 
A

August Karlstrom

Anarki said:
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
 
M

Martin Ambuhl

Joachim said:
Anarki said:
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".
 
J

Joachim Schmitz

Martin said:
Joachim said:
Anarki said:
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
 
K

Keith Thompson

August Karlstrom said:
Anarki said:
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.
 
A

August Karlstrom

Keith said:
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
 
P

Peter Nilsson

Anarki said:
//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);
 
B

Bartc

Martin Ambuhl said:
Anarki said:
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.
 
A

arnuld

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

Ian Collins

arnuld said:
#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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top