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
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:
Are normal Arrays also dynamic? [duplicate]
(3 answers)
Closed 2 years ago.
int a[5];
for(int i=0;i<12;i++)
{
printf("enter element #%d: ",i);
scanf("%d",&a[i]);
}
I expected this to give me an error, but didn't give
It does not take the input when the value of i is 8 i.e., for the 9th element. It just jumps when i=8
When you write past the bounds of an array, you invoke undefined behavior. That means the compiler makes no guarantees regarding what the program will do. It may crash, it may output strange results, or it may appear to work properly.
Just because the program could crash doesn't mean it will.
in your for loop ,your final value of control is 11 and the size of your array is 5,so you fill in the array 11 elements and you array contains only 5 boxes in memory ,your answer is like this :
#include<stdio.h>
int main()
{
int a[5];
for(int i=0;i<5;i++)
{
printf("enter element #%d: ",i);
scanf("%d",&a[i]);
}
}
This question already has answers here:
What can happen if printf is called with a wrong format string?
(5 answers)
Closed 4 years ago.
C written in Emacs
// print1.c --displays some properties of printf()
#include <stdio.h>
int main(void)
{
int ten = 10;
int two = 2;
printf("Doing it right: ");
printf("%d minus %d is %d\n", ten, 2, ten - two);
printf("Doing it wrong: ");
printf("%d minus %d is %d\n", ten); // forgot 2 arguments
return 0;
}
Welcome to the Emacs shell
Output:
~ $ ./a.exe
Doing it right: 10 minus 2 is 8
Doing it wrong: 10 minus 2 is 8
It should do nothing of the sort.
The behaviour of your program is undefined.
One manifestation of undefined behaviour is the compiler figuring out what you really wanted to do. Don't ever rely on that though.
This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 5 years ago.
Here is my code:
#include<stdio.h>
int main()
{
int i, list[1];
list[0]=1;
list[1]=2;
list[2]=7;
list[55]=70;
i=sizeof list;
printf("%d %d %d %d %d Size of array is %d",list[0],list[1],list[2],list[3],list[55],i);
return(0);
}
It returns "1 2 7 4 70 Size of array is 4". Why can i assign, say 55 to list[55]. list[55] should not exist as I only gave the array list enough memory for 1 integer, right? In addition shouldn't this give me an error as list[3] doesn't exist? and if for some reason i am changing the size of the array why isn't the size 56? It comes out as 4.
So what is happening to give me the output i got?<--{main question}
[As i don't want to create a separate thread for a related question, why when i code int list[0]; the program crashes, if i am somehow changing the size from 1 to 4 shouldn't I be able to change the size from 0 to 4?]
Thanks for your help, I know this probably a stupid or obvious question.
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
#include <stdio.h>
int main()
{
int a=3, b = 6;
printf(&a["Hi!Hello! %s\n"], &b["Mnnit/Softathalon"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
return 0;
}
output:
Hello! Softathalon
That is C !
Why is this the output? Can anyone explain different format specifier in it?
For any array T arr[N], the expression arr[i] is equivalent to *(arr + i).
Because the addition is commutative in the latter expression, you can also write this as *(i + arr), and hence as i[arr].
In particular, arr[3] and 3[arr] denote the same thing.
It's one of those "curiously funny things you can do in C", but it should go without saying that serious code should never actually use such a construction.