my terminal is only printing the first output from main - c

beginner to C here, and I am having trouble printing the words "one" in int one_three() and "two" in in the function().
the only words printing in my terminal is "starting now" - is anyone anyone aware as to why this is happening?
Any help would be very much appreciated.
int one_three();
int two();
int main(void)
{
// Countdown begins
printf("starting now: ");
printf("\n");
int one_three();
int two();
return 0;
}
int one_three()
{
printf("one");
int two();
return 0;
}
int two()
{
printf("two");
return 0;
}

You don't call the functions, you declare the functions (again).
To call e.g. one_three then you do
one_three();
And speaking about function declarations, in C you must explicitly use void as argument if a function doesn't take any arguments. A declaration such as
int two();
tells the compiler that the function two returns an int, and takes an unknown number of unspecified arguments. The proper declaration would be
int two(void);

int one_three();
int two();
int main(void)
{
// Countdown begins
printf("starting now: ");
printf("\n");
one_three();
two();
return 0;
}
int one_three()
{
printf("one");
two();
return 0;
}
int two()
{
printf("two");
return 0;
}
While calling the function you should not declare the type(ie. char, int or float).

You should change the code like this.
...
int main(void)
{
// Countdown begins
printf("starting now: ");
printf("\n");
one_three();
two();
return 0;
}
...

You have declared functions again inside main(). That's why printing only "starting now". You need to call that function from main() like this:
int main(void)
{
// Countdown begins
printf("starting now: ");
printf("\n");
one_three();
two();
return 0;
}

Let me explain you some basics.
The first two lines in your code
int one_three();
int two();
- are function declarations or function prototypes which tells the compiler that there is a function definition after the main function.
If you don't want it, you can keep the function definitions before main function.
int one_three()
{
printf("one");
int two();
return 0;
}
int two()
{
printf("two");
return 0;
}
int main(void)
{
// Countdown begins
printf("starting now: ");
printf("\n");
one_three(void);
two(void);
return 0;
}
You're re-declaring the functions again in main(), where you need to call them.
Pass a actual argument as void if you're not passing any value or reference.
Since you're returning 0 from those functions the return type is int, which will be fine.
int main(void)
{
// Countdown begins
printf("starting now: ");
printf("\n");
one_three(void);
two(void);
return 0;
}

Related

What does it means to have function calling procedure in place of initialization (for loop)?

#include<stdio.h>
int fun()
{
static int num=16;
return num--;
}
void main()
{
for(fun();fun();fun())
printf("%d\n", fun());
}
Here what is meaning of for(fun();fun();fun()) ?
All I know about for loop is that for(initialization;condition;change in variable ) and they(ini..,cond...,chang...) should contain (some variable with value, algebraic condition, ..).
Please correct me.
Your code is equivalent to:
#include <stdio.h>
int fun()
{
static int num=16;
return num--;
}
void main()
{
fun();
while (fun())
{
printf("%d\n", fun());
fun();
}
}
Is it clear now?

Extra 444 returned while calling main function recursively?

#include<stdio.h>
int main()
{
static int s;
++s;
printf("%d",s);
if(s<=3)
main();
printf("%d",s);
}
I'm getting output 12344444 but need only 12344. Can anyone please explain why this problem arises and provide solution?
#include<stdio.h>
int main()
{
static int s;
++s;
printf("%d",s); // this printf is called and will print each
// number as the recursion is called down
if(s<=3)
main();
printf("%d",s); // <<-- this is why
// this one is called as the recursion functions
// return so it will be called with the highest
// number as many times as there were recursion.
}
To get what you want try
#include<stdio.h>
void recursive()
{
static int s;
++s;
printf("%d",s); // this printf is called and will print each
// number as the recursion is called down
if(s<=3)
recursive();
else
printf("%d",s); // call only if s > 3
}
int main()
{
recursive();
}

How to create a dynamically sized global array?

I am making a multi-threaded program to find prime numbers in C. How do I take an input in the following C program and not use (#define N =88)?
I am getting the following error:
main.c:8:7: error: expected declaration specifiers or ‘...’ before string constant
scanf("%d", &N);
^~~~
#include <stdio.h>
#include <pthread.h>
#include <conio.h>
#define MAX_THREADS 4
int N;
scanf("%d", &N);
int prime_arr[N]={0};
void *printprime(void *ptr)
{
int j,flag;
int i=(int)(long long int)ptr;
while(i<N)
{
printf("Thread id[%d] checking [%d]\n",pthread_self(),i);
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0 && (i>1))
{
prime_arr[i]=1;
}
i+=MAX_THREADS;
}
}
int main()
{
pthread_t tid[MAX_THREADS]={{0}};
int count=0;
for(count=0;count<MAX_THREADS;count++)
{
printf("\r\n CREATING THREADS %d",count);
pthread_create(&tid[count],NULL,printprime,(void*)count);
}
printf("\n");
for(count=0;count<MAX_THREADS;count++)
{
pthread_join(tid[count],NULL);
}
int c=0;
for(count=0;count<N;count++)
if(prime_arr[count]==1)
printf("%d ",count);
return 0;
}
You can't have general statements outside of functions.
The simple solution is to read the input in the main function and then create the array in the main function as well.
And keep the array as a local variable inside the function, and pass it as an argument to the functions to call (together with its size).
To mass multiple arguments to the thread function, create a structure with the "arguments" as members, and pass a pointer to one such structure.

