I use CLion by the way.
int ch;
printf("Insert the operation you want to execute: ");
scanf("%d", &ch); //HERE
I declare ch as an integer variable and with scanf and %d and I want the user to insert a value to insert in the ch variable but HERE a warning pops up and says: " 'scanf' used to convert a string to an integer value"
I can't understand why...
Here is the entire code, as requested in comments: (the code is modified because I changed it since yesterday)
typedef unsigned int boolean;
struct list{
float * buffer;
int size;
int head;
int tail;
};
int getsize();
float getvalue();
void init(struct list*, int);
boolean suf_insert(struct list*, float, int);
boolean pre_insert(struct list*, float, int);
void visit(struct list*);
int main(){
struct list listA;
struct list listB;
int size=0;
int ch;
while(1){
printf("Sequential Lists operations\n");
printf("1. Insert the size of the array\n");
printf("2. Initialize the list A\n");
printf("3. Initialize the list B\n");
printf("4. \n");
printf("5. \n");
printf("6. \n");
printf("7. \n");
printf("\nInsert the operation you want to execute: ");
scanf("%d", &ch);
switch(ch){
case 1: size=getsize();
break;
case 2: init(&listA, size);
break;
case 3: init(&listB, size);
break;
case 4: getvalue();
break;
case 5:
break;
case 6:
break;
case 7:
break;
default: printf("\nInvalid command. Retry\n");
break;
}
}
}
int getsize(){
int size;
printf("Insert the size of the list you want to create: ");
scanf("%d", &size);
return size;
}
float getvalue(){
float value;
printf("The value you want to insert: ");
scanf("%f", &value);
return value;
}
void init(struct list * ptr, int size) {
if (size != 0) {
ptr->buffer = (float *) malloc(size * sizeof(int));
ptr->size = size;
ptr->head = 0;
ptr->tail = 0;
} else {
printf("\nBefore continue, insert the size.\n");
}
}
boolean suf_insert(struct list * ptr, float value, int size){
if(((ptr->tail + 1) % ptr->size) != ptr->head && size!=0) {
ptr->buffer[ptr->tail] = value;
ptr->tail = (ptr->tail + 1) % ptr->size;
return TRUE;
} else {
return FALSE;
}
}
boolean pre_insert(struct list * ptr, float value, int size){
if(((ptr->tail + 1) % ptr->size) != ptr->head && size!=0){
ptr->head = (ptr->head + ptr->size - 1) % ptr->size;
ptr->buffer[ptr->head] = value;
return TRUE;
}else{
return FALSE;
}
}
void visit(struct list * ptr){
int position;
for(position=ptr->head; position!=ptr->tail; position=(position+1)%ptr- >size){
printf("%f", ptr->buffer[position]);
}
}
IT SEEMS LIKE I HAVE TO REPEAT: this code ISN'T PERFECT, maybe the exact opposite! I'm still working on it!
The warning is clear about why; you have omitted the part that says "function will not report conversion errors". However it is also clearly nonsense : strtol()
does not report conversion errors either - it simply returns zero if there is a conversion error - but that is indistinguishable from a successful conversion of "0".
With scanf()
at least the return value is an unambiguous indication of success or otherwise. It is true that scanf()
does not report conversion errors, it reports a count of successful conversions, so when there is only one conversion as in this case, a return value of zero is exactly reporting the conversion error.
The "warning" is generated by your IDE not your compiler and is merely advice (bad advice IMO) - you can clearly choose to ignore the advice.
But you should at least check the return value from scanf()
. If scanf()
fails, the value of ch
is undefined. An advantage of strtol()
is that on failure it returns at least a defined value - zero - and in this specific case at least, zero is an invalid value, so it has merit; but not for the reason suggested in the warning perhaps.