This question already has answers here:
C scanf() and fgets() problem
(4 answers)
Closed 3 years ago.
This is a problem of URI online judge.Problem no.1914 beginner.According to my code if i give input 4 then the the program should read strings 4 times and also integer 4 times each time two integer.but the program are only taking 4 times input either 4 times string or 4 times integer or 2 times string+ 2 times integer.
#include <stdio.h>
int main()
{
char name[1000][100],ch;
int a,b,c,i,j=0,k,n[1000][2];
scanf("%d",&a);
for(i=0;i<a;i++)
{
gets(name[i]);
for(k=0;k<2;k++)
{
scanf("%d",&n[i][k]);
}
}
}
if inputs are
4
Quico PAR Chiquinha IMPAR
9 7
Dami PAR Marcus IMPAR
12 3
Dayran PAR Conrado IMPAR
3 1000000000
Popis PAR Chaves IMPAR
2 7
After taking 4 lines of input the program end.If u can help help with that please help.
quico PAR chiquinha IMPER
9 7
dami PAR marcus IMPER
12 3
Read this- https://www.geeksforgeeks.org/problem-with-scanf-when-there-is-fgetsgetsscanf-after-it/
Your scanf is leaving a newline on the buffer.
Related
This question already has answers here:
Is accessing a global array outside its bound undefined behavior?
(8 answers)
Closed 8 months ago.
I am a Computer science student and I feel like I am missing something very simple. Could you please help me out ?
#include <stdio.h>
void do_stuff(int *c) {
static int a = 0;
int b = 0;
a+=3;
printf("%d %d\n", *(c+a), c[b]);
printf("%d %d\n", *(c+6), c[b]);
printf("%d %d\n", c[6], c[b]);
}
int main (void){
static int array[6] = {5,17,23,42,127,3};
do_stuff(array);
do_stuff(array);
do_stuff(array);
return 0;
}
This is the outcome of this code:
42 5
3 5
3 5
6 5
6 5
6 5
0 5
9 5
9 5
I don't get, why it is 6 5 for the second do_stuff(array).
I thought it would be 0 5 for every print of second and third do_stuff(array). Then I thought maybe It was something to do with static a and I tried it without a variable, so just with the number 6. But the answer was the same.
Could you please explain the reason for the outputs with the bold font?
Thank you for your help.
In an array with 6 elements, using index 6 will read the first position after the array, which is not 0. The read value depends on the underlying architecture and compiler implementation; depending if such memory position is mapped to your process or not, the OS may kill your application.
In your case, it looks like in memory you have the value of variable a stored just after the input array of do_stuff(), that's why printing c[6] basically prints the value of a.
Of course this is best described as undefined behavior and the source code is basically incorrect.
This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Using "\n" in scanf() in C [duplicate]
(2 answers)
Closed 2 years ago.
firstly i am a noob in c, and i have seen this issue various times in c, and it happens most of the time
here is the code
#include <stdio.h>
int main ()
{
int n,a=5, A[n], i;
printf("value of n is\n");
scanf("%d\n", &n);
for(i=0; i<n; i++)
{
printf("value of A[%d] is %d\n", i, a++);
}
return 0;
}
so when it comes to output, something strange happens, the program asks for two input values
here see this
/tmp/bi8gJ4fc18.o
value of n is
10
7
value of A[0] is 5
value of A[1] is 6
value of A[2] is 7
value of A[3] is 8
value of A[4] is 9
value of A[5] is 10
value of A[6] is 11
value of A[7] is 12
value of A[8] is 13
value of A[9] is 14
so why does scanf require two inputs or when using scanf two inputs are to be provided, and it only considers the 1st input, what is happening here
also one more question, here in the stack-over-flow why I have to press enter two times in the body so that the next line actually goes to the next line in the output here
also, one more plz comment on this and have mercy on me, what is the proper way to search a question in sttack-over-flow, I mean i am never able to find the answer to my questions but when i ask a question others easily find some years old answer to it
This question already has answers here:
read int with scanf until new line
(3 answers)
How to get integer input in an array using scanf in C?
(4 answers)
Closed 2 years ago.
I'm new to C language and I'm trying to make a program that includes an array of type int with the max length of 10.
I want to enter a random number of 1-digit numbers and set all the other values to 0.
For example, the console will look like this:
Enter B Values : 1 2 5 7 8
and the B array will look like this 1 2 5 7 8 0 0 0 0 0
I'm trying to add the numbers at the same line without using Enter.
Could anyone help?
Could the answer be EOF related?
This question already has answers here:
C/C++ unsigned integer overflow
(4 answers)
Closed 4 years ago.
I was trying to get into c programming, but in a question a get stuck, please explain this.
int main()
{
char c = 255;
c=c+10;
printf("%d",c);
return 0;
}
the output it gave is
> 9
kindly explain this to me.
The maximum value of a char is 255.
By adding 10 to that number you get 265.
Because that value is not a suitable value for a char it will do 265 % 256 resulting 9
That's why your result is 9
This question already has answers here:
Why don't I get a segmentation fault when I write beyond the end of an array?
(4 answers)
Closed 4 years ago.
guyz, today I got a serious confusion (well, for me at least). I was declared an array, with following property:
int arr[0][1] = {1,2,3,4,5,6 ... };
while doing this, i was getting this warning:
warning: excess elements in array initializer.
while printing this code, i was getting, some garbage value in every index.
after that, i was trying with following snippets,
int arr[][1] = {1,2,3,4,5,6,7,8,9};
and strangely, i was getting no warning and no error. and when i was executing with following code:
#include <stdio.h>
int main() {
int t[][1] = {1,2,3,4,5,6,7,8,9};
for(int i=0; i<9; i++){
for(int j=0; j<9; j++)
printf("%i ",t[i][j]);
printf("\n");
}
return 0;
}
i was getting this result:
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 0
3 4 5 6 7 8 9 0 8
4 5 6 7 8 9 0 7 3
5 6 7 8 9 0 6 4 13247168
6 7 8 9 0 5 5 13247168 0
7 8 9 0 4 6 13247168 0 4199400
8 9 0 3 7 13247168 0 4199400 0
9 0 2 8 13247168 0 4199400 0 0
Now this is become a serious problem and confusion for me to understand. Help me if you know the reason behind. Thank you.
when imposing the first dimension to the compiler, since the compiler sees that you're trying to initialize with more data than the dimension can contain, you get a warning.
Now with int t[][1] = {1,2,3,4,5,6,7,8,9};, you're letting the compiler compute the size automatically. But that doesn't mean there will be a run-time check for the bounds.
Once compiled without warnings, there is no run-time to check that a dynamic access out of bounds is catched. What you're experiencing at run-time is just undefined behaviour of reading/writing out of bounds.