A
aarklon
I also suggest the addition of '-Wall' '-W' and '-pedantic' options.
when we put -pedantic switch we are complying with c89 standards isn't
it??
I also suggest the addition of '-Wall' '-W' and '-pedantic' options.
when we put -pedantic switch we are complying with c89 standards isn't
it??
Shifting a negative number right invokes undefined behavior,
i don't thenk there is a compiler bug in it.I compiled the program as
follows:- gcc -ansi -g -o check check.c
"gcc version 4.0.3 (Ubuntu 4.0.3-1ubuntu5)"
I'm surprised and shocked that no one has pointed out the lack of the
newline - which of course makes the whole program invalid (and OT...)
The program is valid and OT, it is just that it
may not produce any output.
J. J. Farrell wrote, On 04/11/07 17:28:
The incorrect output suggests either a compiler bug or the OP not
posting the actual code used. I tried and gcc produced the correct
results at all optimisation level even without me correcting the program
by including stdio.h
Well, it allows the OP to see if s/he is testing using an older version
of gcc and thus helps in determining whether it is a bug that has been
fixed.
How is it that the format string "i = %d +i = %d"
results in a comma being output?
Also, how does the substring "-i = %d" result in
the output "-i =1", what happened to the space
before the %d ?
I suspect you have not reported your results accurately.
(And perhaps not reported the program correctly; why do
you write "+i = %d" and then give -i for the argument?)
okay this is what i have done
cat check.c
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i= -1;
-i;
printf("i = %d -i = %d",i,-i);
puts("");
return(EXIT_SUCCESS);
}
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
now i am changing unary - operator to unary +
zodiac@ubuntu:~$ cat check.c
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i= -1;
+i;
printf("i = %d -i = %d",i,-i);
puts("");
return(EXIT_SUCCESS);
}
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
okay this is what i have done
cat check.c
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i= -1;
-i;
printf("i = %d -i = %d",i,-i);
puts("");
return(EXIT_SUCCESS);
}
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
now i am changing unary - operator to unary +
zodiac@ubuntu:~$ cat check.c
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i= -1;
+i;
printf("i = %d -i = %d",i,-i);
puts("");
return(EXIT_SUCCESS);
}
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
okay this is what i have done
cat check.c
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i= -1;
-i;
printf("i = %d -i = %d",i,-i);
puts("");
return(EXIT_SUCCESS);
}
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
now i am changing unary - operator to unary +
zodiac@ubuntu:~$ cat check.c
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i= -1;
+i;
printf("i = %d -i = %d",i,-i);
puts("");
return(EXIT_SUCCESS);
}
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
(e-mail address removed) writes:
[...]
okay this is what i have donecat check.c#include<stdio.h>
#include<stdlib.h>
Style point: leave a space before the "<". The compiler doesn't care,
but it's easier to read.
int main(void)
{
int i= -1;
-i;
This does nothing. The expression ``-i'' (which has no side effects)
is evaluated, and the result is silently discarded.
printf("i = %d -i = %d",i,-i);
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
This is the result I'd expect.
now i am changing unary - operator to unary +
zodiac@ubuntu:~$ cat check.c#include<stdio.h>
#include<stdlib.h>
See above.
int main(void)
{
int i= -1;
+i;
This does nothing; see above.
printf("i = %d -i = %d",i,-i);
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
Again, this is exactly what I'd expect.
Did you expect different results? If so, what and why?
You've posted two programs and their output, but you haven't actually
posted a question. Did you have a question?
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
(e-mail address removed) writes:
[...]
Style point: leave a space before the "<". The compiler doesn't care,okay this is what i have done
cat check.c
#include<stdio.h>
#include<stdlib.h>
but it's easier to read.
This does nothing. The expression ``-i'' (which has no side effects)int main(void)
{
int i= -1;
-i;
is evaluated, and the result is silently discarded.
This is the result I'd expect.printf("i = %d -i = %d",i,-i);
puts("");
return(EXIT_SUCCESS);
}
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
See above.now i am changing unary - operator to unary +
zodiac@ubuntu:~$ cat check.c
#include<stdio.h>
#include<stdlib.h>
This does nothing; see above.int main(void)
{
int i= -1;
+i;
Again, this is exactly what I'd expect.printf("i = %d -i = %d",i,-i);
puts("");
return(EXIT_SUCCESS);
}
zodiac@ubuntu:~$ gcc -ansi -pedantic -g -o check check.c
zodiac@ubuntu:~$ ./check
i = -1 -i = 1
Did you expect different results? If so, what and why?
You've posted two programs and their output, but you haven't actually
posted a question. Did you have a question?
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
I just wanted to check this question which is given in a book.
all i wanted is to test unary + and unary - operators.
so what exactly is the use of unary operators???
so what exactly is the use of unary operators???
! yields 0 if its (numeric!) operand is non-zero, and 1 otherwise.
! yields 0 if its scalar operand does not compare equal to zero,
and 1 otherwise.
pete said:
Oh yes, you can do !p, can't you? I see that as being rather silly, but
you're quite right that it's legal. I'm not sure that I see the
distinction between "is non-zero" and "does not compare equal to zero". I
consider them to be equivalent statements.
Richard said:pete said:
Oh yes, you can do !p, can't you?
I see that as being rather silly, but
you're quite right that it's legal. I'm not sure that I see the
distinction between "is non-zero"
and "does not compare equal to zero". I
consider them to be equivalent statements.
Richard Heathfield wrote, On 05/11/07 11:24:
The difference is that a null pointer is not zero so talking about
whether pointers are zero or non-zero does not make sense. Ponters can,
on the other hand, be compared against zero.
No. The '-ansi' or '-std=c89' switches do that. The '-pedantic' switch
tells gcc to conform to the relevant language dialect as strictly as
possible.
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.