-3
char c[100];
char *pc;
pc = c;
printf("Enter the string:\n");
gets(c);

while(*pc != '\0')
{
    if(isdigit(*pc)==1)
        printf("%c",*pc);
    pc++;
}

why this program is not giving the output since it gives no errors while executing, please help.

  • 1
    Can you post the full program? include your `main(){` part of the function? – J0hn Aug 02 '17 at 13:25
  • what is the given input and which is your expected output? – Forever Learner Aug 02 '17 at 13:48
  • 1
    Your title says there's a runtime error but in your question you say there's no errors... It's looks to be a program that prints only numeric characters while ignoring non-numeric characters. Though it uses the disapproved `gets()` function, in the event that the input is less than 100 char, it prints what is expected? – Miket25 Aug 02 '17 at 13:53
  • Yeah, he edited the post, look at the previous edits. – Alexandre Mercier Aubin Aug 02 '17 at 13:54

3 Answers3

1

Avoiding the use of gets(), fgets() accomplishes the same task, and it's safer.

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

int main(){
    char c[100];
    printf("Enter the string:\n");
    fgets(c,100,stdin);
    int i = 0;
    while(c[i] != '\0'){
        if(isdigit(c[i])){
             printf("%c",c[i]);
        }
        i++;
    }
}

Edit: Without the rest of your code, a reasonable source of error is the comparison isdigit(*pc) == 1, by the standard,

C11 §7.4.1

The functions in this subclause return nonzero (true) if and only if the value of the argument c conforms to that in the description of the function.

That is, int isdigit(int c);. isdigit could be returning non-zero values for when it is true, and it can not be printed due to the comparison of just 1.

Community
  • 1
  • 1
Miket25
  • 1,895
  • 3
  • 15
  • 29
0

Well, isdigit takes a int as input parameter and you are giving it a char array.
You might want to do something like this instead of what you are doing:

if(isdigit(*pc))
    printf("%c",*pc);
pc++;

Doing so should make an implicit cast of the character to int.

Update:

Have you tried printing the output of isdigit before doing the if?

printf("%d",isdigit(*pc));

Sometimes, it returns an odd number such as 4.

  • 1
    I believe your first snippet of code is correct. `isdigit(int c)` takes an `int c` where c is the ascii value of some character, so `isdigit(1)` is false where `isdigit('1')` is true. `*pc` is not a character array, but a character after being dereferenced, so it's also valid. The last line will print 1's & 0's, but it's valid :) – Miket25 Aug 02 '17 at 14:04
  • isdigit might return a 0 or a digit value greater than 0 : https://www.programiz.com/c-programming/library-function/ctype.h/isdigit – Alexandre Mercier Aubin Aug 02 '17 at 14:10
  • 1
    Ah good point. It also says zero and non-zero in §7.4.1 of the C11 standard. – Miket25 Aug 02 '17 at 14:15
0

The issue is == 1 the isdigit is not returning 1 only non zero value. Remove it or if you want to compare with 1 for some reasons put the !! in the front of the isdigit

0___________
  • 60,014
  • 4
  • 34
  • 74