-3
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int digit(s1);
int main()
{char s1[10];
printf("\n Type anything you want:\n");
gets(s1);
printf("The number of digits is:%d",digit(s1));
return 0;
}
int digit(char* s1)
{
    int i=0;
  while(*s1)
{
i+= !!isdigit(*s1++);
}
return i;
}

This is my code, I need to find out is the element that I give a digit or not. I change it into void digit and it runs perfectly now.

mesad
  • 1
  • 1
  • 1
    Please take notice of compiler warnings - you pass the wrong type to `isdigit` --- OH you `#include ` but rewrite the library function `isdigit` quite differently. The code doesn't even compile, for other reasons too. – Weather Vane May 08 '20 at 14:38
  • Your function needs to return a value indicating its result, which then needs to be used by the code in `main`. Also, please do not use `gets` function. It is no longer part of the c library. – Weather Vane May 08 '20 at 14:43
  • @WeatherVane Thats what I learn from lecture notes, what should I write for second return? I know that I need to write something at the very bottom but don't know what exactly – mesad May 08 '20 at 14:50

3 Answers3

0

isdigit is a standard C function declared in <ctype.h>. Your function conflicts with it. Choose a different name for your function.

That will avoid the compiler message about conflicting types, but there are other errors in your program you will need to fix.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • I choose a different name for it but couldn't understand the rest. I should write something at the bottom like second return but don't know what to write exactly – mesad May 08 '20 at 14:53
0

The code you provided has several major issues. If the lecture says to use gets(), throw it into the trash, use fgets() instead. The reason why, you can find in this link:

Why is the gets function so dangerous that it should not be used?

Apart from that, The lecture seems to not even provided you the knowledge to correctly pass parameters or to output values of variables correctly.

I recommend you to read a good C starting book, f.e. this. A list of recommended books can be also found here:

The Definitive C Book Guide and List

To only focus one issue, the output:

printf("There are %d digits in your string.");

This use of printf() is incorrect. Where shall the value specified by %d come from? The %d format specifier is missing a corresponding argument which points to an integer value or an int variable, like:

int a = 10;
printf("a = %d",a);

%d requires a matching argument of type int to actually print a value. Else the behavior is undefined.

This shall in all cases give a diagnostic. Never ignore compiler warnings.


Why should I always enable compiler warnings?

0
#include<stdio.h>
#include<string.h>
#include<ctype.h>

int count_digits(const char *s1);

int main()
{
    char s1[20];
    printf("\n Type anything you want:\n");
    fgets(s1, sizeof(s1), stdin);
    printf("The number of digits is:%d", count_digits(s1));
    return 0;
}

int count_digits(const char* s1)
{
    int count = 0;
    while(*s1) 
    {
        count += !!isdigit(*s1++);
    }
    return count;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • Note, that OP isn´t even be able to understand how to use a format specifier and passing function parameters. The code in `count_digits` will probably knock him out. – RobertS supports Monica Cellio May 08 '20 at 15:05
  • I'm still learning so could please explain the part "count += !!isdigit(*s1++);" I would be so greatful.What is that stands for? – mesad May 08 '20 at 15:17
  • @RobertS: That is by design. I gave him correct code that he can step through and study, but cannot turn in for his homework assignment. – abelenky May 08 '20 at 15:25