How to zero a struct in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

typedef struct USERs {
	char name[32];
	int age;
	float salary;
} USER;

int main(void) {

	USER user;

	memset(&user, 0, sizeof(user));

	printf("user.name : %s\n", user.name);
	printf("user.age : %d\n", user.age);
	printf("user.salary : %.2f\n", user.salary);

	return 0;
}




/*
run:

user.name :
user.age : 0
user.salary : 0.00

*/

 



answered May 19, 2022 by avibootz
...