Exit Value 1 when returning 0 - c

I've searched up and down Google and I cannot find a reason why this code is still failing.
void returnSeven();
int main() {
returnSeven();
return 0;
}
void returnSeven(){
printf("Hello");
}
It builds properly but gives me the
RUN FAILED (exit value 1, total time: 21ms)
error. Any possible ideas how to get rid of this? I've run it from command line and it gives me the same error. I've even had a friend run it and it seemed to work just fine for him. Any ideas would be appreciated.

Your code are ok after adding #include <stdio.h>. So, have you add that line in your code?
In the C programming language, if you haven't define printf, it can also be compiled just with a warning like this : incompatible implicit declaration of built-in function ‘printf’ [enabled by default]

Related

What does "declaration is incompatible" error mean in VS Code with C_lang linting?

I'm writing a simple code in C language, and this works.
Which compiles and excutes with no errors, gives the expected output.
#include <stdio.h>
int main(void) {
struct SiteTemplate {
int views;
};
int visit(struct SiteTemplate *site) {
site -> views++;
return 0;
}
struct SiteTemplate site;
site.views = 0;
visit(&site);
printf("%d\n", site.views);
return 0;
}
But in my VS Code, with C_Cpp linting is on, my IDE shows the following error and other problems with it.
declaration is incompatible with previous "visit" (declared at line 8)
Having a screenshot of it:
This error linting is really confusing me since my code works with gcc, it doesn't show any error when compiling.
And also, if I move my struct and function definition to the global level instead of inside main(), then the errors don't exist anymore... But what's the error declaration is incompatible? Or is there any problem with my code?
Click here to view the another screenshot to save whitespaces of this page.
By the way, the version of my VS Code is 1.52.0, with default C_Cpp linting.
Nested function definition is not standard C, it's supported by compiler extensions. According to C standard, any function definition needs to appear outside of any other function definition.

How do I ignore the gets() warning when compiling?

I know you quickly clicked this expecting to answer NEVER USE GETS! but I have a valid reason. I am learning about buffer overflows and need to purposely develop vulnerable software.
So, as the title states, how do I ignore the warnings so the compilation succeeds? I have tried:
gcc bo.c -o bo -Wall
... to no avail.
Thanks for any help.
This code:
#include <stdio.h>
int main() {
char foo[10];
gets( foo );
return 0;
}
produces the following output when compiled:
bo.c: In function 'main':
bo.c:4:2: warning: 'gets' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
gets( foo );
^
/tmp/cclk8TkP.o: In function `main':
bo.c:(.text+0x10): warning: the `gets' function is dangerous and should not be used.
The first warning is from the compiler, and we can see what flag to use to suppress it: -Wno-deprecated-declarations.
This leaves the second warning, which is from the linker. As far as I can tell there is no way to suppress that warning easily (see here and here). However it should not be a problem, since it is a warning, not an error; the executable gets created.
use fgets instead of gets
Example:
fgets (foo, sizeof(foo), stdin);

Warning: Function must be extern error in C

I am following along a tutorial for C programming in 6502 Assembly and am having a great deal of difficulty on the 3rd step. When compiling my code in the command line, i get these errors:
test4.c(8): Error: '{' expected
test4.c(9): Warning: Function must be extern
test4.c(9): Error: ';' expected
test4.c(13): Error: '}' expected
I am using a program to compile .c files made in code::blocks to .nes files. The current tutorial is having me also make .s assembly file when compiling in the cl65 command line in Windows from the program that is compiling it. Here is the link to the tutorial page i am on:
https://helloacm.com/tutorial-3-c-programming-in-6502-using-assembly/
I have tried many different combinations of code that i can think of to try and get rid of a least some of the problems, but alas to no avail. I am an amateur in C, usually use C++ but i cannot and still trying to figure this out. I was not able to find the "Function must be extern" error anywhere with a quick google search either. Anyone have an idea what's going on?
Here is how i wrote the code in code::blocks:
void main()
{
asm("nop");
}
void testasm()
void main()
{
while(1) {
testasm(); // call the assembled function
}
}
Also, had a really difficult time following along on this particular tutorial part.
Thanks in advance, any help is appreciated on diagnosing the problem!
Perhaps this is what you're after?
void testasm()
{
asm("nop");
}
void main()
{
while(1) {
testasm(); // call the assembled function
}
}
Your code had two main() functions, and a prototype void testasm() with no terminating semicolon.
Alternatively, if testasm is written in assembly, your code should look like this (removing the extra main function):
extern void testasm(); // `extern` not specifically required, but may be for
// your particular compiler
void main()
{
while(1) {
testasm(); // call the assembled function
}
}
You need to be much more careful writing code.

