如何指向和修改全局变量 - C 编程
How to Point and Modify a Global Variable - C Programming
我试图在一个名为 "userval" 的函数中引用全局变量,然后根据用户输入修改这些变量。我需要在函数末尾 return 这些变量吗?
我试图通过在主函数中打印这些全局变量来检查我的代码。但是,我不断收到随机字符。
下面是我的代码。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Global Constants
#define MAX_PSWD_LEN 80
char password = 0;
int len = 0;
int upper = 0;
int lower = 0;
int digit = 0;
int special = 0;
int main(int argc, char *argv[]) {
userval();
printf(&len);
printf(&upper);
printf(&lower);
printf(&digit);
printf(&special);
}
int userval() {
printf("Please enter the minimum length of your password: \n");
scanf("%d", &len);
printf("Please enter the minimum number of uppercase letters in your password: \n");
scanf("%d", &upper);
printf("Please enter the minimum number of lowercase letters in your password: \n");
scanf("%d", &lower);
printf("Please enter the minimum number of decimal digit characters in your password: \n");
scanf("%d", &digit);
printf("Please enter the minimum number of special characters in your password: \n");
scanf("%d", &special);
printf("Thank you. \n");
return len, upper, lower, digit, special;
}
正确的使用方法是
printf("%p\n",(void*)&len);
但是这会打印变量的地址 - 您很可能想打印变量的值。 (在您的示例中也适用于其他 int` 变量)。
printf("%d\n",len);
使用 printf
时,第一个参数是格式字符串,其余参数是 0 个或多个变量(由格式字符串指定)。
来自标准
int printf(const char * restrict format, ...);
这是printf
函数的签名。另外关于格式说明符 §7.21.6.1
The format
shall be a multibyte character sequence, beginning and ending in its initial shift state. The format is composed of zero or more directives: ordinary multibyte characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments, converting them, if applicable, according to the corresponding conversion specifier, and then writing the result to the output stream.
对于 printf
.
,它是 stdout
而不是任何输出流
printf
函数不是这样工作的。
第一个参数是格式字符串。它包含您要打印的任何静态文本以及您要打印的任何值的格式说明符。
例如,如果您只想打印一个整数后跟一个换行符,您将使用的格式字符串是 "%d\n"
,其中 %d
是整数的格式说明符,\n
是换行符。
任何后续参数都用于填写格式。对于要打印的值,您可以执行以下操作:
printf("%d\n", len);
printf("%d\n", upper);
printf("%d\n", lower);
printf("%d\n", digit);
printf("%d\n", special);
我试图在一个名为 "userval" 的函数中引用全局变量,然后根据用户输入修改这些变量。我需要在函数末尾 return 这些变量吗?
我试图通过在主函数中打印这些全局变量来检查我的代码。但是,我不断收到随机字符。
下面是我的代码。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Global Constants
#define MAX_PSWD_LEN 80
char password = 0;
int len = 0;
int upper = 0;
int lower = 0;
int digit = 0;
int special = 0;
int main(int argc, char *argv[]) {
userval();
printf(&len);
printf(&upper);
printf(&lower);
printf(&digit);
printf(&special);
}
int userval() {
printf("Please enter the minimum length of your password: \n");
scanf("%d", &len);
printf("Please enter the minimum number of uppercase letters in your password: \n");
scanf("%d", &upper);
printf("Please enter the minimum number of lowercase letters in your password: \n");
scanf("%d", &lower);
printf("Please enter the minimum number of decimal digit characters in your password: \n");
scanf("%d", &digit);
printf("Please enter the minimum number of special characters in your password: \n");
scanf("%d", &special);
printf("Thank you. \n");
return len, upper, lower, digit, special;
}
正确的使用方法是
printf("%p\n",(void*)&len);
但是这会打印变量的地址 - 您很可能想打印变量的值。 (在您的示例中也适用于其他 int` 变量)。
printf("%d\n",len);
使用 printf
时,第一个参数是格式字符串,其余参数是 0 个或多个变量(由格式字符串指定)。
来自标准
int printf(const char * restrict format, ...);
这是printf
函数的签名。另外关于格式说明符 §7.21.6.1
The
format
shall be a multibyte character sequence, beginning and ending in its initial shift state. The format is composed of zero or more directives: ordinary multibyte characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments, converting them, if applicable, according to the corresponding conversion specifier, and then writing the result to the output stream.
对于 printf
.
stdout
而不是任何输出流
printf
函数不是这样工作的。
第一个参数是格式字符串。它包含您要打印的任何静态文本以及您要打印的任何值的格式说明符。
例如,如果您只想打印一个整数后跟一个换行符,您将使用的格式字符串是 "%d\n"
,其中 %d
是整数的格式说明符,\n
是换行符。
任何后续参数都用于填写格式。对于要打印的值,您可以执行以下操作:
printf("%d\n", len);
printf("%d\n", upper);
printf("%d\n", lower);
printf("%d\n", digit);
printf("%d\n", special);