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
For an assignment i must define a variable N as 100, then recall that variable in a printf statement. the code looks like :
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#define N 100
int main ( )
{
...
printf("Try to guess a number between 1 and N \n\n") ;
...
}
The N is just coming out as N rather than 100.
This is because everything between double quotes are considered as a character array, i.e. a string. So if you want to show N in the string, you shall use it as an "usual" variable:
printf("Try to guess a number between 1 and %d \n\n", N) ;
#define won't expand in literal character string (block of character between "). You should write:
printf("Try to guess a number between 1 and %d \n\n", N)
Related
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
i'am triyng to solve this simple exercise for my code class but i tried everthing and have no ideia why this code inst working
// Develop a modular structure with a function that you receive through
// parameter a positive integer and returns the number quantity of this number.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
int digitos(float N){
int i;
for(i = 0;i<10;i++){
printf("%f\n",N/pow(10,i));
if((N/pow(10,i))<0.0){
return i;
}
}
}
int main(){
int d ;
d = digitos(3000);
printf("The number of digist %d",d);
}
Looks like an issue with your math. You're never going to get a negative number unless N itself is negative. Did you mean:
if ((N/pow(10, i)) < 1.0) {
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
#include <stdio.h>
#include <stdlib.h>
#define SIZE 13
int main(){
int die1, die2, sum, i, occurrences[SIZE], j=2;
for(i=0; i<36000; i++){
die1=1+rand()%6;
die2=1+rand()%6;
sum=die1+die2;
++occurrences[sum];
}
printf("%10s","Sums");
for(i=1; i<=12; i++){
printf("%4d", i);
}
printf("\n%10s","Occurrences");
for(i=2; i<=12; i++){
("%4d",occurrences[i]);
}
return 0;
}
Why doesn't this code work? The program has to sum all the random numbers generated by two dies and then it has to print the occurrences of the sums. Why doesn't it work? The output is this:
Sums 1 2 3 4 5 6 7 8 9 10 11 12
Occurrences
The array occurrences[SIZE] is not initialized and has indeterminate values, so the addition ++occurrences[sum]; will invoke undefined behavior. You should initialize it like occurrences[SIZE] = {0}.
The line ("%4d",occurrences[i]); is just a expression using comma operator surrounded by parenthesis, so it won't do printing. It looks like printf should be added before the parenthesis.
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
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'm struggling to get Char to work. It keeps returning an error.
#include <stdio.h>
#include <cs50.h>
int main (void)
{
int tower_height;
char #;
// Inputs
do {
printf("Let's build! Give me a number between 0 and 23 'inclusive'.\n");
tower_height = GetInt();
}
while
(tower_height < 0 || tower_height > 23);
// Outputs
for (tower_height = 0; tower_height <= 23; tower_height++)
printf ("%c = tower_height - 2\n");
}
C identifier names may contain letters, underscore, and digits, as long
as the first character isn't a digit, and as long as the identifier
isn't a keyword. They may not contain #.
# is not a valid variable name.
As pointed out, # is not a valid variable name.
You can see how # is properly used in the first line of your code: #include <stdio.h>
Instead, call your char variable something that uses letters and numbers eg: char 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
Please tell me, what is wrong here,it compiles but console crashes when i enter the number. I don't know what to write next, i will just write something to make my post possible.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
unsigned int i,l,p,w;
printf("Enter natural number excluding 0: ");
scanf("%d",l);
p = 1;
for(i=1;i<=l;i++)
{
p=p*i;
}
w=p;
printf("\nFactorial of entered number %d",w);
return 0;
}
scanf("%d",l); you need to insert the address of l, which is &l.
You should also use %u for unsigned ints, not %d.