Need help in C programming - uninitialized variable

Discussion in 'Programming' started by MarianCvrta, Oct 26, 2010.

  1. #1
    i have a program like this one:

    #include<stdio.h>
    void sum(int a,int b, int s){
    s=a+b;}
    
    int main(){
    int s;
    suma(2,3,s);
    printf("Sum is %d\n",s);
    return 0;}
    Code (markup):
    the error is in function main as it says i m using 's' - uninitialized variable.
    i need to initialize the variable s with s=0; but i don't know where to insert this line.

    any help would be appreciated. thanks
     
    MarianCvrta, Oct 26, 2010 IP
  2. drhowarddrfine

    drhowarddrfine Peon

    Messages:
    5,428
    Likes Received:
    95
    Best Answers:
    7
    Trophy Points:
    0
    #2
    Are you trying to learn to program in C? How did you get that far without knowing how to do this?

    In your main function, just add 's=0;' after 'int s;'.
     
    drhowarddrfine, Oct 26, 2010 IP
  3. MarianCvrta

    MarianCvrta Peon

    Messages:
    195
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    man, that's the first thing i tried, but it didn't work. it shows the sum being equal to 0, meaning the function is not taking any action on s.

    that's why i posted that.
     
    MarianCvrta, Oct 27, 2010 IP
  4. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #4
    You can either do this:
    
    #include <stdio.h>
    
    int sum(int a, int b) {
      return a+b;
    }
    
    int main(){
    int s = 0;
    s = sum(2,3);
    printf("Sum is %d\n",s);
    return 0;
    }
    
    Code (markup):
    or this (but this is as C++ now because of the use of reference)..
    
    #include <stdio.h>
    
    void sum(int a, int b, int& s) {
      s = a+b;
    }
    
    int main(){
    int s = 0;
    sum(2,3,s);
    printf("Sum is %d\n",s);
    return 0;
    }
    
    Code (markup):
     
    Rainulf, Oct 27, 2010 IP
  5. drhowarddrfine

    drhowarddrfine Peon

    Messages:
    5,428
    Likes Received:
    95
    Best Answers:
    7
    Trophy Points:
    0
    #5
    Your request was how to resolve the warning about s being uninitialized, not that you were getting the wrong answer. @rainuff solves the second part.
     
    drhowarddrfine, Oct 27, 2010 IP
  6. CPACore

    CPACore Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    your function is a pass by value function, and it's a non-return type, it does not change anything to the variable, you have to pass by referrence, or pass by pointer, use & before the variable in the function header like this:

    void sum(int a, int b, int &s), and the function in your main function I see it's suma(2,3,s), I duno whether you type wrong in here, or in your computer, so
    one way is that you can make a return type, like
    int sum(int a, int b, int s){s = a+b; return s;}
    int main()
    {
    int s;
    s = sum(2, 3, s);
    printf("sum is %d\n“, s);
    return 0;
    }

    and another way is:

    void sum(int a, int b, int &s)
    {s = a+b;}
    int main()
    {
    int s; s = 0;
    sum(2, 3, s);
    printf("Sum is %d\n", s);
    return 0;
    }



    good luck with that!
     
    CPACore, Oct 27, 2010 IP
  7. drhowarddrfine

    drhowarddrfine Peon

    Messages:
    5,428
    Likes Received:
    95
    Best Answers:
    7
    Trophy Points:
    0
    #7
    @CPACore - You must have a really slow internet connection.
     
    drhowarddrfine, Oct 28, 2010 IP
  8. CPACore

    CPACore Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    WHY? How can you tell slow? lol :)
     
    CPACore, Oct 28, 2010 IP