Too many arguments to function 'rand'? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have only used the function twice and it displays the aforementioned error. Can someone explain as to why the compiler does that?
void printrandom()
{
int x = (rand(5)+1);
int y = (rand(5)+1);
printf("%d and %d - a total of %d", x, y, (x+y));
}

It is actually rand(void), which is why you are getting that error.
Try int x = (rand() % 5) + 1;
EDIT as Daniel points out, using % will actually affect the probability. See his link for how to address this issue.

Declaration for rand() function is
int rand(void);
This means that it takes no arguments. Remove 5 from rand. If you want to generate random numbers from 1 to 5, the you can do this as
int x = rand()%5 + 1;

Related

This code should print i=1 but it is printing i=0 Why is it so? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
#include<studio.h>
int main()
{
int a=5,i;
i!=a>10;
printf("i=%d",i);
return 0;
}
This code should print i=1 but it is printing i=0 Why is it so?
That's because you don't do anything to i.
Your "i!=a>10" evaluates to false, but the result is not stored into a variable.
As it is mentioned in the comments, you need something like this:
int a = 5;
int i = !(a > 10);
The != is mostly used in if-clauses, like
if (a != 0) {...}
I hope this helps. ;)
It's not printing because:
the variable i is not initialized
the second statement should be changed and be a variable int i = !(a > 10)
Returning a value and printing it are different things, but that's not the point of the question.

When I execute the following code, there is no output, what could be the cause? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Here is my C code. I am not getting any output. Please help. I also tried adding the initialization of inside the main function, then also I am not getting any output.
#include <stdio.h>
int x = 10;
int main()
{
if (x = 20)
{
x = -1;
}
else
{
printf("x not eqaul to 20\n");
}
if (x > 0)
{
printf("x not greather than 0\n");
}
else
{
/* notjing */
}
return 0;
}
So in the first if-statement you wrote if(x=20). This is not a conditunal argument, this is a mathematical operand.
So x will be set to 20; afterworths it will be set to -1. And no printf() will be called.
If think you wanted to use if(x==20).

Is side-effect operator allowed in C expression with more than a variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to use side effect operator in my expression which does not have just a variable. My program was compiled successfully but I got a runtime error "Segmentation fault"
Here is my code:
int main()
{
int x = 1;
printf(1 + (x++));
return 0;
}
C requires you to format the string, this way it knows what it should print. What you have in your example is nothing but memory addresses, which makes the C compiler confused.
int main()
{
int x = 1;
printf("%d\n", (1 + (x++)));
return 0;
}

Guessing Game - Can't figure out why it produces a large negative integer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Cheers guys. I am having trouble with this code I produced. It functions everything fine but when it prints the number when I guessed correctly, it prints a number like -3529583 which I don't understand. Shed some light?
#include <stdio.h>
#include <ctype.h>
#include <time.h>
int main()
{
int y, iRandomNum1; // Declare the three variables
y = 0;
iRandomNum1 = 0;
srand(time(NULL)); // Randomize function
iRandomNum1 = rand() % 10; // Randomize and collect 1 to 10 Value
while (iRandomNum1 != y) {
printf("Guess a number from 1 to 10\n");
scanf("%d", &y);
}
printf("\nCorrect Guess! Congrats! The answer is %d.\n", &y);
return 0;
}
You are display the address of the variable &y instead of the variable itself in your printf just remove the & symbol and it should be ok
https://stackoverflow.com/users/2173917/sourav-ghosh had the answer before me in comment

Basic While loops doesn't print anything [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to create a while loop in C, it says that build is successful, however it doesn't print out anything. I don't really see whats wrong, it doesn't show anything in the console.
int main()
{
int w = 0;
while (w >=100){
printf("w = %i" , w);
w++;
}
return 0;
}
You define w=0 and in the next line you write "while w is greater or equal than 100", which cannot work.
Try
while (w <= 100)

Resources