C/JAVA:
int sum = 0;
for(int i=0;i<3;i++)
sum+=sum++;
Now print the value of sum
Answer...
C:
7
Java:
0
Why?...
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Thursday, June 05, 2008
Tuesday, November 06, 2007
infront or infront of back
sounds crazy right, i could not come up with a sexy title for (pre or post increment operators in c), anyways today the issue of pre/post increment operators in c, sagar and i had a good 15 minute discussion how this works so he results are here
int i=5;
printf("%d,%d,%d",++i, i, i++);
/*
output: 7,7,5
*/
how, we understood that printf evaluates from right to left, so it'll do i++ first and return '5' and update value of i i.e. 6 in stack, the middle i is just i, the first ++i, will increment value of i in stack i.e. it becomes 7, now it'll examine the value of stack finds i=7, therefore it substitutes 7 as first and second argument
int i=5;
printf("%d,%d,%d",++i, i, ++i);
/*
output: 7,7,7
*/
how, we understood that printf evaluates from right to left, so it'll do ++i in stack i.e. 6 in stack, the middle i is just i, the first ++i, will increment value of i in stack i.e. it becomes 7, now it'll examine the value of stack finds i=7, therefore it substitutes 7 as first and second argument
because in post increment operator, the assignment happens before increment, therefore in case one 5 is assigned or returned, and get incremented on stack.
fair enough understanding i think.
refer:-
int i=5;
printf("%d,%d,%d",++i, i, i++);
/*
output: 7,7,5
*/
how, we understood that printf evaluates from right to left, so it'll do i++ first and return '5' and update value of i i.e. 6 in stack, the middle i is just i, the first ++i, will increment value of i in stack i.e. it becomes 7, now it'll examine the value of stack finds i=7, therefore it substitutes 7 as first and second argument
int i=5;
printf("%d,%d,%d",++i, i, ++i);
/*
output: 7,7,7
*/
how, we understood that printf evaluates from right to left, so it'll do ++i in stack i.e. 6 in stack, the middle i is just i, the first ++i, will increment value of i in stack i.e. it becomes 7, now it'll examine the value of stack finds i=7, therefore it substitutes 7 as first and second argument
because in post increment operator, the assignment happens before increment, therefore in case one 5 is assigned or returned, and get incremented on stack.
fair enough understanding i think.
refer:-
- http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixPost=171881
- http://gcc.gnu.org/ml/gcc-help/2006-10/msg00174.html
Blogged with Flock
Monday, November 05, 2007
Nurses or NCurses
the first time i saw this package was during Fedora Core 1 installation, a nurses package... what a funny name or is it really for nurses, but i forgot about it.
next comes kernel compilation for Advanced OS class, where make menuconfig reports some error in ubuntu.. after some digging someone found out that we have to install libncurses-dev package for it to work, and i remembered that funny incident that happened 3 years before.
the assignment questions seemed simple
so i want to add interactivity to my shell , that is history, command completion etc.. , so i have to capture keystrokes, but what shit, ours is not a complete shell, so the shell that we run on didn't give us the keycodes that we expect .. after a lot of blah blah .. nurses or ncurses came to resuce, there is a getch() function will capture all keys if we set keypad(stdsrc,TRUE), done...
worked for three more days to "port".. when every thing is working .. Hmm this is not what i wanted SEGMENTATION FAULT, why the hell!!, Hmm if do chdir() there is a seg fault, i got bugged of curses so had to revert back to non-interactive shell, but i can upgrade my proc code to work as a task manager (like top command) again 2 days of work, i could implement killing a process provided you have sufficient access rights, ya i know it has lot of flaws but i believe it is a "proof of concept" , now ask me do something in ncurses, i can do it with a bit of struggle.
next is a file manager in queue .. (long live ncurses ).
food for thought: try "tpp" the ncurses based based presentation program, ultimate, so decided to torture people with "tpp" (available in ubuntu/debian repos)
next comes kernel compilation for Advanced OS class, where make menuconfig reports some error in ubuntu.. after some digging someone found out that we have to install libncurses-dev package for it to work, and i remembered that funny incident that happened 3 years before.
the assignment questions seemed simple
- Observing “Linux“ behavior“ by writing a program to use the /proc mechanism to inspect various kernel values
- Learn how to write a UNIX—style shell program
so i want to add interactivity to my shell , that is history, command completion etc.. , so i have to capture keystrokes, but what shit, ours is not a complete shell, so the shell that we run on didn't give us the keycodes that we expect .. after a lot of blah blah .. nurses or ncurses came to resuce, there is a getch() function will capture all keys if we set keypad(stdsrc,TRUE), done...
worked for three more days to "port".. when every thing is working .. Hmm this is not what i wanted SEGMENTATION FAULT, why the hell!!, Hmm if do chdir() there is a seg fault, i got bugged of curses so had to revert back to non-interactive shell, but i can upgrade my proc code to work as a task manager (like top command) again 2 days of work, i could implement killing a process provided you have sufficient access rights, ya i know it has lot of flaws but i believe it is a "proof of concept" , now ask me do something in ncurses, i can do it with a bit of struggle.
next is a file manager in queue .. (long live ncurses ).
food for thought: try "tpp" the ncurses based based presentation program, ultimate, so decided to torture people with "tpp" (available in ubuntu/debian repos)
Blogged with Flock
Wednesday, October 10, 2007
How do i Flush!!
Most programs i write suffer from either stdout or stdin not getting empty when it is supposed to (when using Eclipse CDT, explicit flush id required for me to print anything).
while i can flush stdout using fflush(stdout);, i cant do the same thing with stdin, this was creating a major headache for me, and i came across a site which gave a code snippet to clear the buffer
int ch;
while( (ch = fgetc(fp)) != EOF && ch != '\n' )
/* null body */;
while i can flush stdout using fflush(stdout);, i cant do the same thing with stdin, this was creating a major headache for me, and i came across a site which gave a code snippet to clear the buffer
int ch;
while( (ch = fgetc(fp)) != EOF && ch != '\n' )
/* null body */;
Blogged with Flock
Where is my Pointer
being a TA, its my job to help the students when they encounter any problem in implementation or tools used for their projects, when Joseph said that his program is receiving SIGSEGV, i thought it must be some simple problem either the index of array is not proper or memory not allocated, so i started to debug and Voila! the program works just fine giving correct output.
we all were wondering what is wrong, and eclipse(CDT) was the first target, but debugging in eclipse also worked fine, and now we decided to go manual line by line checking so few things are observed
now its time for more digging, so i prefer to user -Wall option to clean and make my code perfect, there i noticed one warning, function returns reference to local pointer, voila got it... so changed the function a bit, instead of returning a pointer we passed a pointer to the function and worked on it, and the result SUCCESS.
it shouldn't have taken much time for me to find out, being an OS student, we always deal with functions(system calls) which take pointer as an argument instead of returning them, so there must be something wrong/bad in returning the reference to local pointer, the memory gets lost!!!. but why not in debugging, the only reason that came to my mind is when debugging the memory areas used by the program is locked till it is freed or the program ends.
we all were wondering what is wrong, and eclipse(CDT) was the first target, but debugging in eclipse also worked fine, and now we decided to go manual line by line checking so few things are observed
- they wanted a 2D array for which they know that the column size is two and row size should be dynamically allocated
- Array of pointers were used
now its time for more digging, so i prefer to user -Wall option to clean and make my code perfect, there i noticed one warning, function returns reference to local pointer, voila got it... so changed the function a bit, instead of returning a pointer we passed a pointer to the function and worked on it, and the result SUCCESS.
it shouldn't have taken much time for me to find out, being an OS student, we always deal with functions(system calls) which take pointer as an argument instead of returning them, so there must be something wrong/bad in returning the reference to local pointer, the memory gets lost!!!. but why not in debugging, the only reason that came to my mind is when debugging the memory areas used by the program is locked till it is freed or the program ends.
Blogged with Flock
Subscribe to:
Posts (Atom)