I can't change variable value outside functions.
this code below throws up an error
#include <stdio.h>
#include <string.h>
struct student{
unsigned int age:3;
};
struct student student1;
student1.age=8;/*this line*/
int main( ) {
student1.age = 4;
printf( "student1.age : %d\n", student1.age );
student1.age = 7;
printf( "student1.age : %d\n", student1.age );
student1.age=5;
printf( "student1.age : %d\n", student1.age );
return 0;
}
this doesn't throw error
#include <stdio.h>
#include <string.h>
struct student{
unsigned int age:3;
};
struct student student1;
/*student1.age=8;this line*/
int main( ) {
student1.age = 4;
printf( "student1.age : %d\n", student1.age );
student1.age = 7;
printf( "student1.age : %d\n", student1.age );
student1.age=5;
printf( "student1.age : %d\n", student1.age );
return 0;
}
please explain why. Also, outside a function, it doesn't allow for me to change global variable's value once it's defined.