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

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.

Related

Why is segmentation fault given? C [duplicate]

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;
}

why compiler give me error when i assign char variable to int variable in global scope? [duplicate]

This question already has answers here:
Error "initializer element is not constant" when trying to initialize variable with const
(8 answers)
Closed 4 years ago.
I have an error
initializer element is not constant
when I initialize variable in global scope
this is my wrong code
char x = 65 ;
int c = x ;
int main(void) {
printf("%d",c); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
but when i
initialize int variable inside main functions it works correctly
char x = 65 ;
int main(void) {
int c = x ;
printf("%d",c); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
Initializers for global variables must be a compile time constant. The value of another variable (even a const variable) is not a compile time constant.
A numeric constant (or an expression consisting only of numeric constants) is a compile time constant.

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;
}

What's the terminology for this C construct: 'int myInt = ( { int x=42; x; } );'? [duplicate]

This question already has an answer here:
Weird initialization in C
(1 answer)
Closed 8 years ago.
I've just been shown a very neat C trick:
int myInt = ( { int x=42; x; } ); // sets myInt to 42
This is very useful for writing macros. But what exactly is going on here? Could someone pick this line apart and isolate/identify the mechanisms?
It's the same as int myInt = 42;. Just initializing with an expression whose value comes from x which has been initialized by 42.

In C, what does it mean when I declare a variable following a function signature, before the function body? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why are declarations put between func() and {}?
In C, what does it mean when I declare a variable following a function signature, before the function body?
Example:
int foo (i) int i {
printf ("the value of variable 'i' is: %d", i);
return i;
}
When I compile the the code in addition to initializing variable i, I get a compile error:
"cannot initialize parameter: p"
It means you are looking at old code.
That is the old K&R syntax.
Basically it says, i is the argument, and it is an int
You can rewrite it as
int foo (int i)
{
printf ("the value of variable 'i' is: %d", i);
return i;
}

Resources