How to declare at beginning of program

自作多情 提交于 2019-12-02 10:22:43
r.x = 150;

This is not a declaration, nor a definition, but an assignment.

C does not allow assignments on global level.

You still could define a variable at global scope

#include <SDL2/SDL.h>

SDL_Rect r;

int main (int argc, char** argv) {

Every variable defined globally undergoes a default initialisation:

  • integers variables are set to 0.
  • floating point variables are set to 0..
  • pointer variables are set to NULL.

Even more you could also initialise it explicitly

#include <SDL2/SDL.h>

SDL_Rect r = {1, 2, 3, 4};

int main (int argc, char** argv) {

Although an initialisation looks similar to an assignment it is not the same (as you already observed).

More on the difference between assignment and initialisation here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!