10 Replies - 382 Views - Last Post: 26 January 2013 - 07:50 AM Rate Topic: ***-- 2 Votes

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

No Semicolon Printing

Posted 23 January 2013 - 11:40 PM

So my instructor has a penchant for asking tricky questions, last day he asked how to print Hello, World in C without typing a single semicolon. There are several answers to this like the if/while/switch statements or printing inside the argument of the main function. However one answer was way different, he talked about stealing a semicolon from a header file. Here's the code:
int main(int ac, char **av)
{
#define typedef
#define uint8_t a[printf("Hello, World\n")]
#include <stdint.h>
}


Can you Please explain me how this works? I know uint8_t is a byte representation but still I have no clue. I also read that this doesn't work on FreeBSD. Why so?
Thanks.

This post has been edited by Jeet.in: 25 January 2013 - 10:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: No Semicolon Printing

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,393
  • Joined: 25-December 09

Re: No Semicolon Printing

Posted 24 January 2013 - 07:11 AM

I really have no idea how this might work, nor do I have any desire to figure it out. In my opinion you would be better off learning how to properly construct C/C++ programs and leave the "parlor tricks" to your "brilliant" instructor.

Jim
Was This Post Helpful? 4
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: No Semicolon Printing

Posted 24 January 2013 - 07:17 AM

Thanks Jim.

Quote

leave the "parlor tricks" to your "brilliant" instructor

Hearing this from an expert is very appreciated. He is always onto those tricky questions that are present all over the internet ! But my credit points are in his hands :'( so I keep him as pleased as possible..

This post has been edited by Jeet.in: 24 January 2013 - 07:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: No Semicolon Printing

Posted 24 January 2013 - 04:15 PM

I second jimblumberg. These party tricks serve no real purpose and would probably just distract and confuse people learning a language. However, I'll try to explain it the best I can.

#define typedef

This will make the compiler effectively remove any occurrences of the name, "typedef", from any preceding code. More accurately, the compiler will insert exactly nothing in place of all occurrences of "typedef".

#define uint8_t a[printf("Hello, World\n")]

After this line, the compiler will replace any occurrence of "uint8_t" with "a[printf("Hello, World\n")]".

#include <stdint.h>

This inserts the header file "stdint.h", which contains lots of declarations, including a typedef for "uint8_t".

So, one of the resulting lines that we get is as follows.

unsigned char a[printf("Hello, World\n")]


Since GCC supports variable length arrays, this creates an array the size of the value returned by printf. There is some obscure part of an included header which inserts the semi-colon. If you compile the code as gcc -E main.c, you can see the preprocessed file output to stdout, which will give some clues. Here it is.

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.c"
int main(int ac, char **av)
{


# 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stdint.h" 1 3 4


# 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 1 3 4
# 24 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 3 4
# 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stddef.h" 1 3 4
# 324 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stddef.h" 3 4
 short unsigned int wchar_t;
# 353 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stddef.h" 3 4
 short unsigned int wint_t;
# 25 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 2 3 4


 signed char int8_t;
 unsigned char a[printf("Hello, World\n")];
 short int16_t;
 unsigned short uint16_t;
 int int32_t;
 unsigned uint32_t;
 long long int64_t;
 unsigned long long uint64_t;


 signed char int_least8_t;
 unsigned char uint_least8_t;
 short int_least16_t;
 unsigned short uint_least16_t;
 int int_least32_t;
 unsigned uint_least32_t;
 long long int_least64_t;
 unsigned long long uint_least64_t;





 signed char int_fast8_t;
 unsigned char uint_fast8_t;
 short int_fast16_t;
 unsigned short uint_fast16_t;
 int int_fast32_t;
 unsigned int uint_fast32_t;
 long long int_fast64_t;
 unsigned long long uint_fast64_t;
# 66 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 3 4
  int intptr_t;
# 75 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 3 4
  unsigned int uintptr_t;




 long long intmax_t;
 unsigned long long uintmax_t;
# 4 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stdint.h" 2 3 4
# 6 "main.c" 2
}


This post has been edited by Aphex19: 24 January 2013 - 08:13 PM

Was This Post Helpful? 3
  • +
  • -

#5 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: No Semicolon Printing

Posted 24 January 2013 - 11:01 PM

View PostAphex19, on 25 January 2013 - 04:45 AM, said:

I second jimblumberg. These party tricks serve no real purpose and would probably just distract and confuse people learning a language. However, I'll try to explain it the best I can.

#define typedef

This will make the compiler effectively remove any occurrences of the name, "typedef", from any preceding code. More accurately, the compiler will insert exactly nothing in place of all occurrences of "typedef".

#define uint8_t a[printf("Hello, World\n")]

After this line, the compiler will replace any occurrence of "uint8_t" with "a[printf("Hello, World\n")]".

#include <stdint.h>

This inserts the header file "stdint.h", which contains lots of declarations, including a typedef for "uint8_t".

So, one of the resulting lines that we get is as follows.

unsigned char a[printf("Hello, World\n")]


