int variable should have value 45, is printed as 2358648 [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 7 years ago.
Improve this question
How this answer is come
#include<stdio.h>
#include<conio.h>
int main() {
int i, sum;
for (i = 0, sum = 0; i < 10; i++)
sum += i;
printf("%d", & sum);
return 0;
}
Output
2358648

You are printing addess instead of value
just remove & in printf
printf("%d",sum);

Related

sum += (i*8); beginner can some one explain the problem I’m getting confused [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 months ago.
Improve this question
sum += (i\*8);
Can some one explain the problem? I, as a beginner, I am getting confused.
#include<stdio.h>
int main(){
int sum = 0;
for (int i == 1; i < 11; i ++)
{
sum += (i*8);
} I
printf("The value of sum is %d", sum);
return 0;
}
can someone explain
Instead of write i == 1, write i = 1. That assigns 1 to the variable i.
i == 1 : comparison
i = 1 : assignment

The program is not terminating [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<stdio.h>
int reverse(int );
int main()`
{
int num,rem,r;
printf("Enter a number:");
scanf("%d",&num);
r=reverse(num);
r=reverse(num);
return 0;
}
int reverse(int num)
{
int rem,rev=0;
while(num>=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
return rev;
}
Its not showing any errors and after entering the number the program stay still, its neither showing any output nor terminating
You have not used printf for output.
r = reverse(num);
printf("%d",r);
return 0;
Use this.
And also while condition should be
while (num > 0)
There is no need to call reverse function twice as well.

How can I insert a new line after 10 characters in C? [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 6 years ago.
Improve this question
I'm reading data from standard input one character at a time and outputting the results to standard output. Every 10 characters, I want to have a new line. For example:
Input-
123456789101112
Output-
1234567891
01112
This is what I have so far, but it doesn't work.
if(numChars = 10) {
printf("\n");
numChars = 0;
}
#include <windows.h>
int main()
{
char test[] = "123456789101112123133431234567891011121231334312345678910111212313343";
int i = 0;
while (i < strlen(test))
{
printf("%c", test[i++]);
if (i % 10 == 0) {
printf("\n");
}
}
return 0;
}

printing too much stars [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 type this code on Ubuntu 14.04
#include<stdio.h>
int main() {
int i, j, n;
scanf("%",&n);
for (i=0;i<=n;i++){
for (j=0;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
but it print too much stars and it Continue until I close it.
Should be:
#include<stdio.h>
int main() {
int i, j, n;
scanf("%d",&n);
for (i=0;i<=n;i++){
for (j=0;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
You forgot to tell scanf you were reading in an integer by using the %d argument
There is mistake in
scanf("%",&n);
it will be:
scanf("%d",&n);
to tell the complier that the value of n is an integer type.

C - sizeof(string) result is less than it should be [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I know that a string should be n+1 in length, but for some reason my program is printing the sizeof(string) as n-2.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name [] = "Tom";
int x = sizeof(name);
int i;
printf("sizeof(name) = %d\n", i);
for(i = 0; i < x; i++)
{
printf("Character at %d is %c\n", i, name[i]);
}
return 0;
}
Can anyone explain why?
You're printing i, not x.
i was never initialized, so you get undefined behavior.

Resources