Why isn't main() returning any value? - c

/I am trying to return the 1st bit of boolean value of 10 using right shift in the cb function./
#include<stdbool.h>
bool cb(int N,int i){ //`called function`
return ((N>>i)&1)==1;
}
int main(void) { //`main function`
cb(10,1);
return 0;
}
//Status:successfully Executed,but no output.

main doesn't magically return the result of another function, you need to return the value also from main
int main(void)
{
return cb(10, 1);
}
or you can exit the program from your function with a value:
bool cb(int N,int i){ //`called function`
exit(((N>>i)&1)==1 ? EXIT_FAILURE : EXIT_SUCCESS);
}
and check the return in the console:
./yourprogram
echo $?
But notice that this is considered bad practice, we usualy return EXIT_FAILURE only when something went wrong, instead, you can print the result:
int main(void)
{
printf("%d\n", cb(10, 1));
return 0;
}
Finaly, you can use a debugger
Change your code to
Line 6 int res = cb(10, 1);
Line 7 return 0;
and start the debugger
gdb yourprogram
breakpoint 7 (after the line you want to inspect)
run
print res

So Here's your program:
#include<stdbool.h>
//`called function`
bool cb(int N,int i)
{
return ((N >> i) & 1) ==1;
}
//`main function`
int main(void)
{
cb(10,1);
return 0;
}
Your program is executing - which means that the main() function is returning successfully (a Value of 0). You also invoke cb(10,1); which calls your function declaration above (and returns a boolean: True/False). But you don't store the value of that function call, nor display the value with a printf() or cout statement.
You'll need to add more for your program to give you more noticable output.

Related

Creating a loop using setJmp longJmp

I am trying to create an infinite loop using those functions (unsuccessfully).
My code :
jmp_buf buf1;
void foo(){
int z = 4, y = 1;
int v = setjmp(buf1);
if(v == 0){
printf("A%d", z);
longJmp(buf1, 1);
}
else if(v == 1){
printf("B%d", y);
longjmp(buf1,1);
}
}
int main(){
int v = setjmp(buf1);
if( v == 0){
printf("C%d", 1);
foo();
longjump(buf1,1);
}
}
I thought it would print C1A4B1B1B1.... (B1 repeats forever) but I just get C1A4B1 and the program stops (SEGMENTATION ERROR).
Isn't calling longJmp always return to setJmp with parameter val?
Would like to understand where is my mistake.
EDIT : The code I copied before I got answers is mistakenly incorrect, longjmp(buf1,2) instead of longjmp(buf1,1).
Also I didn't copy the full main code which cause the seg fault. (another longjmp after foo being called)
after line 15, longjmp(buf1,2);
v is 2.
When next repeat, v is not 1, function has end.
longjmp(buf1,2); > longjmp(buf1,1);

Segmentation fault when calling functions recursively

I'm writing a program that prints infinite numbers.
#define false 0
#define true 1
int test(int idx) {
printf("%d\n",idx);
test(idx+1);
return 0;
}
int main() {
test(0);
return 0;
}
// Segmentation fault: 11
This program ended with a segfault after printing 262045.
I understand it is caused by stack overflow.
Is there any clever trick that can make the recursion go deeper?
Like calling another recursive function when it reaches a certain number and clears the stack?
I tried doing this.
#define false 0
#define true 1
int test2(int idx) {
printf("test2 here\n");
printf("%d\n",idx);
test2(idx+1);
return 0;
}
int test(int idx) {
if (idx == 262000) {
return test2(idx);
}
printf("%d\n",idx);
test(idx+1);
return 0;
}
int main() {
test(0);
return 0;
}
But the stack is not cleared. There is still a segfault after printing 262044.
In the first snippet, you're hitting a stac overflow.
In the second case, it invokes UB because of signed integer overflow.

Recursive function is not working with a pointer

