please help with C


 
Thread Tools Search this Thread
Top Forums Programming please help with C
# 1  
Old 02-05-2011
please help with C

I have a C test next week,so someone please help me in understanding the output of following 2 codes

1.
Code:
#define T t 
void main() 
{ 
char T = `T`; 
printf("
%c %c
",T,t); 
}

the output is T T, but since a macro is defined, i thought it would be t t


2.
Code:
void main() 
{ 
int a=0; 
int b=0; 
++a == 0 || ++b == 11; 
printf("
%d,%d",a,b); 
}

the output is 1 1 but the condition is false, so why this output?

Last edited by Scott; 02-05-2011 at 12:15 PM.. Reason: Please use code tags
# 2  
Old 02-05-2011
For question number one:

T is substituted by t in the code-part so

PHP Code:
char T = `T`; 
becomes
PHP Code:
char t = `T`; 
and

PHP Code:
printf("%c %c",T,t); 
becomes
PHP Code:
printf("%c %c",t,t); 
Then I hope it becomes obvious.


For question two there is no real condition test to care about as I see it.
++a makes a = 1.. tested against the value 11 but the result ain't used anywhere.
Same for ++b == 11 as I see it.

The change scottn suggested makes no difference as I tested it either.

Guess the style of coding should be avoided if we have no idea what it's about Smilie
# 3  
Old 02-05-2011
You're right, I made another change to the code. It didn't seem to make any sense after I wrote it, but before I could fix it, you replied. You were too quick Smilie
# 4  
Old 02-07-2011
Quote:
Originally Posted by ra2000
the output is T T, but since a macro is defined, i thought it would be t t
It seems you were expecting 'T' to get transformed into 't'. This doesn't happen because nothing inside a character constant or string constant is changed by the preprocessor.


Quote:
Originally Posted by ra2000
the output is 1 1 but the condition is false, so why this output?
The condition "a = 0; ++a == 0" is false - but remember the increment will still happen. Then, because the first condition is false, the second one needs to be tested to see if the whole statement is true or false. Whatever the truth value of the second part, the increment to b still happens because it's evaluated.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question