Bruce B Bruce B - 1 year ago 89
C Question

number of characters in a passord in C

I'd like to write a code in C in which the number of characters of a password is declared but I dont 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download