void DECIMAL_TO_HEXA( int num, char *hex )
{
int aux_hex[32],i=0;
char hexa[32], resu[32];
while(num>0) {
aux_hex[i]=num%16;
num/=16;
i++;
}
resu[0]='\0';
i--;
for(i;i>=0;i--) {
if (aux_hex[i]<10) {
sprintf(hexa, "%d", aux_hex[i]);
strcat(resu, hexa);
}
else if (aux_hex[i]==10) strcat(resu, "A");
else if (aux_hex[i]==11) strcat(resu, "B");
else if (aux_hex[i]==12) strcat(resu, "C");
else if (aux_hex[i]==13) strcat(resu, "D");
else if (aux_hex[i]==14) strcat(resu, "E");
else if (aux_hex[i]==15) strcat(resu, "F");
}
strcpy(hex, resu);
}
Friday, January 23, 2009
WTF #2
This must be work of some dark evil mastermind.
Thursday, November 27, 2008
C/C++ puzzler #2 - Is it typo ?
Ok, here is next C/C++ specific puzzler. Puzzler here is to guess, what will be result of program ?
Highlight text bellow code to find out correct answer.
I'll just show you program output:
Is it not, what you have expected ? Fine, let's walk the program together. On entry to main() program prints sizeof(user defined type name_t), which has length of 30 chars. Next it'll print sizeof(instance of name_t) which again is 30 chars long. Then it calls function fn(instance of name_t), In fn() it prints sizeof(user defined type name_t) which is again 30 bytes. But when it tries to print sizeof(value) it prints 4, because arrays are passed as reference to first element in C/C++. And the sizeof(pointer) is 4 respectively 8 for 64 bit system.
- It won't compile
- It will compile, but it will segfault when we try to run it
- It will run and print 4 times 30
- Anything else you can think of
Highlight text bellow code to find out correct answer.
/* compile with cc -g -o test_typedef_sizeof test_typedef_sizeof.c */
#include <stdio.h>
#include <string.h>
typedef char name_t[30];
void fn(name_t value)
{
printf("%d\n", sizeof(name_t));
printf("%d\n", sizeof(value));
}
int main (void)
{
name_t aa;
printf("%d\n", sizeof(name_t));
printf("%d\n", sizeof(aa));
fn(aa);
return 0;
}
I'll just show you program output:
30
30
30
4
Is it not, what you have expected ? Fine, let's walk the program together. On entry to main() program prints sizeof(user defined type name_t), which has length of 30 chars. Next it'll print sizeof(instance of name_t) which again is 30 chars long. Then it calls function fn(instance of name_t), In fn() it prints sizeof(user defined type name_t) which is again 30 bytes. But when it tries to print sizeof(value) it prints 4, because arrays are passed as reference to first element in C/C++. And the sizeof(pointer) is 4 respectively 8 for 64 bit system.
Java EE factsheets
Stefan Jäger recently posted bunch of Java EE fact-sheets on his blog. Stefan created fact-sheets for: SF and SL session beans, MDB's and JMS, Transactions and security, and finally JPA. Very nice, and available as PDF downloads.
Thursday, October 30, 2008
C/C++ puzzler #1 - Where do you want to go today ?
First puzzler is in C/C++ and it is based on puzzler found in book Java Puzzlers by Neal Gafter and Joshua Bloch.
Problem:
We have following C program:
Options:
What will the program do:
a) print firefox.exe
b) run firefox.exe open http://www.google.com in new tab
c) fail to compile
d) other
Solution: (highlight to see)
First look at source, and you probably think that this couldn't compile. In fact it does. Real puzzler is, why does it compile. Problem seems to be in line http://www.google.com; But let's take a closer look. Is it problem ? It compile because of long forgotten feature of C, which is labels. (Except for kernel source, you'll find plenty of labels there ;) What compiler see is label http: followed by comment //www.google.com;
Problem:
We have following C program:
/* compile with: cc -g -o test_xxx test_xxx.c */
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
printf("firefox.exe");
http://www.google.com;
printf(":new\n");
exit(0);
}
Options:
What will the program do:
a) print firefox.exe
b) run firefox.exe open http://www.google.com in new tab
c) fail to compile
d) other
Solution: (highlight to see)
First look at source, and you probably think that this couldn't compile. In fact it does. Real puzzler is, why does it compile. Problem seems to be in line http://www.google.com; But let's take a closer look. Is it problem ? It compile because of long forgotten feature of C, which is labels. (Except for kernel source, you'll find plenty of labels there ;) What compiler see is label http: followed by comment //www.google.com;
Puzzlers series
I guess everyone have been in situation when he/she has written code, which didn't behave as it should. Most of these times, problem was of PEBKAC (problem exists between keyboard and chair) type. Computers (nearly) always do, what they're told to do. Under influence of Java Puzzlers book by Neal Gafter and Joshua Bloch, I've decided to start series of posts to this blog, which would cover situations when program doesn't do what we think it should. At first posts will be mostly in C/C++, but i think more languages will be covered in the future. Posts will be formated, so that you'll see only problem with possible answers. Solution will have font color set to background color, or some close color, so you'd have to highlight block in order to see it. You're more than welcome to add your own puzzlers. Just contact me.
Thursday, October 16, 2008
Java Puzzlers
Oh my - oh my - oh my. Yet another funny presentation, this time by Joshua Bloch (Author of Effective Java) and Neal Gafter (Java Puzzlers: Traps, Pitfalls, and Corner Cases, closures proposal for Java7). Presentation is from JavaPolis 2007 and it's called Java Puzzlers/Overview. It's interesting even for non Java programmers.
Tuesday, September 23, 2008
Rules of API design
Joshua Bloch, author of Effective Java writes article about key principles of designing API's. However not only API developers should read this, because as first points states: "All programmers are API designers".
These principles are also mentioned in his original presentation: How to Design a Good API & Why it Matters.
These principles are also mentioned in his original presentation: How to Design a Good API & Why it Matters.
Subscribe to:
Posts (Atom)