Hi, I am new in Programming and I wrote this simple C program, This program get executed but I was wondering why I cannot get 70 in output. Thank you in advance.
#include<stdio.h>
void fun(int);
void replace(int);
int main()
{
int a = 30;
fun(a);
printf("%d\n", a);
return 0;
first of all you are returning a int in your function "void fun(int b)" which returns nothing (void), if you want it to return int replace void with int.
and be more precise, why do you expect the output to be 70?
Thanks leryss, I just wanted to see how a function works, I got 30 and 60 but not 70 so I was wondering why? I did the following replacement but still did not get 70. I tried by replacing all void by int and still it doesn't work.
#include<stdio.h>
void fun(int);
int replace(int);
int main()
{
int a = 30;
fun(a);
printf("%d\n", a);
return 0;
in your int main() you called fun() function instead of replace(which returns no value), I was talking about the fun() function that should return int. first you have to call the function if you want to use it, like you called fun() in int main
functions don't call themselves that would be a disaster since libraries could contain thousands of them
#include<stdio.h>
int fun(int); // <--
int replace(int);
int main()
{
int a = 30;
fun(a);
printf("%d\n", a);
replace(1234); // <--
return 0;
}
int fun(int b) // <--
{
b = 60;
printf("%d\n", b);
return b; // <--
}
int replace(int c )
{
c = 70;
printf("%d\n",c);
}
Thanks Leyrss and Kemort. Kemort I run the script you gave me and it works fine. I tried with replace() instead of replace{1234), but that doesn't work. To try myself I used some other random four number , and that works. what is the reason behind this? I noticed even a single digit or a character also works. Thank you so much again.
The reason it doesn't work with just replace() is because the function requires an integer.
The function will take any integer you like and you'll notice the printout is always the same. Also if the function had a return c the returned value would be 70. In both cases you can pass any integer to the function which isn't very useful. Also by not having a return doesn't make much sense with the value returned being undefined.
This might make more sense:
int replace(int c )
1 2 3 4 5
{
int d = c;
printf("%d\n",d);
return c * 2;
}
and in main()
1 2
int x = replace(123);
std::cout << x;// or use printf(...
Hello,
I am trying to calculate the factorial of an Integer and wrote the following program, I can run this Program but cant get the output, can anyone help me please. Thanks in Advance.
/*I want to calculate the factorial of an Integer*/
#include<stdio.h>
int factorial(int);
int mian()
{
int a, fact;
printf("enter the number\n");
scanf("%d",&a);
fact = factorial(a);
printf("Factorial value = %d\n", fact);
return 0;
}
int factorial(int x)
{
int f = 1, i;
for(i=x;i>=1;i--)
f = f*i; /*factorial calculation*/
return (f);
I have compiled this code and it works. I am quiet confused about the output, as it is Boolean it should be 1 and 0, but for b+b it gives 2, can some body explain it briefly please.
Thanks in advance.
#include<iostream>
using namespace std;
int main()
{
bool b =32;
int i = false;
cout<<endl<<b<<endl<<i;
int j = b+b;
bool k = b+b;
cout<<endl<<j<<endl<<k<<endl;
return 9;
bool is designed to have only two values, true or false, and to be used with logical operators && || etc. So anything outside that is undefined behaviour and not worth experimenting with. Sorry to say your program is a waste of time and effort. :)
PS bool + bool has no meaning in just the asame way that bool * boll or bool / bool does. What does true * true mean or sqrt(false) or Cos(true)? Who knows? Who cares? :)