How Can I get input in arrays in C? - c

#include <stdio.h>
#include <math.h>
int main()
{
int numbers[5];
printf("Enter your first number:%d");
scanf("%d", numbers[0]);
printf("Enter your second number:%d");
scanf("%d", numbers[1]);
numbers[3]=numbers[0]+numbers[1];
printf("Your desired result is:%d",numbers[3]);
return 0;
}
I seem to find no problem in the code but it won't even let me input numbers in the array I declared

The issue is in the second argument of scanf.
scanf("%d", numbers[0]);
it expects an address of where to write your input to but you are passing the value of numbers[0]. So scanf will write to whatever is in numbers[0] which is an int, not a pointer.
Take the address of numbers[0] by changing it to:
scanf("%d", &numbers[0]);

Related

Seg fault after 2 scanf to pointers

I am getting my head wrapped around pointers, and I get a segmentation fault on this code, for no apparent reason.
Basically this works:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *a;
printf("Please enter a number: ");
scanf("%i", *(&a));
printf("1st number is: %i\n", *a);
}
And this doesn't:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *a, *b;
printf("Please enter a number: ");
scanf("%i", *(&a));
printf("Please enter the second number: ");
scanf("%i", *(&b));
printf("1st number is: %i\n", *a);
printf("2nd number is: %i\n", *b);
}
Just why?
You have declared two pointers to integers, without ever declaring or allocating any memory that those pointers could point to. Rather, declare two integers on the stack, and pass pointers to them:
int a, b;
scanf("%i", &a);
scanf("%i", &b);
printf("Numbers: %i %i\n", a, b);
The fact that your first example works is out of pure luck--it's very likely that a seemingly-valid pointer was left on the stack at just the right location for the example to barely work.
Neither of them should work. If they work it's because undefined behavior includes the behavior of working properly.
Your main problem is that you are writing to an uninitialized pointer. I don't see why you want to use pointers here. If you want to, you have to make them point to something. This is one way:
int x;
int *a = &x; // Pointer that points to x
Or you could just remove the pointers completely:
int a;
printf("Please enter a number: ");
scanf("%i", &a);
printf("1st number is: %i\n", a);
And doing *(&a) is just unnecessary. It means exactly the same as a.

Simple for loop running infinitely

I'm new to C and I need help with this simple exercise using for. I need to get a char and an int value from the user. Then I have to print that char as many times as the int entered before.
This is what I have:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
printf("Enter a character:");
scanf(" %c", &a);
int n;
printf("Enter a number:");
scanf(" %c", &n);
printf("\n");
int x;
for(x=0; x < n; x++){
printf(" %c", a);
}
return 0;
}
My problem is that it makes an infinite loop in the for.
Please I need your help.
Thanks
Here, n is an int, not a char. Thus, you need to use %d to read it. Using %c here will cause undefined behavior. According to "C99 – ISO 9899-1999":
§7.19.6.2 The fscanf function1
[...] If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
Change
scanf(" %c", &n);
to
scanf(" %d", &n);
Check out here for more info.
1: The scanf function is equivalent to fscanf with the argument stdin interposed
before the arguments to scanf.

C program array and for loop exercise

I have to write a function which checks if a value is located in an array with N elements using for loop.I wrote the code
#include <stdio.h>
int main ()
int N[100],n,i;
printf ("Write the value of n:");
scanf ("%d",&n);
i=0;
printf ("Write the value of the element :");
scanf ("%d",&v[i]);
for (i=0,i<n,i++)
{
if (N[i]==n)
}
printf ("The value is located in the array :");
return 0;
When I compile it,it says syntax error before printf.What does this mean?What have I done wrong?
Basic syntax issues. Try:
#include <stdio.h>
int main(void)
{
int N[100],n,i;
printf ("Write the value of n:");
scanf ("%d",&n);
i=0;
printf("Write the value of the element :");
scanf("%d", &v[i]); /* v doesn't exist */
/* you are looping up to n, which could be anything */
for (i=0; i<n; i++)
{
/* you never populate N, so why would you expect n to be found?
the value of any element in N is indeterminate at this point */
if (N[i]==n)
{
printf ("The value is located in the array :");
}
}
return 0;
}
That said, you have logical problems here:
v is not declared anywhere.
You never populate your array (N).
n is a value entered by the user, not the upper bound of the array. What if I enter 101?
Those are more than syntax issues, you'll need to fix your logic.

