コード例 #1
0
int main(void)
{
    /*
     * Function pointer declaration.
     * int <- return value of the function
     * (*arithmetic_operator) <- pointer to the function
     * (int, int) <- arguments to the function
     *
     * Note that the function signature of the
     * function pointer and the function pointed by
     * the function pointer should be the same.
     *
     * e.g. signature of arithmetic_operator
     * is same as addition, subtraction, multiplication
     */
    int (*arithmetic_operator)(int, int);
    float (*farithmetic_operator)(int, int);

    /*
     * This statement is like a normal pointer assignment.
     */
    arithmetic_operator = &addition;
    printf("%d\n", arithmetic_operator(10, 20));

    arithmetic_operator = &subtraction;
    printf("%d\n", arithmetic_operator(20, 10));

    arithmetic_operator = &multiplication;
    printf("%d\n", arithmetic_operator(10, 20));

    /*
     * What happens when the function signature of the function pointer
     * and the assigned function does not match.
     *
     * Note: Warning is reported by the C compiler on the
     * incompatible pointer assignment
     *
     * Why does the following printf statements print
     * different values?
     */
    printf("Non-matching function signatures\n");
    arithmetic_operator = &division;
    printf("%f\n", arithmetic_operator(10, 20));
    printf("%f\n", division(10, 20));

    printf("\nMatching function signatures\n");
    farithmetic_operator = &division;
    printf("%f\n", farithmetic_operator(10, 20));
    printf("%f\n", division(10, 20));

    return 0;
}
コード例 #2
0
ファイル: FSM.c プロジェクト: manavs19/compiler-design
//main function
int main()
{
	initialize_Symbol_Table();
	
	remove("a4_1.out");
	
	char input[1000];
	
	token t;
	int i;
	
	//loop for rading lines of source code into input buffer	
	while(gets(input))
	{
		lexemeBegin = 0;
		
		while(input[lexemeBegin]!='\0'&&input[lexemeBegin]!='\n' &&input[lexemeBegin]!='\r')  //while end of line is not found
		{
			while(input[lexemeBegin]==' ' || input[lexemeBegin] == '\t')
				lexemeBegin++;
						
			//checking for keywords and identifiers
			t = identifier_keyword(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}
			
			//checking for integer constants
			t = integer_constant(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}
			
			//checking for floating point number constants
			t = float_constant(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}

			//checking for arithmetic operators
			t = arithmetic_operator(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}

			//checking for assignment operators
			t = assignment_operator(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}

			//checking for relational operators
			t = relational_operator(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}

			//checking for special symbols
			t = special_symbol(input);
			if(t.tokenID!=NOTOK)
			{
				write_token(t);
				continue;
			}
			
			//checking for comments
			comment(input);
			//checking erroneous lexeme
			error_condition(input);
		}
	}	
}