ioctl and hdreg to get information on harddrives

I want to get basic information from a hard-drive and print it out. The most important is that the physical sector size is correct.
For the past few hours I have been fighting with ioctl to get what I want but I can't figure it out.
I have never used ioctl before and I can't seem to find an easy explanation on what exactly you have to do.
Anyway my code looks something like this
int main () {
FILE *driveptr;
int sectorsize;
struct hd_driveid hd;
driveptr=fopen("/dev/sda","r");
if (ioctl(driveptr,HDIO_GET_IDENTITY, &hd)!=0) {
printf("Hard disk model: %s\n",hd.model);
printf("Serial number: %s\n",hd.serial_no);
printf("Sector size: %i\n",hd.sector_bytes);
sectorsize=hd.sector_bytes;
} else {
printf("Error fetching device data.\n");
}
}
In the compiler it throws these warnings, it compiles but the strings are empty when printed.
gcc -o test source.c
source.c: In function ‘main’:
source.c:67:9: warning: passing argument 1 of ‘ioctl’ makes integer from pointer without a cast [enabled by default]
/usr/include/x86_64-linux-gnu/sys/ioctl.h:42:12: note: expected ‘int’ but argument is of type ‘struct FILE *’
I hope somebody can explain to me what goes wrong!
Instead of
if (ioctl(driveptr,HDIO_GET_IDENTITY, &hd)!=0) {
you probably want
if (ioctl(fileno(driveptr),HDIO_GET_IDENTITY, &hd)!= -1) {
^^^^^^^ ^ ^^
Because ioctl's first argument need to be an integer file descriptor not a FILE *
fileno() will give you an integer fd from a FILE *.
Note also that ioctl returns -1 on an error and sets errno.
Reading the man pages of the functions you are using is probably quicker than posting to StackOverflow.
See man pages of ioctl, fileno.

Why do I get "Invalid conversion from "void *" to "int**" error?

my code shows this warnings when compiling with "g++ -Wall -pedantic -Wno-long-long -c main.c". I have to compile in this mode, becouse its a homework and we have an application that corrects them and it uses this compile mode.
Error: invalid conversion from "void" to "int** " [-fpermissive]
Error: invalid conversion from "void" to "int* " [-fpermissive]
Error: invalid conversion from "void" to "main(int, char*)::VYSLEDEK" [-fpermissive]
the same errors continue as i realloc quite a lot in my program. I tried to change almost everything in that realloc, it is still the same.
Parts of the code :
struct VYSLEDEK
{
int sirka;
int vyska;
int zacatek_x;
int zacatek_y;
int soucet;
} *vysledek;
int **matice,**soucty;
.....
matice=(int**)malloc(1*sizeof(int*));
matice[0]=(int*)malloc(1*sizeof(int));
soucty=(int**)malloc(1*sizeof(int*));
soucty[0]=(int*)malloc(1*sizeof(int));
.....
1. matice=realloc(matice,naalokovano*2*sizeof(int*));
2. soucty=realloc(soucty,naalokovano*2*sizeof(int*));
.....
for (i=0;i<(naalokovano*2);i++)
{
3. matice[i]=realloc(matice[i],sizeof(int));
4. soucty[i]=realloc(soucty[i],sizeof(int*));
};
.....
5. vysledek=realloc(vysledek,vysledku*sizeof(struct VYSLEDEK*));
Thank you for your help.
You already did cast the result of malloc to the right type. Do the same for the realloc calls, too.
BTW: Don’t complain that they force you to switch on the warnings. I think it’s the most sensible thing to do by default, I always use at least -Wall -Werror.

Resources