Why my program combines two printf commands?

So here is my program. It is suposed to write out square of some intiger.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
When i run a program in Eclipse it first requires me to input a number.I put in 5. And then as output it gives me
Type an intiger.Square of that intiger is 25.
It should first print "Type an intiger" and then the rest. But it just combines two printf commands. What is the problem?
You need a newline character - printf("Type an intiger.\n");
In computing, a newline, also known as a line break or end-of-line
(EOL) marker, or simply break, is a special character or sequence of
characters signifying the end of a line of text.
Also format specifier for integer is %d
scanf("%d", &a);
printf("Square of that intiger is %d", a*a);
If you want it on separate lines you can always add '\n' to the string to get a new line.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.\n");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
There is 2 problem in it. First, if you input the integer, it should be %d. Example :
scanf("%d", &a);
The second, after the input, you should print \n. So, it will be like this printf("\n");. Take a look at my code :
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%d", &a);
printf("\nSquare of that intiger is %d", a*a);
return 0;
}
In code::blocks it compiles fine anyway put a \n at the end of the first printf and change %i with %d

How to fill a 2D array in C with user input values?

Note: This is a homework question.
Use FOR construction to fill 2D board with values that were given by
user. The program asks for board size n, m and then it asks for each
board value.
My try
#include <stdio.h>
int main(){
printf("Enter the number of columns");
int i = scanf("%d",&i);
printf("Enter the number of rows");
int y = scanf("%d",&y);
int r[i][y];
int a;
int b;
for (a=0; a<i; a++){
for(b=0; b<y; b++){
int r[a][b] = scanf("%d",&a,&b); //bug
}
}
}
Bug: c:13 variable-sized object may not be initialized
EDIT:
#include <stdio.h>
int main(){
printf("Enter the number of columns");
int i;
scanf("%d", &i);
printf("Enter the number of rows");
int y;
scanf("%d", &y);
int r[i][y];
int a;
int b;
for (a=0; a<i; a++){
for (b=0; b<y; b++){
scanf("%d",&r[a][b]);
}
}
}
scanf takes the address of the variable that is being read and returns the number of items read. It does not return the value read.
Replace
int i = scanf("%d",&i);
int y = scanf("%d",&y);
by
scanf("%d",&i);
scanf("%d",&y);
and
int r[a][b] = scanf("%d",&a,&b);
by
scanf("%d",&r[a][b]);
EDIT:
You are using variable length array (VLA) in your program:
int r[i][y];
as i and y are not constants and are variables. VLA are a C99 standard feature.
you have to allocate the 2D array dynamically cause you don't know it size in compilation.
replace
int r[i][y];
with
int *r = malloc(i*y*sizeof(int));
and when finish, add:
free(r);
*and also the SCANF errors, people already answered here.
First the return value of scanf isn't the value that was read from stdin, instead it is the number of input values scanf read.
Secondly, C does not allow creation of arrays by using variables. You have to first create one array by dynamically allocating it. And for each entry in the first array you have to create yet another array.
Don't forget to free the memory you allocate!
The use of scanf(%d, &var) is incorrect.
scanf reads from console an integer (this type is specified by its first paramenter %d) and stores it in the second parameter.
This second parameter must be a pointer, so an & is needed when your variable is not a pointer yet.
Therefore you should correct your code in this way:
int i;
scanf("%d",&i);
int y;
scanf("%d", &y);
And in your for loop
scanf("%d", &r[a][b]);
It doesn't print the message because of line buffering.
If you add \n to your strings (to start a new line), it might do what you expect:
printf("Enter the number of columns\n");
Alternatively, if you really want the user to type on the same line, you need to flush the buffer manually:
printf("Enter the number of columns");
fflush (stdout);

Resources