This function aims to return the number of zeroes in a number, num. The function rCountZeros2() passes the result through
the pointer parameter result.
`
void rCountZeros2(int num, int *result)
{
if (num==0)
return;
else
{
if (num%10==0){
(*result)++;
}
rCountZeros2(num/10, result);
}
}
`
See when you are invoking rCountZeros2() , my guess is value in variable result is not zero.It may be some garbage value or some other value from previous computation.However with details you have provided it is difficult to provide exact answer.
Kindly try the following standalone program, I got correct answer using your code
void rCountZeros2(int num, int *result)
{
if (num==0)
return;
else
{
if (num%10==0){
(*result)++;
}
rCountZeros2(num/10, result);
}
}
int main()
{
int result = 0;
int num=12300000;
rCountZeros2(num, &result);
printf("number of zeros in %d = %d",num ,result);
}

./a.out: file not recognized: File truncated collect2: error: ld returned 1 exit status?

I wrote a small program that fills an array with prime numbers which are returned from a separate function. The program compile fine but when i go to run the executable i get the above error. Here is my source code. I'm not really sure why its happening. Could anyone here do me the huge favor of compiling and running the following code? I'm wondering if its something on my laptop but i really have no clue. I'm running linux mint if that helps at all.
#include <stdio.h>
int prime(int x);
int main()
{
int intergers,index,return_value;
int array[100]={2}; /*intialize array[0] being 2*/
for(index=1;index<100;index=index+1) /*begin array index at array[1]*/
{
for(intergers=3;intergers<102;intergers=intergers+1) /*check for prime numbers starting from 3*/
{
return_value = prime(intergers);
if(return_value==999)
array[index]=-1;
else
array[index]=return_value;
}
}
printf("hello world");
}
int prime(int x)
{
int divisors,count,value;
for(divisors=2;divisors<x;divisors=divisors+1)
{
if(x%divisors==0)
{
value=999;
break;
}
else
{
value=x;
}
}
return value;
}
The following code compiled and linked without errors or warnings from the gcc compiler on SuSE SLES 11 using the following command: gcc -Wall -o test test.c
#include <stdio.h>
int prime(int x);
int main()
{
int intergers,index,return_value;
int array[100]={2}; /*intialize array[0] being 2*/
for(index=1;index<100;index=index+1) /*begin array index at array[1]*/
{
for(intergers=3;intergers<102;intergers=intergers+1) /*check for prime numbers starting from 3*/
{
return_value = prime(intergers);
if(return_value==999)
array[index]=-1;
else
array[index]=return_value;
}
}
printf("hello world\n");
return(0); // Added this line to rid compiler: warning: control reaches end of non-void function
}
int prime(int x)
{
int divisors,/* count, */ value;
for(divisors=2;divisors<x;divisors=divisors+1)
{
if(x%divisors==0)
{
value=999;
break;
}
else
{
value=x;
}
}
return value;
}
I then executed the program:
> ./test
hello world
The code executed as expected.

Return values in C

void draw( int I, long L );
long sq( int s );
int main()
{
long y;
int x;
y = sq( x );
draw( x, y );
return();
}
long sq( int s )
{
return( s * s );
}
void draw( int I, long L )
{
return;
}
What is the difference between return(), return (S*S) and return? Please give an explanation.
Well:
return(); is illegal, have you tried compiling?
return(s*s) is the same as return s*s; and it tells the function what value to return.
For example, if you would have:
long x = sq(1);
//x would be 1 here
return; exits from a void function. You can't put an empty return statement inside a function with a non-void return type. Put at the end of a void function, it does nothing. But you can use it to exit the function early:
void foo()
{
if ( someCondition )
return;
statement1;
statement2;
return;
}
The first return has the effect that it will exit the function if someCondition is true. So the statements will not be executed. The second return makes no difference whatsoever.
There is hardly any difference.
Basically there are two syntaxes.
First of all:
return somethinghere;
is exactly the same as
return (somethinghere);
You can replace "somethinghere" with anything you want (as long as it qualifies with the function's return type), an equation another funciton anything that has a value, including simply nothing if the return type is "void".
If you put nothing, then it means you function returns nothing, otherwise you are returning the result of whatever you have placed there.

Resources