Hello laginagesh
The key issue is the expect script exit status. Easier to investigate that at the command line than in C. When it is working OK then call it from the C program.
Test your expect script like this
Bash sets $? to the exit status of the last command.
You will get more help if you attract expect experts by putting expect in the title of your post, maybe "How can I set expect script exit status?". You could start a new thread.
BTW1 please use CODE tags around your code (that's CODE and /CODE in [] brackets -- or you can go into Advanced posting mode and use the # icon).
BTW2 it is safer to give the full path to ./test.sh in your C, rather than assume the compiled C program is being run in the directory containing test.sh.
BTW3 the file name extension .sh is conventionally used to indicate a shell script. Maybe clearer to call the expect script test.expect?
Best
Charles