1

Please help me understand this line and firstly I was using getch() but then I used return(0) and it shows this:

Process returned 14 (0xE) execution time : 0.015 s

#include <stdio.h>
#include <conio.h>
#include <math.h>

int add(int a, int b) {
    int c;
    c = a + b;
    return (c);
}

void main() {
    int x, y, z;
    x = 5;
    y = 10;
    z = add(x, y);
    printf("The sum is: %d", z);

    return (0);
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
bibhukjha
  • 33
  • 4
  • 3
    This code looks very **C**, and not very **C++**. Given the ``, it also appears to be ancient, from the time neckbearded dinosaurs roamed the earth. – Eljay Jul 10 '21 at 15:40
  • 4
    `main` function should return `int`, not `void`. – Quimby Jul 10 '21 at 15:42
  • 2
    If you want to learn C++, please consider reading a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You are currently learning many poor habits – alter_igel Jul 10 '21 at 15:44
  • 2
    And there's no need for parentheses around the value you're returning. Nor in anything approaching modern C is there a need for separate declaration of variables and initialization. `int a = 1;` vs. `int a; a = 1;' for instance. – Chris Jul 10 '21 at 15:44
  • 1
    True, I am learning Data Structure through C and C++, basic also new to stackoverflow so added the tag c++ for reach, hopefully I won't get extinct from this ecosystem at least. – bibhukjha Jul 10 '21 at 15:44
  • What on earth does the basic and dsa tag do here? They are completely random. – klutt Jul 10 '21 at 15:45
  • 1
    There is a meta post on [C/C++ tagging](https://meta.stackoverflow.com/questions/252430/disallow-the-tagging-of-questions-with-both-c-and-c-tags), and an [answer](https://stackoverflow.com/questions/68045486/print-line-number-incrementing-with-each-input-in-c/68058865#68058865) related to the use of `getch()`, which might be useful reference for the future. – Nagev Jul 10 '21 at 16:31
  • 1
    Also, it's useful to mention which compiler you are using. – Nagev Jul 10 '21 at 16:37

1 Answers1

3

You are returning an int from main, but the return type is void. Change the return type of main to int, you'll get 0 as the return value.

If you compile this with gcc in a file test.c, it will tell you, e.g.:

test.c: In function ‘main’:
test.c:19:7: warning: ‘return’ with a value, in function returning void
   19 | return(0);
      |       ^
test.c:11:6: note: declared here
   11 | void main()
      |      ^~~~

Note that the #include <conio.h> has to be removed first to be compiled with gcc, since it is non-standard.

Nagev
  • 10,835
  • 4
  • 58
  • 69
devReddit
  • 2,696
  • 1
  • 5
  • 20