# Decrementing a number in c as an argument

Note: I am a beginner to the world of C language.
I was trying to write a program that calculates the sum of N natural number.
Here it is:

#include<stdio.h>

int sum_of_N_with_recursion(int N);

int main()
{
int n,result;
do
{
printf("enter a natural number : ");
scanf("%d",&n);
if(n<0)
{
printf("%d is not a natural numbern",n);
}
}while(n<0);
result = sum_of_N_with_recursion(n);
printf("sum of the first %d natural numbers is %dn",n,result);

return 0;
}

int sum_of_N_with_recursion(int N)
{
if(N==0)
{
return 0;
}
return  N + sum_of_N_with_recursion(N--);
}


What is my problem?

When I tried to decrement N in the sum_of_N_number() like this sum_of_N_number(N--) but it doesn’t work. Why?

Example: sum_of_N_number(3) should equal to 6 but I got 3!!

Stack Overflow Asked on February 22, 2021

On this line:

return  N + sum_of_N_with_recursion(N--);


You're both reading the value of N and writing the value of N in the same expression without a sequence point in between. Attempting to do so is undefined behavior.

You don't actually need to change the value of N here. You just want to pass in the value N-1, so do that instead:

return  N + sum_of_N_with_recursion(N-1);


Correct answer by dbush on February 22, 2021

## Related Questions

### Why does this usage of C++17 if constexpr fail?

4  Asked on December 5, 2021 by cow

### Can i show all variable results in a div in jQuery?

0  Asked on December 5, 2021 by nippledisaster

### Comparing two generics in compareTo()

2  Asked on December 5, 2021 by chalupabatmac

### arranging the json file base on key value

1  Asked on December 5, 2021 by prateek-singh

### Vue watch method not filtering object

3  Asked on December 5, 2021

### getting pivot table in pandas in the desired format

1  Asked on December 5, 2021

### Passing a variable between HTML pages using JavaScript

2  Asked on December 5, 2021 by robert-little

### Strange, inconsistent behavior in checking whether a python list contains a numpy array

1  Asked on December 5, 2021 by user49404

### How to avoid code redundancy with JAVA generics

1  Asked on December 5, 2021 by cosimoth

### Why is my project no longer updating when I write new code?

1  Asked on December 5, 2021 by stephen-horton

### questions about learning Data Structures and Algorithms in a specific language (noob question)

1  Asked on December 5, 2021 by kchou612

### pm2 restart loop in AWS Elastic Beansalk (Node environment)

1  Asked on December 5, 2021 by tom-schreck

### node https ssl your connection is not private error

2  Asked on December 5, 2021 by andrea-dattero

### Fluent NHibernate Select Max of Count Grouped by property

1  Asked on December 5, 2021 by tamas-toth

### Nested for loop using dict

9  Asked on December 5, 2021 by purwu

### React Hooks – Input loses focus when 1 character is typed in

4  Asked on December 5, 2021 by dragos-strugar

### Mocking Firebase auth signInWithPopUp with jest for React App

1  Asked on December 5, 2021 by user881667

### How do I build a C Python Extension for Windows using Cygwin64?

2  Asked on December 5, 2021 by zenith2198