I hope i got your problem right, suppose you want to open a TCP post (assuming your machine is a Linux machine) then you need the following command -
iptables -I INPUT -p tcp --dport 12345 --syn -j ACCEPT
Above command will open the TCP post with the port-id as 12345 on local machine, now what uyou need is to fire the same command from your C code may be system would be handy something like
system("iptables -I INPUT -p tcp --dport 12345 --syn -j ACCEPT")
You may need to provide full path of iptables based on your local setting.