How to create multiple functions in C

I am new to C programming and I am trying to create functions. The first function executes but the second one doesn't.
#include <stdio.h>
char get_char();
int main(void)
{
char ch;
printf("Enter a character > ");
scanf("%c", &ch);
return ch;
}
int get_int()
{
int i;
printf("Enter an integer between 0 and 127 > ");
scanf("%d", &i);
return i;
}
For anyone else that arrives here, you may solve your problem, by making sure your main function is at the bottom of the file.
Why? Because if function a calls function b, a should be before b.
main is the entry point for your program. The C environment calls main when your program is executed. get_int() is not the name for an entry point, so the fact that you never call it directly or indirectly in main means it will never be executed.
You also didn't declare it before main meaning your compiler will warn about not finding it, but since get_int returns int it will link successfully regardless.
Fix:
int get_int();
int main ()
{
//...
}
int get_int()
{
//...
}
Your second function isn't called and it should be as follows:
int main()
{
int m = 10;
get_int(m);
return 0;
}
int get(int num)
{
int multiply = num * num;
return multiply;
}

Calling pthread_create error - expected primary expression before 'void'

This is a Matrix multiplication code. It creates a thread to multiply each row of the first matrix to the second matrix and saves the result in matrix C.
It gives an error in the pthread_create line expected primary-expression before 'void'.
I run this code on ubunto 13.10 virtual machine.
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct matrices
{
int matrixA[10][10];
int matrixB[10][10];
int matricC[10][10];
int r1,r2,c1,c2;
}*ptr;
int p;
void *matrixMul(void *);
int main()
{
int i=0,j=0;
pthread_t threads[10];
ptr=(struct matrices*)malloc(sizeof(struct matrices));
printf("Enter size of first matrix(Rows then Columns)");
scanf("%d",&(ptr->r1));
scanf("%d",&(ptr->c1));
printf("Enter elements of first array : ");
for(i=0; i<ptr->r1; i++)
{
for(j=0; j<ptr->c1; j++)
{
scanf("%d",&ptr->matrixA[i][j]);
}
}
printf("Enter size of second matrix(Rows then Columns)");
scanf("%d",&(ptr->r2));
scanf("%d",&(ptr->c2));
if(ptr->c1!=ptr->r2)
{
printf("Dimensions ERRORR! ");
}
else
{
printf("Enter elements of second array : ");
for(i=0; i<ptr->r2; i++)
{
for(j=0; j<ptr->c2; j++)
{
scanf("%d",&ptr->matrixB[i][j]);
}
}
for(i=0;i<ptr->r1;i++)
{
for(j=0;j<ptr->c2;j++)
{
ptr->matricC[i][j]=0;
}
}
for (p=0;p<ptr->r1;p++)
{
**********pthread_create(&threads[p],NULL, *matrixMul,void &p);**********
}
for(i=0;i<ptr->r1;i++)
{
pthread_join(threads[i],NULL);
}
for(i=0;i<ptr->r1;i++)
{
for(j=0;j<ptr->c2;j++)
{
printf("%d",ptr->matricC[i][j]);
}
}
}
return 0;
}
void *matrixMul(void *rownum)
{
int *i;
int n=0,m=0;
i=(int*)rownum;
for(n=0;n<ptr->c2;n++)
{
for(m=0;m<ptr->c1;m++)
{
ptr->matricC[*i][n]+=(ptr->matrixA[*i][m])*(ptr->matrixB[m][n]);
}
}
return NULL;
}
Your code contains minor error, but the logic is correct, don't worry.
I downloaded the code and tested it on my machine, so please note the following:
This line should be written this way...
pthread_create(&threads[i],NULL, matrixMul, &i);
Because according to the specs of pthread library that pthread_create should take void pointer to the runner function and void pointer to the parameter. You don't need to add (void *) because you already declared your runner function matrixMul as void *.
Your primary error here was (void) &i and it should be &i only as you already delcaired this parameter as void * in the runner function's prototype. You shall too pass the runner function like this &matrixMul.
Some other notes: "Code Review"
You shouldn't put your logic in the else statement, you can simply after printf("Dimensions ERRORR! "); write exit(-1); because this is basically what you do if the dimensions error.
Check for the return value (status) of pthread_create and pthread_join

Resources