You are here:

C/Having a problem compiling

Advertisement


Question
I am tring to fing out the results of sum1,sum2 but I am having a problem compiling this code. What am I doing wrong.

int fun(int *k) {
  *k +=4;
  return 3 * (*k) - 1;
  }
void main() {
  int i = 10, j = 10, sum1, sum2;
  sum1 = (i / 2) + fun(&i);
  sum2 = fun(&j) + (j / 2);
  }


Answer
There is no compilation errors in this file.
There can only be warning, which you can ignore.
The main() function has to return int and you are making it return void.
Hence it is giving warning.

Here is the corrected code:

#include <stdio.h>

int fun(int *k)
{
       *k +=4;
       return 3 * (*k) - 1;
}

int main()
{
       int i = 10, j = 10, sum1, sum2;
       sum1 = (i / 2) + fun(&i);
       sum2 = fun(&j) + (j / 2);

       printf("SUM1 = %d\n", sum1);
       printf("SUM2 = %d\n", sum2);
}

If you are still having problems, add the exact errors that you are seeing and we will try to solve that.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.