In this program, I cannot in put - integer. It outputs wrong result. What should I do to make it possible? Thanks heaps !!!

#include<stdio.h>

int main(){
	int num;
	int max=0;
	printf("Enter +/- integers or enter 0 to Exit \n");	
	while(1){
		scanf("%d",&num);
		if(num==0)
			break;
		if(num>max)
			max=num;
	}
		printf("Max is %d",max);
		return 0;
	}

Recommended Answers

All 3 Replies

Initialise your max to something negative (very large and negative).
Look in limits.h

Thanks !! got it.

1. Always check up scanf return code. It returns a number of successfully converted fields.

if (scanf("%d",&num) != 1) {
   /* User type not-a-number or close stdio */
}

2. Always print some kind of a prompt message, for example:

printf("Enter an integer: ");
if (scanf("%d",&num) != 1)
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.