当前位置:  开发笔记 > 编程语言 > 正文

在C中声明变量

如何解决《在C中声明变量》经验,为你挑选了2个好方法。

我很难在C中声明我的变量.编译器在令牌之前显示消息"预期的构造函数析构函数或类型转换."我做错了什么?

#include 

int count =0;
int abc;
ABC; 
a = 19, b = 27, c = 3;

a = 4 + 5 * 3;
b = (4 +5) * 3;
c = 25 -(2 * (10 + (8 / 2)));

main {
   printf("Enter a value please\n");
   scanf("%d, , \n");
   return 0;
}

John Bode.. 6

这是一个重写,显示如何解决各种问题:

#include 

int count = 0;     // declarations and intitializations are allowed at file
                   // scope (that is, outside of any function body).  The
                   // storage for count will be set aside at program start
                   // and held until the program terminates.  
                   //
                   // count is visible to all functions defined within this
                   // file, and will be visible to other files compiled and
                   // linked into the same program.  

int main( void ) { // Implicit typing of functions is no longer allowed as of C99,
                   // and it was never good practice to begin with. 
                   // IOW, the compiler will not assume a return type of
                   // int if a type specifier is missing.  main always
                   // returns int, and either takes 0 or 2 arguments.

  int a, b, c;     // int abc; declares a *single* variable named "abc", 
                   // not three variables named "a", "b", and "c".  
                   // Storage for these variables will be set aside at
                   // function entry and held until the function exits.
                   //
                   // None of a, b, or c are visible outside of the
                   // function body.            

                   // ABC has not been defined anywhere; it's not clear
                   // what the intent was behind ABC;

  a = 19;          // While not illegal, a = 19, b = 27, c = 3; is not the best style
  b = 27;          // in the world; it's better to make these three separate statements 
  c = 3;           // Note that assignment statements (like any other executable 
                   // statement) are not allowed outside of a function body.  


  a = 4 + 5 * 3;                   // fine
  b = (4 +5) * 3;                  // fine
  c = 25 -(2 * (10 + (8 / 2)));    // fine

  printf("Enter a value please\n"); // fine

  scanf("%d", &a); // Each conversion specifier in a scanf call needs a corresponding 
                   // argument to write the input value to, and the argument needs
                   // to be of the correct type.  %d expects the corresponding 
                   // argument to have type "int *" (pointer to int).  The expression
                   // "&a" gives the address of the variable "a", and the type 
                   // of the expression is "int *".  So, this statement will read an 
                   // integer value from standard input and store it to a.

  return 0;
}


Bathsheba.. 5

    你不能写a = 19, b = 27, c = 3;外部函数之类的赋值.

    int count = 0; 是全局变量的初始化,所以这是允许的.

    ABC;除非ABC已经#define数字化,否则也没有意义,即便如此,它也将是无操作的.

    main也是畸形的.您需要将其编写为int main()并确保返回值.

    最后,您的scanf参数列表不正确.请查阅文档.

研究C的介绍是个好主意.Kernighan&Ritchie是一本很好的书.



1> John Bode..:

这是一个重写,显示如何解决各种问题:

#include 

int count = 0;     // declarations and intitializations are allowed at file
                   // scope (that is, outside of any function body).  The
                   // storage for count will be set aside at program start
                   // and held until the program terminates.  
                   //
                   // count is visible to all functions defined within this
                   // file, and will be visible to other files compiled and
                   // linked into the same program.  

int main( void ) { // Implicit typing of functions is no longer allowed as of C99,
                   // and it was never good practice to begin with. 
                   // IOW, the compiler will not assume a return type of
                   // int if a type specifier is missing.  main always
                   // returns int, and either takes 0 or 2 arguments.

  int a, b, c;     // int abc; declares a *single* variable named "abc", 
                   // not three variables named "a", "b", and "c".  
                   // Storage for these variables will be set aside at
                   // function entry and held until the function exits.
                   //
                   // None of a, b, or c are visible outside of the
                   // function body.            

                   // ABC has not been defined anywhere; it's not clear
                   // what the intent was behind ABC;

  a = 19;          // While not illegal, a = 19, b = 27, c = 3; is not the best style
  b = 27;          // in the world; it's better to make these three separate statements 
  c = 3;           // Note that assignment statements (like any other executable 
                   // statement) are not allowed outside of a function body.  


  a = 4 + 5 * 3;                   // fine
  b = (4 +5) * 3;                  // fine
  c = 25 -(2 * (10 + (8 / 2)));    // fine

  printf("Enter a value please\n"); // fine

  scanf("%d", &a); // Each conversion specifier in a scanf call needs a corresponding 
                   // argument to write the input value to, and the argument needs
                   // to be of the correct type.  %d expects the corresponding 
                   // argument to have type "int *" (pointer to int).  The expression
                   // "&a" gives the address of the variable "a", and the type 
                   // of the expression is "int *".  So, this statement will read an 
                   // integer value from standard input and store it to a.

  return 0;
}



2> Bathsheba..:

    你不能写a = 19, b = 27, c = 3;外部函数之类的赋值.

    int count = 0; 是全局变量的初始化,所以这是允许的.

    ABC;除非ABC已经#define数字化,否则也没有意义,即便如此,它也将是无操作的.

    main也是畸形的.您需要将其编写为int main()并确保返回值.

    最后,您的scanf参数列表不正确.请查阅文档.

研究C的介绍是个好主意.Kernighan&Ritchie是一本很好的书.

推荐阅读
yzh148448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有