'How do you check if a c code is comiling with linux?
I submitted a C Code for an homework I had and for me and my friends. It seemed to be working with no warnings. We used VSCodium and a Terminal with an texteditor and a Debug Console, to both it seemed to be working just fine.
My teacher still said, that it didnt compile and that's why we got 0 points for it. I would like to ask if there are any other options to test if a code is working (a website, or to download something extra etc). I use the Linux system.
If someone has a tip I would really appreciate it. (If someone could check thess two codes for warnings/or if its compiling for me, would be nice)
the first is a calc.c
#include<stdio.h>
int main(){
char operator;
double num1, num2;
printf("Geben sie einen Operator ein (+, -, *, /): ");
scanf("%c", &operator);
printf("Geben sie die beiden Zahlen nacheinander ein: ");
scanf("%lf %lf", &num1,&num2);
switch (operator)
{
case '+':
printf("%.2lf + %.2lf = %.2lf",num1,num2,(num1+num2));
break;
case '-':
printf("%.2lf - %.2lf = %.2lf",num1,num2,(num1-num2));
break;
case '*':
printf("%.2lf * %.2lf = %.2lf",num1,num2,(num1*num2));
break;
case '/':
if( num2 != 0.0 )
printf("%.2lf / %.2lf = %.2lf", num1,num2,(num1/num2));
else
printf("durch 0 teilen geht nit");
break;
default:
printf("%c ist kein gültiger Operator",operator);
break;
}
return 0;
}
and the second cat.c
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd;
char buf[80];
char msg[50] = "hallo, hier steht immer was in foo steht";
fd = open ("foo.txt", O_RDWR);
printf("fd = %d", fd);
if (fd != -1)
{
printf("\n foo.txt wurde mit (sys)read und (sys)write geoffnet\n");
write(fd, msg , sizeof(msg));
lseek(fd,0,SEEK_SET);
read(fd, buf, sizeof(msg));
printf("\n %s wurde in die datei geschrieben\n", buf);
close (fd);
}
return 0;
}
this one should read what is written in foo.txt and other textdatas by calling it with e.g. "cat foo.txt bar.txt baz.txt"
Solution 1:[1]
There's nothing wrong with the programs.
I think your professor is compiling with g++.
operator
is a keyword in C++
I tried compiling with g++ and I got these warnings:
calc.c: In function ‘int main()’:
calc.c:4:14: error: expected type-specifier before ‘;’ token
4 | char operator;
| ^
calc.c:8:22: error: expected type-specifier before ‘)’ token
8 | scanf("%c", &operator);
| ^
calc.c:13:17: error: expected type-specifier before ‘)’ token
13 | switch (operator)
| ^
calc.c:37:53: error: expected type-specifier before ‘)’ token
37 | printf("%c ist kein gültiger Operator",operator);
| ^
make: *** [Makefile:7: calc] Error 1
Maybe provide your professor with a Makefile?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | debido |