0
Why this code has error
#include <stdio.h> struct students { char name; char address; int roll; int number; }pavel,shoun,anik; int main() { struct students pavel={'pavei','krishnager',44,96}; //pavel.name = 'pavel mukhergi'; //pavel.adress ='krishnager'; //pavel.roll = 44; //pavel.number = 9674180546; printf ("The name of pavel is %s\n The adress of pavel is %s\n The roll of pavel is %d\n The number of pavel is %d",pavel.name,pavel.address,pavel.roll,pavel.number); return 0; }
1 Answer
0
1. <name> and <address> field must be defined as `char` array. As it is in the code, they can only accept a single character.
2. Use C-String literal when assigning value for <name> and <address>. C-String are wrapped in double quotes e.g. "SoloLearn". Use of single quotes are for `char` literals e.g. a single character like 'G'
3. Need to use `unsigned long` type for the <number>. `int` doesn't support large integer value such as 9674180546 (causes integer overflow).
#include <stdio.h>
struct students
{
char name[ 25 ];
char address[ 25 ];
int roll;
unsigned long number;
} pavel, shoun, anik;
int main()
{
struct students pavel = { "pavel mukhergi", "krishnager", 44, 9674180546UL };
printf ( "The name of pavel is %s\nThe adress of pavel is %s\nThe roll of pavel is %d\nThe number of pavel is %lu\n", pavel.name, pavel.address, pavel.roll, pavel.number );
return 0;
}