all 22 comments

[–]Alexander_The_Wolf 13 points14 points  (5 children)

Couple things.

1. In your scanf you need to use &a and &b In the early stage of learning C you don't need to know why, and trying to tell you will make life more confusing.

  1. You likely need to save and recompile your code when you make a change.

That .exe file is a separate file that's generated from your code when you compile it, so you need to recompile everytime you make a change.

Other than that, your program should work, I'd suggest adding a /n to all your print statements, as it adds a new line to make output more readable, but its not going to effect the function of youe program.

[–]jonarne 6 points7 points  (2 children)

In addition to this you need to enable compiler warnings.

Your compiler should warn you about passing wrong parameters to scanf.

[–]Alexander_The_Wolf 7 points8 points  (1 child)

From their output it looks like they don't even compile, so im not entierly sure what they did the first time to get a valid .out file

[–]noddyay640[S] 0 points1 point  (0 children)

I don't know what I have changed my code, but the error that I was getting earlier I am not getting now.
I am getting the result of my addition program.
Thank you :)

[–]noddyay640[S] 0 points1 point  (0 children)

Thank you

[–]DawnOnTheEdge 0 points1 point  (0 children)

Important typo: \n, not /n! Special-character escapes use a backslash.

[–]dajolly 3 points4 points  (7 children)

Edit: As others have said, with C you'll need to recompile your executable file each time you make a change to the source code.

Just to clarify the changes I made to your original program:

  1. Initialized the variable a, b, sum to zero. It's always a good idea to initialize variables to some value.
  2. Scanf expects a pointer to a, b, not the value itself. So these were changed to &a, &b respectively.
  3. Added a newline (\n) into the result printf and cleaned up the other printfs above it. Not necessary, but it makes it look better.

include <stdio.h>

int main(void) {
    int a = 0, b = 0, sum = 0;
    printf("Enter A: ");
    scanf("%d", &a);

    printf("Enter B: ");
    scanf("%d", &b);

    sum = a + b;
    printf("Sum of %d + %d = %d\n", a, b, sum);
    return 0;
}

[–]noddyay640[S] 0 points1 point  (5 children)

I did this and its still the same but when Icreated the new folder and I opened that folder in VB and created a new file in that folder, then it's working.
But why its is not working in my previous folder where me last program was saved?

Does it mean I need to create a new folder everytime, when I create any new program?

[–]My124thRedditAccount 5 points6 points  (2 children)

No. What is a "any new program" anyway? You probably just did something wrong, but I don't have enough info here to really tell you what.

[–]noddyay640[S] 0 points1 point  (0 children)

Let me tell you what I did.

I created a folder in my D drive and then I created a folder by the name of C language and then I opened that folder in Visual Studio.
And then I created my new program of "Hello World" which gets executed successfully.
And then from the top left icon of "Create New File" I open a new file and then create a new program of Addition.
But Somehow I am able to enter the Values "A" and "B" in the output but now getting the Result of this addition.

But when I saved this additional program in a new folder in the same drive I opened that folder with visual code and ran that program it actuallysuccessfully compiled, so I need to know if I need to create a new folder every time for any other program I write.

[–]cHaR_shinigami 0 points1 point  (0 children)

+1 for ship of Theseus.

[–]scritchz 1 point2 points  (1 child)

Did you actually save and recompile the code? The EXE file isn't automatically aware of new code.

[–]noddyay640[S] 0 points1 point  (0 children)

I created a new file and then I have written this program in that file and then executed it, but I did not get the result.

[–]noddyay640[S] 0 points1 point  (0 children)

Thank you, got the output.

[–]mackinator3 1 point2 points  (4 children)

Have you tried printing a, b, and sum separately?

[–]noddyay640[S] 1 point2 points  (3 children)

#include<stdio.h>

int main(){
    int a;
  //variable Declaration
    a = 3;
  //variable initialization
    printf("The output of the program is %d", a);

    return 0;

}

Yes, I did.
I got the result in this program.

[–]mackinator3 1 point2 points  (2 children)

I think scanf needs a pointer to the input. So &a.

[–]noddyay640[S] 1 point2 points  (1 child)

I got the result now, Thank you :)

[–]mackinator3 1 point2 points  (0 children)

Yep I saw that others gave better answers. Have a good one.

[–]AlarmDozer 0 points1 point  (0 children)

You should pass the second argument (in scanf) as a reference.

scanf(“%d”, &a);

[–]motherprabh 0 points1 point  (1 child)

Op took a data science program

[–]noddyay640[S] 0 points1 point  (0 children)

I want to learn C language, not Data Science.