rando rando - 10 days ago 7
C Question

C program skips first char in string

Works fine except skips first character in string name. Can anyone explain what I am not seeing?

#include "stdio.h"
/*
*BN
*/
int main(void) {
// Disable stdout buffering
//setvbuf(stdout, NULL, _IONBF, 0);

struct data {
char name[40];
char street[50];
char city[30];
char state[3];
char zip[6];
float salary;
};

struct data p = {
scanf("%s %s %s %s %s %f", &p.name, &p.street, &p.city, &p.state, &p.zip, &p.salary)
};

printf(" Name:%s\n Street:%s\n City:%s\n State:%s\n Zipcode:%s\n Salary:%.2f", p.name, p.street, p.city, p.state, p.zip, p.salary);
return 0;
}

Answer

Initialize your structure as struct data p; then you do the scanf.

struct data p;
scanf("%s%s%s%s%s%f", &p.name, &p.street, &p.city, &p.state, &p.zip, &p.salary);

You can check some struct stuff here.