C aptitude

Programming, for all ages and all languages.
LongHorn

C aptitude

Post by LongHorn »

Next week going to attend C aps. Could somebody post some C aps questions (with answers and explanations). Something that you find interesting and odd.
Let me be first. May be its known to everyone.
main()
{
printf("Hello","World");
printf("Hello""World");
}
HelloHelloWorld
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C aptitude

Post by Solar »

"Interesting and odd" is in the eye of the beholder.

As for your example:

Code: Select all

printf( "Hello", "World" );
[tt]printf()[/tt] prints 0..n arguments under control of the format string passed as first argument. Arguments are converted to output using "conversion specifiers", which might be as simple as "%d" (print a decimal integer) and as complex as "%+12.6e" (print a floating point in exponent notation, right-aligned in a 12-char field, with a precision of 6 and a leading plus sign if not negative).

Your format string "Hello" does not contain any conversion specifier, resulting in the argument "World" being ignored.

As for the second, string constants are concatenated.

Sorry that I don't come up with somethign "strange" myself, but the "strangeness" that I do find lurks in the depths of C++ and linkers. ;)
Every good solution is obvious once you've found it.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C aptitude

Post by Pype.Clicker »

if _that_ confuses you, then what would it be with

Code: Select all

   int x = 5;
   if (x=6) printf("six");
   else printf("not six");
which does print ... "six"
and will likely to issue a warning, such as your ("hello", "world") will.

or even

Code: Select all

#include <stdio.h>

#define SIX 1 + 5
#define NINE 8 + 1

int main(void)
{
    printf( "What do you get if you multiply %d by %d? %d\n", SIX, NINE, SIX * NINE );
    return 0;
}
which does print ... 42 :P (that one is on wikipedia :P)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C aptitude

Post by Solar »

Pype.Clicker wrote: ...and will likely to issue a warning, such as your ("hello", "world") will.
If you run [tt]gcc -Wall[/tt]. If you don't use [tt]-Wall[/tt], don't use GCC. 8)
Every good solution is obvious once you've found it.
JoeKayzA

Re:C aptitude

Post by JoeKayzA »

@Pype: That's exactly why one is advised to always put brackets around #define'd statements that are longer than a single expression. ;)

cheers Joe
YeXo

Re:C aptitude

Post by YeXo »

Adding numbers to pointers always confuses me.

Code: Select all

long *ptr = (long*)0x10;
long a = 0x10;
ptr++;
a++;
ptr points to the value: 0x14
a contains the value: 0x11
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C aptitude

Post by Candy »

C89 or C99?

Code: Select all

int value = 42 + 12 //* divisor */ 3
   + 4;
What does this code print?
Schol-R-LEA

Re:C aptitude

Post by Schol-R-LEA »

Candy wrote: C89 or C99?

Code: Select all

int value = 42 + 12 //* divisor */ 3
   + 4;
What does this code print?
Not a damn thing ;)

However, if you meant "what would [tt]value[/tt] be after this assignment", then AFAIK the answer would be 58. Since the tokenizing rules for C and C++ (and ingrained in LEX and its descendants) follow the principle of 'continue to the largest possible token', the '//' after the 12 would be interpreted as a line comment, so everything else on that line is ignored.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C aptitude

Post by Solar »

...but in C89 there is no such thing as a line comment, so it would be (42 + 12 / 3 + 4) = 50...
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C aptitude

Post by Candy »

Solar wrote: ...but in C89 there is no such thing as a line comment, so it would be (42 + 12 / 3 + 4) = 50...
Which is exactly my point. If you compile this with a C99-capable compiler (tested with GCC), it won't complain and produce 58. If you compile this with a non-C99 capable compiler (didn't test) you should get 50, again without warning.

Now that's a bug.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C aptitude

Post by Solar »

Not a bug, two different languages. Now don't get me started about the stunts I will have to pull off in PDCLib to enable C89 / C99 compliance with a single flick of a switch... new printf / scanf converters, some subtle changes in semantics (like the % operator)... :o 8)
Every good solution is obvious once you've found it.
LongHorn

Re:C aptitude

Post by LongHorn »

@ pype: certainly i am not confused with that thing. Just want to show an example.
But once again you people proved your skill by talking about compiler compliance, c89, c99 etc. I am not expected to know abt c89, c99 etc such stuff by recruitment peoples in our campus. Just the basics.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C aptitude

Post by Solar »

Well, you better know that stuff when you start working on source in earnest. It's not some arcane knowledge to show of with on some internet forum, it's the medication for "strange errors headache"... ;D
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C aptitude

Post by Candy »

Solar wrote: Not a bug, two different languages. Now don't get me started about the stunts I will have to pull off in PDCLib to enable C89 / C99 compliance with a single flick of a switch... new printf / scanf converters, some subtle changes in semantics (like the % operator)... :o 8)
It's a "bug" of sorts in the newer compiler not to warn for the construct, because it's detectable and causes stuff not to port clearly from C89 to C99.
YeXo

Re:C aptitude

Post by YeXo »

Are there any (modern) compilers using C89?

From what you say I assume gcc (and any windows port of that) is using C99, is that correct?
Post Reply