Why is segmentation fault given? C [duplicate] - c

This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 1 year ago.
I've tried writing this code and the two printf statements appear to give segmentation fault.
why isn't the output 5? what went wrong? and how to edit this code in a way such that it prints 5?
#include <stdio.h>
int* fun(int x){
x=5;
return &x;
}
int main() {
int x = 3;
int* p = fun(x);
printf("%d",*(fun(x)));
printf("%d",*p);
return 0;
}

Related

Is zero size array invalid in C struct? [duplicate]

This question already has answers here:
Array of size 0 at the end of struct [duplicate]
(5 answers)
What is the purpose of a zero length array in a struct? [duplicate]
(2 answers)
Closed 3 years ago.
I have compiled followin gprogram using GCC compiler on Ubuntu platform.
I am wondering why the following program is working fine in C?
Is it undefined behaviour?
#include <stdio.h>
struct str
{
char arr[0];
};
int main()
{
struct str s;
s.arr[0]=1; // I think it is invalid in C
printf("%d\n", s.arr[0]);
return 0;
}
Output:
1
and when I print printf("%zu\n", sizeof(s));. it is print 0.

Declaration of character pointer as global and local variable when using pipe() varies [duplicate]

This question already has answers here:
Printf printing garbage after read() call. The offset is always printed as 0
(3 answers)
Why is my simple C program displaying garbage to stdout?
(5 answers)
Closed 4 years ago.
#include<stdio.h>
#include<unistd.h>
char *msg1="HELLLO",*msg2="NONONO";//global declaration prints without any garbage value
int main()
{
/*char *msg1="HELLLO",*msg2="NONONO";"global declaration prints with garbage value */
char buf[6];
int file[2],i;
if(pipe(file) < 0)
printf("\nyou are out");
write(file[1],msg1,6);
write(file[1],msg2,6);
for(i=1;i<=2;i++){
read(file[0],buf,6);
printf("\n%s",buf);}
return 0;
}
Output:
As Global Variable:
HELLLO
NONONO
As local variable:
HELLLO▒▒▒
NONONO▒▒▒

Why does this C code not throw any error? [duplicate]

This question already has answers here:
About Tentative definition
(2 answers)
Why won't extern link to a static variable?
(4 answers)
Closed 7 years ago.
I have a few doubts regarding the following statement:
C allows a global variable to be declared again when first declaration
doesn’t initialize the variable.
Consider the following snippet:
#include<stdio.h>
int x; //line 1
int x = 5; //line 2
int main(void)
{
printf("%d", x);
return 0;
}
OUTPUT: 5
Q1. In line 1, the first declaration of x does initialize the global variable x to 0 by default. Then why does the compiler not throw any error?
If I rewrite line 1 and line 2 as:
int x=10;
int x=20;
The above snippet results in an error like redefinition of 'x'. Shouldn't I get the same error for the following also because x is, by default, initialized to 0?
int x;
int x=5;
Q2. Both the line 1 and line 2 are defining the global variable x. Is this statement correct? I have read that we can define a variable only once but we can declare it as many times as we want.

Why the following code gives "hello5" as output in C? [duplicate]

This question already has answers here:
what is the return type of printf [closed]
(2 answers)
Closed 7 years ago.
Follwing code in C gives the output "hello5" ...how?
#include<stdio.h>
int main(){
int f = fun();
printf("%d",f);
return 0;
}
void fun(){
printf("hello");
}
What you see is undefined behavior.
The value f is never initialized and you are printing uninitialized variable which will lead to undefined behavior.
printf() returns number of characters successfully printed out so the count here is 5(hello) for printing hello.
You need to return this value if you want then you have defined behavior because you are initializing the variable f in main()
int func()
{
int j;
j = printf("hello");
return j;
}

Crashing when trying to modify constant variable through a pointer [duplicate]

This question already has answers here:
Changing value of const int using using pointer [duplicate]
(2 answers)
Closed 8 years ago.
I got a program crash when I am trying to modify constant variable through a pointer.
#include <stdio.h>
static const int x = 5;
void changeX(int *x)
{
(*x) = 20;
printf("%d", (*x));
}
int main(void)
{
printf("Jelele");
changeX((int *)&x);
return 0;
}
I know it isn't a good practice and there is no need to make such that ... I am just testing something ...
My question is:
Why program crashes ?!
static const int x = 5;
This is a constant variable stored in read-only location so when you try to write to this location then you see a crash.
Like
(*x) = 20; /* This is UB */
Check the below link:
Where are constant variables stored in C?
You could change where the pointer x points to, but as the integer x is constant, its value cannot obviously be changed by definition.

Resources