Hello Guys!

I am suffering with xcode compiler while i am compiling the below code.
At first the problem was on getch( ) and ungetch ( ) , then I have been advised to use getc/fgetc
Now the error massage is: error: expected identifier or '(' before '{' token

Thank you in advance for your help.

Senan

#include <ctype.h>
#include <stdio.h>

{
	unsigned int c, sign;
	while (isspace(c=getc())); //test whether c is a character of white space
	if(!isdigit(c) && c != EOF && c != '+' && c != '-') //if c is not a digit and it is not equal End Of File (valued -1) and it is not + and it is not -
	{
		ungetc(c); //push a character back into an input stream
		return 0;
	}
	sign = (c == '-') ? -1 : 1; //if c equal - so it is -1 else it is 1
	
	//if c equal - so it is -1 else it is 1
	
	if (c == '+' || c == '-') // if c equal + or c equal -
		c = getc(); //single character is get or read thru getch
	
	if (!isdigit(c)) { ungetc(c);}
	else 
	{
		*pn =0;
		for (; isdigit(c) ; c= getc())
			*pn=10 * *pn + (c - '0');
		
		*pn *= sign; //*pn = *pn * sign
		if(c != EOF)//if c equal end of file
			ungetc(c); //push a character back into an input stream
	}
	return c;
}

int main (void)
{
	int a; int g;
	
	a=getc(&g);
	printf("a");
	printf("g");
	return 0;
}

You have multiple problems with that code. Since I'm so nice, you can compare and contrast with the "corrected" code:

#include <ctype.h>
#include <stdio.h>

unsigned int get_int()
{
    unsigned int c, sign;

    while (isspace(c = getc(stdin)))
        ;

    if(!isdigit(c) && c != EOF && c != '+' && c != '-') {
        ungetc(c, stdin);
        return 0;
    }

    sign = (c == '-') ? -1 : 1;

    if (c == '+' || c == '-')
        c = getc(stdin);

    if (!isdigit(c)) {
        ungetc(c, stdin);
    }
    else {
        unsigned int pn = 0;

        for (; isdigit(c) ; c = getc(stdin))
            pn = 10 * pn + (c - '0');

        pn *= sign;

        if (c != EOF)
            ungetc(c, stdin);

        c = pn;
    }

    return c;
}

int main (void)
{
    unsigned int x = get_int();

    printf("%u\n", x);

    return 0;
}

I say "corrected" because I didn't deal with any logical errors, only the syntax errors and obvious silliness that kept the function (if it's even a function and not some weird Objective-C thing) from working as I think it was intended.

Thank you so much Narue!!!

It works fine. The compiler is succeeded and no errors shown but still when I am building, it keeps hanging or the debugger is keep running without stop to show the result. I do think it is a logical mistake on main.

You solved an important problem for me. Thank you so much again.

Here I would raise a question for you: if I want to gain more experience and skills in c language: what is your advices and recommendation.

Thanks in advance for your kindness.

if I want to gain more experience and skills in c language: what is your advices and recommendation.

Read a lot of code (it doesn't have to be good code), write a lot of code, and read stuff written by C programmers you respect for their skills. Once you gain a certain measure of knowledge, I think that participating on forums such as this one helps a great deal because you'll be exposed to many different perspectives.

This article has been dead for over six months. Start a new discussion instead.