Repeating a task/block of code X times (introduced by user) [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 5 years ago.
Improve this question
I recently started coding in c. I was wondering how you can repeat/loop a task as many times as the user wants (by input).
int a,i;
scanf("%d", a);
for(i=0; i<a; i++){...}
This is the code I came up with, but it doesn't work. It's an infinite loop.

You need a & to scan into.
int a,i;
scanf("%d", &a);
for(i=0; i<a; i++){...}
Read a bit more about scanning integers here.
How to scanf only integer?

Change scanf("%d", a) to scanf("%d", &a)
The code didn't work for you because you failed to store the value entered by the user.
Before modifying your code as my suggestion try to print the value of a in your code, you'll get it, why it's not working..

The problem is in line:
scanf("%d", a);
You have to use:
scanf("%d", &a);
& sign represents address, you want to store input value on the address of variable a.
Read more about ampersand here:
When should I use ampersand with scanf()

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.

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.

New to C and can't figure out these outputs [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 year.
Improve this question
Hi I am new to c and trying to build just a simple print array however I just get what it wants to give me. Here is my code:
#include <stdio.h>
int main(){
int n[5]={5,10,15,20,25};
int i;
printf("displaying integers:");
for ( i=0; i<5; i++)
{
printf("%d \n", &n[i]);
}
return 0;
}
And the output is:
displaying integers:6422280
6422284
6422288
6422292
6422296
Any help would be great I tried creating it as an enter integers and get an output but regardless of input it gave me extremely large numbers. which is why I'll be happy if it just prints. Sorry if it's an obvious one but I've tried 5 different ways all with similar/basically identical results.
What you get printed are address locations of items stored in array.
If you want to print values of items in array, you should not use the address-of operator [ & ]. Try it this way:
printf("%d \n", n[i]);

Using %c in scanf and assigning the value to an int variable 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 2 years ago.
Improve this question
int main()
{
int i= 0;
printf("i value is %d\n",i);
scanf("%c", &i); // I am giving an input of 255 here
printf("i after scan %d\n",i); // This prints 50. How???
return 0;
}
Can someone explain how does the printf statement give 50? I have a little-endian machine.
Your program won't even compile as I is undeclared. I am assuming that it is a typo. Since you are scanning %c it will read only one character which is 2 from 255. Now 2 has ascii code of 50 which is being printed.

Program with array to find a sum 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 4 years ago.
Improve this question
I was thinking of a code to find the sum of numbers.
So i wrote bellow code :
#include <stdio.h>
#include <math.h>
int main()
{
int n,i,s=0,a[100];
printf("enter number of numbers");
scanf("%d",n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
s=0;
for (i=1;i<=n;i++)
{s=s+a[i];}
printf("sum is%d\n",s);
}
But in the output it shows segmentation error . I.e.
So whats the mistake?
So this line:
scanf("%d",n);
should be replaced by
scanf("%d",&n);
Explanation:
scanf() reads data from stdin according to format and store data in the location pointed by next additional argument. this case, format is %d means we want to read an integer number and this integer number will be stored in the location of n. The & operator is to get the location of a variable in C.
Use this :
scanf("%d",&n);
The reason :
You MUST put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it.
Examples :
scanf("%d %d", &a, &b);
printf("%d %d", a, b);
As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values of a and b and but they are local to scanf(). So in order to reflect changes in the variable a and b of the main function, we need to pass addresses of them. We cannot simply pass them by value.
But in case of printf function as we are only going to print the values of the variables in output console, there are no changes going to be made in variable a and b’s values. So it is not required to send their addresses.

Resources