Fulvio Denza Fulvio Denza - 3 months ago 50
C Question

Loop for over the condition

#include <stdio.h>

int multiple(int, int);

int main()
int i;
int x;
int y;
scanf("%d %d", &x, &y);
printf("%d", multiple(x,y));

int multiple(int N,int M)
int i;
int result;
for (i=0;i*M<N;i++)
printf("%d", result);

When I put input (for example x=100 and y=7) the output displays all the multiple until 105 and not until 98 as it should be.


The loop does print up to 98. However, multiple is declared to return an int but doesn't actually have a return statement, so the return value is unspecified (and in practice you'll get some arbitrary value from a previous calculation). Then you print this "garbage" return value and in your case it happens to be 105.

If you don't want multiple to return a value then don't declare it to return a value, and don't print the value it returns.