'-'-'-'-'/'/'/'

S

sophia.agnes

Dear all,

I recently saw a program on the following lines

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

int main(void)
{

int i,j=0;

for(i=3;i>>j++;--i)
{

printf("%d\t",'-'<< j);
printf("%d\t",'-'-'/'<< j);
printf("%d\t",'-'-'-'/'/'<< j);
printf("%d\t",'-'-'-'-'/'/'/'<< j);

puts("");

}

printf("\n %d",'-');
printf("\n %d",'-'-'/');
printf("\n%d",'-'-'-'/'/');

puts("");
return(EXIT_SUCCESS);

}

when i ran the program(using gcc compiler) i got the o/p as
o/p as:-

90 -4 90 -2
180 -8 180 -4

45
-2
45
-1
now my question is how the following values are
evaluated ?


'-'-'/';
'-'-'-'/'/';
'-'-'-'-'/'/'/';

is it implementation dependent?
does it depend on endianess nature of machine?
 
U

user923005

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


int main(void)
{


int i,
j = 0;


for (i = 3; i >> j++; --i) {


printf("%d\t", '-' << j);
printf("%d\t", '-' - '/' << j);
printf("%d\t", '-' - '-' / '/' << j);
printf("%d\t", '-' - '-' - '/' / '/' << j);


puts("");


}


printf("\n %d", '-');
printf("\n %d", '-' - '/');
printf("\n%d", '-' - '-' / '/');


puts("");
return (EXIT_SUCCESS);
}
/*
Splint output:
foo.c: (in function main)
foo.c(13,23): Right operand of >> may be negative (int): i >> j++
The right operand to a shift operator may be negative (behavior
undefined).
(Use -shiftnegative to inhibit warning)
foo.c(13,17): Left operand of >> may be negative (int): i >> j++
The left operand to a shift operator may be negative (behavior is
implementation-defined). (Use -shiftimplementation to inhibit
warning)
foo.c(13,17): Test expression for for not boolean, type int: i >> j++
Test expression type is not boolean or int. (Use -predboolint to
inhibit
warning)
foo.c(16,31): Right operand of << may be negative (int): '-' << j
foo.c(16,24): Left operand of << may be negative (char): '-' << j
foo.c(16,24): Format argument 1 to printf (%d) expects int gets char:
'-' << j
To make char and int types equivalent, use +charint.
foo.c(16,18): Corresponding format code
foo.c(17,37): Right operand of << may be negative (int): '-' - '/' <<
j
foo.c(17,24): Left operand of << may be negative (char): '-' - '/' <<
j
foo.c(17,24): Format argument 1 to printf (%d) expects int gets char:
'-' - '/' << j
foo.c(17,18): Corresponding format code
foo.c(18,43): Right operand of << may be negative (int): '-' - '-' /
'/' << j
foo.c(18,24): Left operand of << may be negative (char): '-' - '-' /
'/' << j
foo.c(18,24): Format argument 1 to printf (%d) expects int gets char:
'-' - '-' / '/' << j
foo.c(18,18): Corresponding format code
foo.c(19,49): Right operand of << may be negative (int):
'-' - '-' - '/' / '/' << j
foo.c(19,24): Left operand of << may be negative (char):
'-' - '-' - '/' / '/' << j
foo.c(19,24): Format argument 1 to printf (%d) expects int gets char:
'-' - '-' - '/' / '/' << j
foo.c(19,18): Corresponding format code
foo.c(22,9): Return value (type int) ignored: puts("")
Result returned by function call is not used. If this is intended,
can cast
result to (void) to eliminate message. (Use -retvalint to inhibit
warning)
foo.c(28,21): Format argument 1 to printf (%d) expects int gets char:
'-'
A character constant is used as an int. Use +charintliteral to allow
character constants to be used as ints. (This is safe since the
actual type
of a char constant is int.)
foo.c(28,17): Corresponding format code
foo.c(29,21): Format argument 1 to printf (%d) expects int gets char:
'-' - '/'
foo.c(29,17): Corresponding format code
foo.c(30,20): Format argument 1 to printf (%d) expects int gets char:
'-' - '-' / '/'
foo.c(30,16): Corresponding format code
foo.c(33,5): Return value (type int) ignored: puts("")


Lint output:

--- Module: foo.c (C)
_
for (i = 3; i >> j++; --i) {
foo.c(13) : Info 702: Shift right of signed quantity (int)
_
printf("%d\t", '-' - '/' << j);
foo.c(17) : Info 701: Shift left of signed quantity (int)
foo.c(17) : Warning 504: Unusual shift operation (left side
unparenthesized)
_
printf("%d\t", '-' - '-' / '/' << j);
foo.c(18) : Warning 504: Unusual shift operation (left side
unparenthesized)
_
printf("%d\t", '-' - '-' - '/' / '/' << j);
foo.c(19) : Info 834: Operator '-' followed by operator '-' is
confusing. Use
parentheses.
foo.c(19) : Info 778: Constant expression evaluates to 0 in operation
'-'
foo.c(19) : Info 701: Shift left of signed quantity (int)
foo.c(19) : Warning 504: Unusual shift operation (left side
unparenthesized)

*/

