To xuftugulus:
True this advanced but very useful, thank you. I even bookmarked the page 
But unfortunately it doesn't help my question.
To vart
Thank you very much for your reply.
1. if input line is longer than the buffer provided - fgets will not store \n character - so just check its present in the resulting buffer
I figured that out just before I read your comment and was very glad you confirmed it and I added an extra step to check that there is nothing other than two integers. I posted the code I wrote so far below (commented) so whoever have a similar question can benefit from it and I will be glad if you can skim through it.
3. do not forget - you need 2*n2
Thank you
2. to check that n1 and n2 are out of range - scan them with sscanf, check the return value to be sure that both values are parsed, check the position with %n to see that the parsing stopped at \n character, then sprintf them back to the temp buffer and compare the 2 buffers to see that numbers are parsed ok
Unfortunately, I still can't check n1 and n2 for over/underflow. I understood the idea that I have to reconstruct the input into a string using the variables "n1" and "n2" right after executing sscanf and then use strcmp on the two strings.
So it would be something like this
sprintf(tempstr, "%ld %ld", n1, n2);
Now the problems I faced with this method is how to reconstruct the string to be exactly the same? For instance how to I deal with white space? I might be able to trim the two strings from all white spaces before comparison but even then how do I deal with inputs like?
> 00000001111 00003
or this
> +123 --3
n1 and n2 wouldn't obviously have all the zeros neither the positive and double negation signs stored in.
While reading the man page of sprintf I saw this modifier you talked about %n and though it would be useful. I still don't know how to use it though (even after an extensive google search)?
I get a Bus Error with
sprintf(tempstr, "%ld %ld %n", n1, n2, %n3); /*n3 is an int*/
As promised here is my code below:
Code:
#include <stdio.h>
#include <limits.h>
#include <inttypes.h>
#include <string.h>
#define MAX 50 /* MAX is 50 since I can assume that no legal input is more than 50 chars excluding the \n */
long n1, n2;
int c;
char str[MAX+2];
/* This is the string where I will store the input.
I added 2 to MAX to leave room for the new line character as well as for the (null) character
which is added automatically after fgets execution. */
int main(void)
{
if (fgets(str, MAX + 2, stdin) == str) /* To check if fgets ran..successfully..*/
{
if (str[strlen(str)-1] == '\n')
/* I subtracted 1 from strlen to get to the character before the null which should be \n */
{
if (sscanf(str, "%ld %ld %1s", &n1, &n2, %c) == 2)
{
if(c == '\0')
{
if n1 and n2 is not over/underflow /* I am stuck with this part */
{
if (n1 > LONG_MAX - (2*n2))
printf("overflow\n");
if (n1 < LONG_MIN - (2*n2))
printf("underflow\n");
else
printf("%d\n", n1 + (2*n2));
}
}
else
{
printf("illegal input\n");
}
}
else
{
printf("illegal input\n");
}
}
else
{
printf("illegal input\n");
}
}
if (fgets(str, MAX + 2, stdin) == '\0')
/* This means that we have reached EOF so we need to check for illegal input of more than 50 char differently
becuase there might not be a \n */
{
if(strlen(str) <= MAX)
{
/* I will call a function with the above code which starts with if n1 and n2 ... */
}
else
{
printf("illegal input\n");
}
}
else
{
printf("illegal input\n");
}
return 0;
}