1

On running the below code, it stucks after displaying the argv[0], argv[1] and argv[2] line. Further flow of code is blocked at this point, can any one help why it is stopping its execution or is it entering into an infinite loop.

#include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include "p8log.h" #include <errno.h> int main(int argc, char* argv[]) { char* PORT; char* IPADDR; printf("Arg Count=%d\n",argc); printf("Arguments are=%s,%s,%s\n",argv[0],argv[1],argv[2]); printf("HELLO"); PORT=argv[1], printf("WORLD"); IPADDR=argv[2]; printf("START"); printf("port num=%s",PORT); printf("IP ADDR=%s",IPADDR); printf("END"); /* some algorithm of calculation */ return 0; } 

Execution

./file-exe 11111 127.0.0.1 

Output

Arg Count=3 Arguments are=./file-exe,11111,127.0.0.1 
10
  • 1
    The title is C++ but the tag is C. Which language do you use? Commented Mar 3, 2016 at 7:11
  • Apparently there is no control of flow; it is unlikely that the shown code causes an infinite loop. What is the output so far? Is the part where END is written to the output ever reached? Commented Mar 3, 2016 at 7:13
  • 2
    Add \n to all printf's. Post the input you enter, then the output you see. Commented Mar 3, 2016 at 7:16
  • 1
    For me it works fine: $ ./a.exe 11111 127.0.0.1 Arg Count=3 Arguments are=C:\tmp\a.exe,11111,127.0.0.1 HELLOWORLDSTARTport num=11111IP ADDR=127.0.0.1END Commented Mar 3, 2016 at 7:23
  • 2
    P.S. After the OP edit, crystal ball says it's stuck in the /* some algorithm of calculation */ code that was not posted. Commented Mar 3, 2016 at 7:23

1 Answer 1

1

fflush(NULL); is good to do after any output, if you want to make sure it prints to screen. printf is buffered, so it can get lost.

./a.out 11111 127.0.0.1 Arg Count=3 Arguments are=./a.out,11111,127.0.0.1 HELLO WORLD START port num=11111 IP ADDR=127.0.0.1 END 

works fine, you needed some \n to break up lines, like so..

#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char* argv[]) { char* PORT; char* IPADDR; printf("Arg Count=%d\n",argc); printf("Arguments are=%s,%s,%s\n",argv[0],argv[1],argv[2]); printf("HELLO\n"); PORT=argv[1], printf("WORLD\n"); IPADDR=argv[2]; printf("START\n"); printf("port num=%s\n",PORT); printf("IP ADDR=%s\n",IPADDR); printf("END\n"); fflush(NULL); /* some algorithm of calculation */ return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.