Hello world!
DUFF'S DEVICE
Self Replicating program
C in 2 pages
Whereami
Undocumented C
Frequently needed codes (freshers) :)
Hello World !
#include
char scode[] = "\x31\xd2\x31\xc9\x31\xdb\x31\xc0"
"\x50\x6a\x31\x6a\x79\x68\x2f\x2f"
"\x74\x74\x68\x2f\x64\x65\x76\xb1"
"\x02\x89\xe3\xb0\x05\xcd\x80\x89"
"\xc3\x31\xc0\x50\x6a\x0a\x68\x72"
"\x6c\x64\x21\x68\x6f\x20\x57\x6f"
"\x68\x48\x65\x6c\x6c\xb2\x0d\x89"
"\xe1\xb0\x04\xcd\x80\x31\xdb\x89"
"\xd8\x40\xcd\x80";
int main(void)
{
int * ptr;
ptr = (int *)&ptr + 2;
(*ptr) = (int)scode;
}
DUFF'S DEVICE
register n = (count + 7) / 8; /* count > 0 assumed */
switch (count % 8)
{
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
Self replicating program
char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,3
4,s,34);}
Whereami?
The following program Whereami.c won �Best Small Program� prize in The
International Obfuscated C Code Contest held in 1992. This program was by
Brian Westley
main(l
,a,n,d)char**a;{
for(d=atoi(a[1])/10*80-
atoi(a[2])/5-596;n="@NKA\
CLCCGZAAQBEAADAFaISADJABBA^\
SNLGAQABDAXIMBAACTBATAHDBAN\
ZcEMMCCCCAAhEIJFAEAAABAfHJE\
TBdFLDAANEfDNBPHdBcBBBEA_AL\
H E L L O, W O R L D! "
[l++-3];)for(;n-->64;)
putchar(!d+++33^
l&1);}
UnDocumented C
Refering the invalid or null value using * unairy operator;
eg. int *ptr;
ptr=NULL;
printf("%d",*ptr);
________________________
struct foo
{
unsigned int f : 2;
unsigned int g : 3;
unsigned int h : 1;
};
a compiler can place f in the most significant bits of the allocated storage, or in the least significant bits.
Suppose you'd like to compile this struct definition on multiple platforms and guarantee that f is always in the most significant bits of the allocated storage. You can create a header file, say platform.h, with definitions for platform-specific characteristics that you can redefine for each platform. For a target platform that places the first bit-field in the most significant bits, platform.h might contain:
#define FIRST_BITFIELD_IN_MSB 1
For a target platform that places the first bit-field in the least significant bits, platform.h might contain:
#define FIRST_BITFIELD_IN_LSB 1
Then you can write a portable definition for the struct as:
struct foo
{
#if defined FIRST_BITFIELD_IN_MSB
unsigned int f : 2;
unsigned int g : 3;
unsigned int h : 1;
#elif defined FIRST_BITFIELD_IN_LSB
unsigned int h : 1;
unsigned int g : 3;
unsigned int f : 2;
#else
#error unknown bitfield \
allocation policy
#endif
};
The Standard doesn't require compilers to allocate the first bit-field at one end or the other. It simply says that the allocation order is implementation-defined. Other allocation strategies are possible. Thus, this code tests for each known strategy and issues a compile-time error (via an #error directive) if the target doesn't use any of them.
Frequently needed codes (freshers):)
No of bits set in integer
_no_of_bits(int check)
{
int i=0;
for((check<0)?check*=-1:check;i+=(check&1),check>>=1;);
return i;
}
Finding the given number is power of two
#define ISPOWOF2( n ) ( ! ( n & ( n-1 ) )
|
|