C Questions

S

santosh

when we put -pedantic switch we are complying with c89 standards isn't
it??

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

Old Wolf

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

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

Old Wolf

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

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.

True, but the OP for some reason didn't believe it was a compiler bug.
He appeared to be saying that it couldn't be a compiler bug because he
was using that particular compiler. I was probably reading too much
into his comment, particularly given that English does not appear to
be his first language.
 
A

aarklon

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
 
K

Keith Thompson

okay this is what i have done

cat 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);

puts("");
return(EXIT_SUCCESS);
}

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

puts("");
return(EXIT_SUCCESS);
}

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

Philip Potter

okay this is what i have done

cat check.c

#include<stdio.h>
#include<stdlib.h>


int main(void)
{
int i= -1;
-i;

This line does nothing.
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;

This line does nothing.
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

The only difference between the two programs is that you have changed
one line that does nothing to another line that does nothing. Therefore
it's only expected that the two programs have identical output.

Phil
 
S

santosh

okay this is what i have done

cat check.c

#include<stdio.h>
#include<stdlib.h>


int main(void)
{
int i= -1;
-i;

You are discarding the result of this operation. What did you want to do
with '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

What are you trying to do?
 
A

aarklon

(e-mail address removed) writes:

[...]
okay this is what i have done
cat 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"

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

Philip Potter

(e-mail address removed) writes:

[...]
okay this is what i have done
cat 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);
puts("");
return(EXIT_SUCCESS);
}
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);
puts("");
return(EXIT_SUCCESS);
}
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"

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

Let me give some examples using binary +:

x = y + z; /* assigns y + z to x. y and z are unchanged. */
y + z; /* adds y to z and discards the result. y and z are unchanged. */

Similarly:

x = -y; /* assigns -y to x. y is unchanged. */
-y; /* calculates the negative of y and discards it.
y is unchanged. */
y = -y; /* negates y. */

unary + and -, like their binary counterparts, do not change the value
of their operands. You need an assignment operator (=, +=, -=, etc) to
change a variable (or more generally, an lvalue).

(The ++ and -- operators also change their operands, but this is a
special case.)
 
R

Richard Heathfield

(e-mail address removed) said:

so what exactly is the use of unary operators???

The unary operators (many of which use the same glyphs as binary operators)
are:

! ~ ++ -- + - * & (type) sizeof

(I don't think I've missed any.)

! yields 0 if its (numeric!) operand is non-zero, and 1 otherwise. This is
used in circumstances where the "truth" or "falsity" of an expression
needs to be inverted. For example, given an object 'ok' which determines
whether a prior operation succeeded, we might take corrective action
conditionally on if(!valid).

~ does much the same trick as !, but effectively works on bits rather than
larger numeric values. ~0 yields a whole bunch of set bits. This can be
useful in masking operations.

++ increments its scalar operand, which may be placed either before or
after it. The result of the operation (which will be either the operand's
prior value or the value it will have after incrementation) depends on the
placement of the operand. -- is symmetrical to ++, but it decrements
instead of incrementing. Both of these operators are very commonly used in
loops, but they have other uses too.

+ yields the numeric value of its operand. Its presence seems to be purely
for symmetry with -, and I can think of no practical use for it.

- yields the result of subtracting its operand from 0. It is useful
wherever such a subtraction is useful. It is easiest to see its usefulness
by observing what we would have to write if we didn't have it. The
statement:

i = -1;

would have instead to be written as:

i = 0 - 1;

which is rather a wordy way to assign a negative value to i, is it not?

* yields the value of the object or function pointed to by its operand.
This is phenomenally useful in C. All kinds of dynamic data structures
depend on the ability to "get at" the value of an object pointed to by a
pointer.

& yields the address of its operand, a non-register-qualified object. This
is how we get a pointer to an object. One obvious use is when we need to
provide a function with the capability of modifying an object's value.

(type), the cast operator, converts its operand's value to another type.
This is almost useless, but there are specific occasions when it can be of
benefit. See http://www.cpax.org.uk/prg/writings/casting.php for a more
detailed discussion.

sizeof is tricky to describe accurately, so let's allow the Standard to do
it for us:

The sizeof operator yields the size (in bytes) of its operand, which may be
an expression or the parenthesized name of a type. The size is determined
from the type of the operand, which is not itself evaluated. The result
is an integer constant.

This is useful when we need to know how big an object is. For example, if
we want to write the object representation of an object to a stream open
for output, we can do this: fwrite(&obj, sizeof obj, 1, fp);

HTH. HAND.
 
P

pete

Richard Heathfield wrote:
! 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.
 
R

Richard Heathfield

pete said:
! yields 0 if its scalar operand does not compare equal to zero,
and 1 otherwise.

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

Flash Gordon

Richard Heathfield wrote, On 05/11/07 11:24:
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.

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

pete

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.

A type conversion is required for a pointer
to be compared to zero,
and that's the difference between "compares" and "is".


Some more musings concerning the difference between
"compares" and "is":

(0) compares equal to (0u)
(-1) compares greater than (0u)
(0) compares greater than (-1)

Is (-1) greater than or less than (0u)?
 
R

Richard Heathfield

Flash Gordon said:
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.

Good pont. :)
 
?

=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=

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.

No, the -ansi (or as you say, -std=c89) and -pedantic options are _both_
required for gcc to attempt to conform to C89. The -ansi flag makes sure
valid programs are accepted, and the -pedantic flag makes sure invalid
programs are diagnosed. If either option is omitted, the compiler, while
perhaps useful, isn't conforming to C89/C90.

You can use see from these examples:

int main(int argc, char *argv[]) {
int typeof; /* valid declaration, must be accepted */
return 0;
}

int main(int argc, char *argv[]) {
int array[argc+1]; /* a diagnostic is required in C90 */
return 0;
}
 

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

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top