0% found this document useful (0 votes)
7 views3 pages

C Program for Type Checking in Expressions

The document outlines a C program designed to check the validity of variable types in expressions. It includes an algorithm for type checking and instructions for compiling and running the program using the GCC compiler. The program prompts the user for variable names and types, processes an expression, and checks for type mismatches, providing feedback accordingly.

Uploaded by

a.baranidharan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

C Program for Type Checking in Expressions

The document outlines a C program designed to check the validity of variable types in expressions. It includes an algorithm for type checking and instructions for compiling and running the program using the GCC compiler. The program prompts the user for variable names and types, processes an expression, and checks for type mismatches, providing feedback accordingly.

Uploaded by

a.baranidharan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

IMPLEMENT TYPE CHECKING

AIM:

To write a C program to check whether the type of all variables in an expression are valid or not.

ALGORITHM:

Step1: Track the global scope type information (e.g. classes and their members)

Step2: Determine the type of expressions recursively, i.e. bottom-up, passing the resulting types

upwards.

Step3: If type found correct, do the operation

Step4: Type mismatches, semantic error will be notified

STEPS TO RUN C PROGRAM USING GCC COMPLIER:

1. Open any editor, type the needed C program and save as typecheck.c

2. Compile C language source

gcc typecheck.c

Following file will be generated : [Link]

3. Run the executable file : [Link]

#include<stdio.h>

void main()

int n,i,k,flag=0;

char vari[15],typ[15],b[15],c;

printf("Enter the number of variables:");

scanf(" %d",&n);

for(i=0;i<n;i++)

printf("Enter the variable[%d]:",i);

scanf(" %c",&vari[i]);

printf("Enter the variable-type[%d](float-f,int-i):",i);

scanf(" %c",&typ[i]);

if(typ[i]=='f')

flag=1;
}

printf("Enter the Expression(end with $):");

i=0;

getchar();

while((c=getchar())!='$')

b[i]=c;

i++;

k=i;

for(i=0;i<k;i++)

if(b[i]=='/')

flag=1;

break;

for(i=0;i<n;i++)

if(b[0]==vari[i])

if(flag==1)

if(typ[i]=='f')

printf("\nthe datatype is correctly defined..!\n");

break;

else

{
printf("Identifier %c must be a float type..!\n",vari[i]);

break;

else

printf("\nthe datatype is correctly defined..!\n");

break;

You might also like