/*
The expression:
'-' - '-'
is zero, of course, and so it is not very interesting.
*/
 
B

Bart van Ingen Schenau

Dear all,

I recently saw a program on the following lines
now my question is how the following values are
evaluated ?

'-'-'/';

This is equivalent to
'-' - '/' ;
The value depends on the character coding that is used and is therefor
implementation defined.
'-'-'-'/'/';

This is equivalent to
'-' - ('-' / '/') ;
The value depends on the character coding that is used and is therefor
implementation defined.
'-'-'-'-'/'/'/';

This is equivalent to
('-' - '-') - ('/' / '/') ;

Regardless of the character coding, the value must be -1.
is it implementation dependent?

For most of them, yes.
does it depend on endianess nature of machine?

No, it depends on the character coding that is used (e.g. ASCII or
EBCDIC)

Bart v Ingen Schenau
 
J

James Kuyper

Dear all,

I recently saw a program on the following lines ....
printf("%d\t",'-'<< j);
printf("%d\t",'-'-'/'<< j);
printf("%d\t",'-'-'-'/'/'<< j);
printf("%d\t",'-'-'-'-'/'/'/'<< j); ....
printf("\n %d",'-');
printf("\n %d",'-'-'/');
printf("\n%d",'-'-'-'/'/'); ....
now my question is how the following values are
evaluated ?

It will be clearer, I hope, with some additional white space, and the
following definitions:

char dash = '-';
char fslash = '/';
'-' - '/';
dash - fslash;
'-'-'-'/'/';
'-' - '-' / '/';
dash - dash / fslash;
'-'-'-'-'/'/'/';
'-' - '-' - '/ ' / '/';
dash - dash - fslash/fslash;

Please note that all of your examples are written as full statements
(they end with a ';') which which calculate a value but do nothing with
it. That's not how the corresponding expressions in the code you quoted
were written. The <<j in the first four printfs will change the value
displayed.

is it implementation dependent?
Yes.


does it depend on endianess nature of machine?

No. It depends upon the implementation-defined values of the '-' and '/'
characters, it depends upon whether or not plain char is signed or
unsigned, and it depends upon the relationship between INT_MAX and
CHAR_MAX. However, changing the endianess of int would have no effect on
the results.
 
P

pete

now my question is how the following values are
evaluated ?

'-'-'/';
'-'-'-'/'/';
'-'-'-'-'/'/'/';

is it implementation dependent?

Yes.

Here's one that isn't:

/* BEGIN new.c */

#include <stdio.h>

#define ZERO (-('-'-'-')/'/'/'/'-('-'-'-'))

int main (void)
{
puts("hello world" + ZERO);
return ZERO;
}

/* END new.c */
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top