1

I'm trying to reference global variables in a function called "userval", and then modify those variables based on user input. Do I need to return these variables at the end of my function?

I am attempting to check my code by printing these global variables within the main function. However, I keep getting random characters.

Here is my code below.

#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;
}
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
  • 2
    You can't return more than one value from a function, and since those variables are global you don't need to return anything, they've already been changed. You're using `printf` incorrectly. You may want to look at the documentation for that. – Retired Ninja Feb 03 '18 at 21:03

2 Answers2

2

The correct way to use it would be to

printf("%p\n",(void*)&len);

But this would print the address of the variable - most likely you want to print the value of the variable. (Holds for other int` variables also in your example).

printf("%d\n",len);

While using printf the first argument is the format string and the rest of the arguments are 0 or more variables (as dictated by format string).

From standard

int printf(const char * restrict format, ...);

This is the signature of the printf function. Also regarding the format specifier §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.

Instead of any output stream it is stdout for printf.

user2736738
  • 30,591
  • 5
  • 42
  • 56
2

That's not how the printf function works.

The first parameter is a format string. It contains any static text you want to print along with format specifiers for any values you want to print.

For example, if you want to print only a integer followed by a newline, the format string you would use is "%d\n", where %d is the format specifier for an integer and \n is a newline character.

Any subsequent parameters are used to fill in the format. In the case of the values you want to print, you would do the following:

printf("%d\n", len);
printf("%d\n", upper);
printf("%d\n", lower);
printf("%d\n", digit);
printf("%d\n", special);
dbush
  • 205,898
  • 23
  • 218
  • 273