This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 6 years ago.
I'm really confused about the result of the following code:
#include <stdio.h>
#include <stdlib.h>
int one(int a, int b) {
int k, t;
k = a - b;
t = a + b + 1;
if (k % 2 == 0) return t;
else return 0;
}
int two(int x, int y) {
int m;
printf("%d\n", m);
return m + x + y;
}
main() {
int result = two(5, one(4, 3));
// printf("%d\n", one(4, 3));
printf("result is %d\n", result);
}
one(4, 3) returns 0, which is not surprising. But I don't understand why two(5, 0) returns 8. In other words, m takes on the value 3 without being initialized. How did this happen?
C does not automatically initialize values to 0 when you define them. Technically, reading that data before you initialize it is undefined behavior. In practice, this normally results in a garbage value containing whatever data was stored in that location previously.
Related
This question already has an answer here:
The Definitive C Book Guide and List
(1 answer)
Closed 3 months ago.
int f( int i ) {
printf("called f\n");
if (i < 0) return -i;
else
return 3 * i;
}
I just want to know what int "f" means.
Basically, int f(int i) means :
The first "int" is the type of the result returned by the function, aka -i or 3 * i with a type of "integer".
"f" is the name of the function. It could be whatever you want, for example "function".
It is just the name of the function that was declared.
In this case it is a function that returns an integer.
We could declare the same one with a different name:
int name() {
return (1 + 2);
}
The function that you provided is most likely used in the main() function, like this:
int main(){
int x = 2;
int output = f( x );
printf("%d", output);
return 0;
}
This question already has answers here:
How to write absolute value in c
(5 answers)
Closed 2 years ago.
Let a = 4 and b = 5
on adding a+b = 9
on subtracting a-b = -1
But I want the absolute value of the answer, i.e., |a-b|=4-5=1. All I am getting is a negative 1.
How can I do that?
#include <stdio.h>
void add(int*a,int*b){
int c = *a+*b;
printf("%d\n",c);
}
void subtract(int*a,int*b){
int c = *a-*b;
printf("%d\n",c);
}
int main() {
int a, b; //enter the value of a and b
scanf("%d %d", &a, &b);
add(&a,&b);
subtract(&a,&b);
return 0;
}
simply put a condition using ternary operator.
replace assigning to c as following.
int c = *a > *b ? *a - *b : *b - *a;
This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 4 years ago.
if the variable is initialized (i = 0), it's still 1 each time the function func is called, BUT
when i is not initialized:
#include <stdio.h>
int funct(void);
int main(void)
{
funct();
funct();
funct();
return 0;
}
int funct(void)
{
int i;
static int j = 0;
i++;
j++;
printf(" i = %d j = %d\n", i, j);
}
the output is
i = 1 j = 1
i = 2 j = 2
i = 3 j = 3
I don't understand why the variable i behaves like a static one!
The value is unspecified, so anything goes. But, likely the same memory is reused for each call to funct and with that, the same memory is reused and the i just pick up the old value left from the previous run.
This question already has answers here:
What is C local function declaration mechanism?
(3 answers)
Closed 5 years ago.
#include <stdio.h>
void main()
{
static int array[5] = { 200, 400, 600, 800, 1000 };
int sum;
int addnum(int *ptr); // what happened here ?
sum = addnum(array);
printf("Sum of all array elements = %5d\n", sum);
}
int addnum(int *ptr)
{
int index, total = 0;
for (index = 0; index < 5; index++)
{
total += *(ptr + index);
}
return(total);
}
int addnum(int *ptr); //Exactly what does this code mean ?
Just the topic or name of the concept would be fine . Thanks a lot in advance .
int addnum(int *ptr); // what happened here ?
It is so called forward declaration allowing compiler to know that such function will be defined later.
In C it is possible (although unwise) to implement a pair of mutually recursive functions thus:
int first(int x) {
if (x == 0)
return 1;
else
return second(x-1); // forward reference to second
}
int second(int x) {
if (x == 0)
return 0;
else
return first(x-1); // backward reference to first
}
This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 8 years ago.
How is the most basic function pointer created in C?
Here's a pretty straightforward example:
#include <stdio.h>
#include <string.h>
int add(int x, int y) {
return x + y;
}
int sub(int x, int y) {
return x - y;
}
int main() {
char const * operator = "addition";
int (*operation)(int, int) = NULL;
if (strcmp(operator, "subtract") == 0)
operation = sub;
if (strcmp(operator, "addition") == 0)
operation = add;
if (operation) {
int x = 3;
int y = 7;
int z = operation(x, y);
printf("%s %d %d == %d\n", operator, x, y, z);
}
}
Where is this useful?
A common use case that I've seen is to create interfaces. So, for example, we have a type of object, say a struct that represents a network interface. Now, because there are different hardware backend, we might want to implement the functions differently depending on the hardware implementation.
So we might have a function pointer in the interface struct for initializing the hardware, sending packets, and receiving packets.
This code will demonstrate how to create and modify a basic function pointer
#include <stdio.h>
int my_function(int);
int (*function)(int)=&my_function;
int my_function2(int x){
function=&my_function; // & is optional
return (0);
}
int my_function(int x){
function=my_function2;
return (x);
}
/* And an example call */
int main(){
printf ("%d \n",function(10));
printf ("%d \n",function(10));
printf ("%d \n",function(8));
return 0;
}