Bruce B Bruce B - 8 months ago 55
C Question

Number of characters in a password in C

I'd like to write a code in C in which the number of characters of a password is declared but I don't know why after using the following algorithm, I get weird answers.

#include <stdio.h>
#include <stdlib.h>

int main()
int n=1,i=0;
char * pass[100];

printf("Enter your pass: ");
scanf(" %c",&pass);

for (i=0; pass[i]!='0';i++){

printf("number of characters: %d\n", n);

return 0;

Answer Source

To start with char * pass[100] is probably not what you want although it might work. You just need char pass[100].

Secondly you are asking scanf to scan for a single character with the %c option. To read a whole string use the %s option.

Finally in the line: for (i=0; pass[i]!='0';i++){

'0' does not mean null, it refers to the character 0, i.e. 48 on the ascii charts. You are probably looking for '\0' which means null. Null is used to terminate the strings.