Search code examples
c++printfdeclarationimplicit

Implicit declaration of printf


I'm getting implicit declaration error. Please help. I don't know how to explain it in terms of words, I'd appreciate it very much if you could help me with error. It's my assignment at school and I want to resolve the issue. Please help.

#include<stdio.h>
int printmenu(int *size_of_char);
int get_char(int **size_of_char);

int main() { 

int choice = 0, size_of_char;

while (choice == 0) {
  printf("Enter the size of the array: "); 
  scanf("%d", &size_of_char);
  if (size_of_char <= 0) {
    printf("Invalid input\n");
  }
  else {choice = printmenu(&size_of_char);
  }
}
return 0;

}

int printmenu(int *size_of_char) {
int x;
printf("Menu\n\n");
printf("0. Input characters\n");
printf("1. Shift Elements to Right\n");
printf("2. Combinations of 2 digits\n");
printf("3. Exit\n");
printf("Enter choice: "); 
scanf("%d", &x);

if (x == 0) { 
      get_char(&size_of_char);
    }
  }

int get_char(int **size_of_char) {
    char string[**size_of_char];
      for(int i = 0; i < **size_of_char; i++){
      printf("Enter value: %c ", i+1);
      scanf("%c", &string[i]);
      for(int i = 0; i < **size_of_char; i++){
printf("Your grade in subject %d is %c.\n", i+1, size_of_char[i]);
//printf("Your grade in subject %d is %f.\n", i+1, *(grades + i));


}
}
}

Thanks


Solution

  • You've correctly included the header which declares printf in the example that you show.


    There are other bugs however:

    char string[**size_of_char];
    

    This is ill-formed. The size of an array must be a compile time constant. That expression isn't.

    int printmenu(int *size_of_char)
    

    printmenu is declared to return int, but there is no return statement. The behaviour of the program is undefined.

    // int **size_of_char
    printf("Your grade in subject %d is %c.\n", i+1, size_of_char[i]);
    

    You're trying to print a int* with a wrong format specifier. The behaviour of the program is undefined.