So I am suppose to write a program that will implementing one method if computing
checksums for a file containing integers.

I am executing the program by a command line from the following form:

p4a inputfile outputfile


I can compile the program in Dev C++ compiler with no errors. But the problem is that
when compile the program in unix command line by:

gcc p4a.c

I get the following errors:

p4a.c: In function `main':
p4a.c:42: parse error before `*'
p4a.c:43: `fp2' undeclared (first use in this function)
p4a.c:43: (Each undeclared identifier is reported only once
p4a.c:43: for each function it appears in.)

Can anyone point me in the right direction in fixing these errors so that I can compile correctly by command line


Here is the code below:

#include<stdio.h>
#include<string.h>


main(int argc,char* argv[])
{
	short int a[100][5];
	short int res;


	FILE *fp1;
	if(!(fp1=fopen(argv[1],"r")))
		printf("Error opening file\n");
	else
	{
		int i,j,k,l;
		i=j=0;
		
		while(!feof(fp1))
		{
			for(j=0;j<4;j++)
				fscanf(fp1,"%d",&a[i][j]); 
			i++;
		}
		
		for(k=0;k<i;k++)
		{
			unsigned short int musk=!0; //musk=11...111
			musk=musk|15;				//musk=000..001111
			musk=musk<<12;				//musk=111100..000
			
			res=0;
			for(l=0;l<4;l++)
			{
				short int temp=(a[k][l]&musk);
				res=res|temp;
				musk=musk>>4;
			}
			a[k][4]=res;
		}

		FILE *fp2;
		if(!(fp2=fopen(argv[2],"w")))
			printf("Error:cannot open file");
		else
		{
			for(k=0;k<i;k++)
			{
				for(j=0;j<5;j++)
					fprintf(fp2,"%d%s",a[k][j],"\t");
				fprintf(fp2,"%s","\n");
			}
			fclose(fp1);
			fclose(fp2);
		}

}

	return 0;
}

C programs are allowed to declare variables only at the top of the block enclosed with { and }. So move line 42 up to line 16.

I just copied and compiled your code without issue on a Linux box using

gcc filename.c -o filename

It will depend on the version of the compiler that is used. Declaring objects in the middle of a block like we can do with c++ is only supported by versions of C compiler that support C99 standards.

It will depend on the version of the compiler that is used. Declaring objects in the middle of a block like we can do with c++ is only supported by versions of C compiler that support C99 standards.

AD is right most/all C code has declarations at the beginning of the blocks by convention or by requirement...

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.