Kiwi breeder
Kiwi breeder

Reputation: 617

C - What does this comma separated declaration mean

Apologies for the silly question, but I don't know how to find a reference for this. I found some code for a TCP server and this line is confusing me:

int master_sockfd, client_sockfds[3] = {0}, cli_sockfd, client_games_started[3];

None of the variables above have been initialized beforehand so it looks like it's being declared here. Can someone help me understand what this is saying? If I recall correctly {0} sets int to 0 but I can't understand it in this context. I'm also very confused on how there looks to be two declarations with three assignments. And yes, everything compiled without errors/warnings. Thanks!

Upvotes: 1

Views: 140

Answers (1)

Tim
Tim

Reputation: 4948

int master_sockfd, client_sockfds[3] = {0}, cli_sockfd, client_games_started[3];

is identical to:

int master_sockfd;
int client_sockfds[3] = {0};
int cli_sockfd;
int client_games_started[3];
  • master_sockfd and cli_sockfd are integers
  • client_sockfds is an array of 3 integers, all initialized to zero
  • client_games_started is an array of 3 integers

Upvotes: 5

Related Questions