I am getting absurd values for the variable 'credits' [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 6 years ago.
Improve this question
#include <stdio.h>
int main (void)
{
int hist,geo,phy,chem,bio;
int credits=0;
printf("Enter marks in history : ");
scanf("%d",&hist);
if(hist>40)
credits =10;
else
printf("No credits awarded for history");
printf("Credits obtained is %d",&credits);
return(0);
}
when I run the code, and I get a value of 230586 for the variable 'Credits'. Please help. I am a beginner in C

&x is like asking a question "What is the address of variable x?" , that's why you get the strange number. In order to print the variable value, please pass credits instead of &credits to the printf function.
printf("Credits obtained is %d", credits);

Related

I can compile my code without getting errors, but it's not working anyways [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 last month.
Improve this question
#include<stdio.h>
#include<conio.h>
int main(){
int marks[3];
printf("enter number for first array");
scanf("%d",marks[0]);
printf("enter number for second array");
scanf("%d",marks[1]);
printf("enter number for third array");
scanf("%d",marks[2]);
printf("the value of first array is %d",marks[0]);
printf("the value of first array is %d",marks[1]);
printf("the value of first array is %d",marks[2]);
return 0;
}
i was expecting that it will print value of matrix but it keeps saying this progarm has unexpectly closed down
scanf() expects you to pass the address of the variable to fill, eg:
scanf("%d",&marks[0]);
& is the address-of operator which provides that.

C math on user input [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 last month.
Improve this question
I'm new to c and I'm trying to preform maths on a user input, specifically the year of a date.
When I try to divide , or do any maths on the variable 'y' and store it in 'fpy' it will always print the value '6422032' or close to it. Any help would be great, I've been trying for hours now.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int d,m,f,y,spY;
//char y[3];
printf("Day from date calculator \n");
printf("Please enter the date in DD/MM/YYYY formate. For example , 1/7/1440 \n");
printf(">");
scanf("%d/%d/%i",&d,&m,&y);
int fpy = y/10;
printf("%d",&fpy);
return(0);
}
this
printf("%d",&fpy);
should be
printf("%d",fpy);

its giving me random numbers [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 2 months ago.
Improve this question
I'm practicing some simple code. When my int number is 12 the output is -743998012 for some reason even with different numbers too.
here is my input code
#include<stdio.h>
int main()
{
int favnum;
printf("Enter number: ");
scanf("%d", &favnum);
printf("My favorite number is %d.", &favnum);
return 0;
}
I try to change variables like float or double didn't work
As said by user3121023, you need to remove the & in printf("My favorite number is %d.", &favnum);.
This is because when you add it, it means that you're not passing its value, but its address in the memory (the pointer where the variable favnum is stored). That's why in the line before you use & : you want to store the input of the console in the right memory location, where the variable favnum is.

Digits of number and output [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
I have a problem that this code doesn't end with output.
I appreciate if help me.
#include <stdio.h>
int main(){
int number,counter=0;
scanf("%d",&number);
while (number!=0){
number=number/10;
counter++;
}
printf("the number has %d digits",&counter);
return 0;
}
You should remove "&" from your printf statement.
& is for scanning not printing
the correct form is :
printf("the number has %d digits",counter);

Dev C keeps returning a wrong value [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 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
main()
{
int n;
printf("Introduce un número entero\n");
scanf("%d", &n);
printf("Has introducido el número: %d", &n);
}
Every time I run this C code I get 6487628 for n, I have uninstalled and installed it over and over again and it keeps doing that, I don´t know what else to do.
You don't want the &n in the printf(), you want n. You are displaying the memory location n is stored in

Resources