On 07/28/2011 07:30 AM, naga raj wrote:
Hi Mike,
Post the code which actually failed, after you have verified the failure.
Following is the code which has some problem with getchar() function.
#include<stdio.h>
volatile int i,j=0;
int main()
{
char buf[10];
xil_printf("main\n");
while(j<6)
{
i=getchar();
xil_printf("input value of i is:%c\n",i);
buf[j]=i;
xil_printf("value of j is:%d\n",j);
j++;
}
xil_printf("buffer contains:%s\n",buf);
return 0;
}
I have given input as abc
.
I have observed following output:
main
input value of i is:a ---------->my input ( I have not
pressed any key after entering 'a')
value of j is:0
input value of i is: -------------> It is automatically
skipping to take my input
value of j is:1
input value of i is:b ---------->my input ( I have not
pressed any key after entering 'b')
value of j is:2
input value of i is: -------------> It is automatically
skipping to take my input
value of j is:3
input value of i is:c ---------->my input ( I have not
pressed any key after entering 'c')
value of j is:4
input value of i is: -------------> It is automatically
skipping to take my input
value of j is:5
buffer contains: a
If we observe it is waiting for user input when the value of variable
j is 0, 2 and 4.
I have given the input to the program using HyperTerminal. I have
entered only abc as my input.
Look at the assembly generated by gcc. Where is j incremented?
I have observed the code generated for j++ it is correct.
Please look into the below objdump code.
j++;
880002c8: b0008800 imm -30720
880002cc: e8603d88 lwi r3, r0, 15752
880002d0: 30630001 addik r3, r3, 1
880002d4: b0008800 imm -30720
880002d8: f8603d88 swi r3, r0, 15752
Why do you think that this is a problem in getchar()?
With the above observation I am thinking that getchar() function is
appending a null character after each character.
Can you please give me some inputs so that I can proceed further.
Thanks in advance,
Nagaraju
Attach gdb to your process and put a breakpoint in the loop. printf()
is not the best way to debug problems.
--jeff