Static Variables not retaining their values inside function [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Inside functions static variables retain their value when called again but why it is not happening in this case ?
#include <stdio.h>
void print(void) {
static int x;
x = 10;
x += 5;
printf("%d ", x);
}
int main() {
print();
print();
return 0;
}
It is giving output as 15 15.

You assign a new value each time:
static int x;
x = 10; // <- assignment
What you probably want is initialization:
static int x = 10; // <- initialization
Initialization happens only once during variable lifetime, and static means the variable lives for the whole execution time of your program.

Initializing static variable will solve the issue. Below corrected example resolves the issue and gives me 15 and 20
#include <stdio.h>
void print(void) {
static int x = 10; //Initialize here
// x = 10; //remove this statement
x += 5;
printf("%d ", x);
}
int main() {
print();
print();
return 0;
}

Related

My program returns 4 instead of expected value 40 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
#include <stdio.h>
int value(int *a);
int main(){
int num = 4;
value(&num);
printf("value of number is = %d", num);
return 0;
}
int value(int *a){
int c = (*a)*10;
return c;
}
In this code, I transfer the address in function but it does not change, Why?
You have 2 ways of having a function change data outside it: you can pass a variable by reference and update it, or you can return a value and use that. You are mixing half of each method instead. I reordered the code to make my commentary clearer.
#include <stdio.h>
int value(int *a);
// here you are passing by reference, good
int value(int *a){
int c = (*a)*10; // but you don't change a
return c; // instead you return a new value
}
int main(){
int num = 4;
value(&num); // but here you ignore the new value.
printf("value of number is = %d", num);
return 0;
}
to make reference way work, you need:
#include <stdio.h>
void value(int *a);
int main(){
int num = 4;
value(&num);
printf("value of number is = %d", num);
return 0;
}
void value(int *a){
*a = (*a)*10; // change the value that a points at
// no need to return anything
}
or to make the return way work:
#include <stdio.h>
// no need to pass by reference
int value(int a);
int main(){
int num = 4;
num = value(num); // use value return by function
printf("value of number is = %d", num);
return 0;
}
int value(int a){
int c = a * 10;
return c;
}

Multiple errors in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am not able to understand where I am going wrong. Please help! I am new to the website. Appreciate all the help. Thanks a lot :D
#include <stdio.h>
int main()
{
printf("Hello World")
}
int factorial(int x) {
int i;
for(i=1; i < x; i++)
x *= i;
return x;
}
int a = 9;
int b;
b = factorial(int a);
printf("%i", b);
I have corrected the code and added some comments. I also rearranged the factorial slightly, so that it works for 0! which is 1.
#include <stdio.h>
int factorial(int x) { // added the argument type int
int product = 1; // use another variable
for(int i = 2; i <= x; i++) {
product *= i;
}
return product;
}
int main()
{
int a = 9;
int b;
b = factorial(a);
printf("%i", b);
}
Note that you can only generate up to 12! and after that you get overflow due to the range of a 32-bit int.
First here printf("Hello World") you are missing ;
Second add this part to your main.
int main()
{
printf("Hello World");
int a = 9;
int b;
b = factorial(a);
printf("%i", b);
}
and when you are calling your function in main,you shouldn't send int a to function b = factorial(int a) ,because by saying int a instead of a you are redefining it.(so it will be uninitialized,if redefinition is not error)
also as said in comments you should add a prototype for factorial before main or move it before main.
Finally your loop in factorial is infinitive ,for(i=1; i < x; i++) since you're doing x *= i;
this condition i < x is never true.
you will increase x until int type has not enough space for it. so a garbage value will be assigned to it ,and you will exit the loop.

How to determine the number of appearances of a letter in a string using C language? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int appar(char c[], char x);
int main() {
char c[] = "hello everyone!";
int b = appar(c, 'h');
printf("nbr of h is %d ", b);
return 0;
}
int appar(char c[], char x) {
int i = 0, cmpt = 0;
int q = strlen(c);
for (i; i < q; i++) {
if (c[i] == 'x')
cmpt++;
}
return cmpt;
}
I run and compile the program, but I receive "nbr of h is 0".
What's the wrong in this code?
Change c[i]=='x' to c[i]==x
You want to compare with the variable x, not the character constant 'x'

C Arrays gives address instead of value when second array is created [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
First of all, sorry for my English. I hope you will still understand what my problem is.
I am new to C programming and I am a bit confused. Here is my code.
#include <stdio.h>
#include "tableaux.h"
int main(int argc, const char * argv[]) {
int tableauUn[4] = {1, 1, 1, 1};
printf("%d\n", sommeTableau(tableauUn, 4));
printf("%d\n", moyenneTableau(tableauUn, 4));
return 0;
}
And this is the file where my functions are. I also have a file where I have my prototypes.
#include <stdio.h>
#include "tableaux.h"
int sommeTableau(int tableau[], int taille) {
int resultat;
for (int i = 0; i < taille; i++) {
resultat += tableau[i];
}
return resultat;
}
int moyenneTableau(int tableau[], int taille) {
int resultat;
for (int i = 0; i < taille; i++) {
resultat += tableau[i];
}
return resultat / taille;
}
void copierTableau(int tableau[], int taille, int tableauDeux[]) {
for (int i = 0; i < taille; i++) {
tableauDeux[i] = tableau[i];
}
}
So everything works fine. The first printf gives me the total of the values that are stored in the array and the second one gives me the average of the values.
5
1
Program ended with exit code: 0
What I don't understand is why do I get this result when I want to create a second array ?
int tableauUn[4] = {1, 1, 1, 1};
int tableauDeux[4] = {0};
the result
1606416356
1
Program ended with exit code: 0
So I haven't used the second array but the result of the first printf changes and I am a bit confused with what is going on.
I hope you can help me !
Please initialize the sum you return in function
int resultat = 0;
otherwise resultat will take garbage value as initial value

Swap two elements of an array function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Can you help me by telling me what is wrong with this? Why is the swap function not working?
void swap(int a[], int b, int c) {
int temp = a[b];
a[b] = a[c];
a[b] = temp;
}
void bubble1 (int a[], int N){
int i;
for(i=0;i<N-1;i++){
if(a[i]>a[i+1]){
swap(a,i,i+1);
}
}
}
void main() {
int N = 11;
int a[12]={5,3,12,4,25,10,14,35,2,8,13};
bubble1 (a,N);
int i;
for(i = 0; i < N; i++){
printf("%d\n",a[i]);
}
}
If I don't use the swap function and do the swapping manually in the "bubble" function it works. However if I use the swap it doesn't work, even though it's exactly the same. What am I doing wrong here?
int temp = a[b];
a[b] = a[c];
a[b] = temp;
Simple typo, you are assigning to a[b] twice. The second one should be a[c]

Resources