Since GCC supports variable length arrays, this creates an array the size of the value returned by printf. There is some obscure part of an included header which inserts the semi-colon. If you compile the code as gcc -E main.c, you can see the preprocessed file output to stdout, which will give some clues. Here it is.

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.c"
int main(int ac, char **av)
{


# 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stdint.h" 1 3 4


# 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 1 3 4
# 24 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 3 4
# 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stddef.h" 1 3 4
# 324 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stddef.h" 3 4
 short unsigned int wchar_t;
# 353 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stddef.h" 3 4
 short unsigned int wint_t;
# 25 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 2 3 4


 signed char int8_t;
 unsigned char a[printf("Hello, World\n")];
 short int16_t;
 unsigned short uint16_t;
 int int32_t;
 unsigned uint32_t;
 long long int64_t;
 unsigned long long uint64_t;


 signed char int_least8_t;
 unsigned char uint_least8_t;
 short int_least16_t;
 unsigned short uint_least16_t;
 int int_least32_t;
 unsigned uint_least32_t;
 long long int_least64_t;
 unsigned long long uint_least64_t;





 signed char int_fast8_t;
 unsigned char uint_fast8_t;
 short int_fast16_t;
 unsigned short uint_fast16_t;
 int int_fast32_t;
 unsigned int uint_fast32_t;
 long long int_fast64_t;
 unsigned long long uint_fast64_t;
# 66 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 3 4
  int intptr_t;
# 75 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/../../../../include/stdint.h" 3 4
  unsigned int uintptr_t;




 long long intmax_t;
 unsigned long long uintmax_t;
# 4 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.6.2/include/stdint.h" 2 3 4
# 6 "main.c" 2
}



Thank you very much Aphex19 for the explanation. Now I know what's happening. Even I know these party tricks doesn't mean much, but my college instructor insists :( and I have to abide for scoring marks.
Was This Post Helpful? 0
  • +
  • -

#6 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: No Semicolon Printing

Posted 25 January 2013 - 04:18 PM

View PostJeet.in, on 25 January 2013 - 06:01 AM, said:

Thank you very much Aphex19 for the explanation. Now I know what's happening. Even I know these party tricks doesn't mean much, but my college instructor insists :(/>/>/>/>/>/> and I have to abide for scoring marks.


Is this task graded? If so, does it count towards your final mark? If it does, I highly disagree with your lecturer's methods. However, if it doesn't, I actually see no real harm in optional tasks like this. It's obscure, but it forces you to look in depth in to the way GCC pre-processes source files, which is very useful.

Incidentally, if you can figure out where the heck that semi-colon comes from, I'd be a wiser person for it. I have tried to figure it out for a while and I'm lost. Honestly, if anyone can clue me in on that I'd appreciate it.

This post has been edited by Aphex19: 25 January 2013 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: No Semicolon Printing

Posted 25 January 2013 - 11:00 PM

View PostAphex19, on 26 January 2013 - 04:48 AM, said:

View PostJeet.in, on 25 January 2013 - 06:01 AM, said:

Thank you very much Aphex19 for the explanation. Now I know what's happening. Even I know these party tricks doesn't mean much, but my college instructor insists :(/>/>/>/>/>/>/>/>/> and I have to abide for scoring marks.


Is this task graded? If so, does it count towards your final mark? If it does, I highly disagree with your lecturer's methods. However, if it doesn't, I actually see no real harm in optional tasks like this. It's obscure, but it forces you to look in depth in to the way GCC pre-processes source files, which is very useful.

Incidentally, if you can figure out where the heck that semi-colon comes from, I'd be a wiser person for it. I have tried to figure it out for a while and I'm lost. Honestly, if anyone can clue me in on that I'd appreciate it.


It actually counts in my end of year grades :( ! After Googling I found this answer was posted on StackOverflow website(I am sure that's where he got the answer from !) but even there no explanation has been provided ! And my friends are also stuck in the same boat as me ! Anyway thanks Aphex19 for your help.

This post has been edited by Jeet.in: 25 January 2013 - 11:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: No Semicolon Printing

Posted 26 January 2013 - 06:07 AM

Ah, but it is completely unfair challenge to students who are using a compiler that actually adheres to the C++ standard, but does not have variable length arrays.
Was This Post Helpful? 0
  • +
  • -

#9 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: No Semicolon Printing

Posted 26 January 2013 - 07:13 AM

View PostSkydiver, on 26 January 2013 - 06:37 PM, said:

Ah, but it is completely unfair challenge to students who are using a compiler that actually adheres to the C++ standard, but does not have variable length arrays.

Good point ! Some of my fellow mates still use Turbo C on their XP machines !
Was This Post Helpful? 0
  • +
  • -

#10 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: No Semicolon Printing

Posted 26 January 2013 - 07:48 AM

View PostJeet.in, on 26 January 2013 - 02:13 PM, said:

View PostSkydiver, on 26 January 2013 - 06:37 PM, said:

Ah, but it is completely unfair challenge to students who are using a compiler that actually adheres to the C++ standard, but does not have variable length arrays.

Good point ! Some of my fellow mates still use Turbo C on their XP machines !



I think they are in need of an upgrade. Not that I'm against the use of Windows XP... but Turbo C?


This is a odd question to be marked for... requires research for it and deep digging.. I can't see this being on an exam?

This post has been edited by BlueMelon: 26 January 2013 - 07:49 AM

Was This Post Helpful? 0
  • +
  • -

#11 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: No Semicolon Printing

Posted 26 January 2013 - 07:50 AM

View PostBlueMelon, on 26 January 2013 - 08:18 PM, said:

View PostJeet.in, on 26 January 2013 - 02:13 PM, said:

View PostSkydiver, on 26 January 2013 - 06:37 PM, said:

Ah, but it is completely unfair challenge to students who are using a compiler that actually adheres to the C++ standard, but does not have variable length arrays.

Good point ! Some of my fellow mates still use Turbo C on their XP machines !



I think they are in need of an upgrade. Not that I'm against the use of Windows XP... but Turbo C?


This is a odd question to be marked for... requires research for it and deep digging.. I can't see this being on an exam?


This is not in an exam, but is an assignment !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1