two different output in different console with same code in c? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
"getting different output in different console with the same code. I have used gcc compiler in vs code and dev c++ uses system compiler."
```C programming
int main()
{
int a=0;
int arr[a];
int i;
printf("enter the size of array:");
scanf("%d",&a);
for(i=0;i<a;i++)
{
printf("enter a %d no",i);
scanf("%d",&arr[i]);
}
for(i=0;i<a;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
```

The defect is that you declare int arr[a] (latter is a VLA) when a has been initialized to 0. This means no memory is allocated to arr, and when you write to it (via scanf) you undefined behavior.
You can use int arr[a]; if you declare it after you read the value of a. The problem is that there is no way to check for stack overflow (i.e. try set a to >8192 or whatever your stack size is set to and see what happens).
Try something along these lines:
unsigned a;
printf("enter size of array: ");
if(scanf("%u", &a) <= 0 || a == 0) {
// error handling
}
int *arr = malloc(a * sizeof(int));
if(!arr) {
// error handling
}
Use unsigned type for the variable a that holds a size and check that it's > 0 (I believe malloc(0) is not well defined on some platforms; on Linux it would cause malloc to return NULL hence trigger an error handling in the above code). Check all return codes for errors.

Related

What is wrong with this code?? It is giving output as 0.0000? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#include<stdio.h>
double sum_1(int n)
{
int i=1;
double s;
while(n>0)
{
s=s+i/(2*i+2);
i=i+2;
n--;
}
return s;
}
int main()
{
int n=5;
double s1;
printf("Enter n:\n");
scanf("%d",&n);
s1= sum_1(n);
printf("sum = %lf",s1);
return 0;
}
The problem is
s=s+i/(2*i+2);
in the first iteration, s is used uninitialized. Since this is a type which can have trap representation, and it's address is never taken, trying to use the uninitialized value here invokes undefined behavior.
That said, the grouping of the statement
s=s+i/(2*i+2);
is same as
s = s + ( i / (2*i+2) );
^^^^^^^^^^^^^---- integer division
so, it involves integer division, which is most likely what you don't want. You need to enforce floating point arithmetic, like
s=s+i/(float)(2*i+2);
Finally, for printing a double, %f is sufficient, %lf is not needed and has no effect.

Bug in simple C source code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to know if the format of user defined function i wrote i.e return(xxx) is correct or not .
Because when i compile my code, i have to enter the input 2 times.It might be a silly mistake because i just began learning C language
****MY CODE:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****ANswer****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
remove the space after '%ld' it will take one input.
according your code,
scanf("%ld '&input) ;
here compiler at first wants one input for '%ld' then it waits for blank space you used after '%ld'. remove it then it will go next step after one input.
you should use,
scanf("%ld%",&input);

Please check my code. won't return a right value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
#include <stdio.h>
int f_totprice(int,int,int,int);
int main()
{
int menu_1;
int menu_2;
int menu_3;
int menu_4;
int totprice;
int received_money;
while (1){
printf("what do you want to order?\n");
printf("pizza: ");
scanf("%d",&menu_1);
printf("cheese: ");
scanf("%d",&menu_2);
printf("curry: ");
scanf("%d",&menu_3);
printf("soup: ");
scanf("%d",&menu_4);
printf("======================================\n");
totprice=f_totprice(menu_1,menu_2,menu_3,menu_4);
printf("total price is %d\n",totprice); /* this part returns a wrong value */
printf("received ");
scanf("%d",&received_money);
printf("Your change is %d",received_money-totprice);
}
}
int f_totprice (int a, int b, int c, int d)
{
int total;
char price[]={500,1000,1500,2000};
total=a*price[0]+b*price[1]+c*price[2]+d*price[3];
return total;
}
so, i just wrote a code to make a simple menu and the price part doesn't return the total value of my function below.. it keeps saying "the total value is -600,-300,-190" etc instead of integers. in my eyes there is nothing wrong with it. please help? :(
char price[]={500,1000,1500,2000};
Can you figure out why this cannot work?
You should find out how to turn on warnings on your compiler.
You are using char array instead of interger array, and printing the output in integer type (%d), Hence it is printing the ASCII equivalent of the sum , Please change character array to integer array.
int price[]={500,1000,1500,2000};
Your problem lies here
int f_totprice (int a, int b, int c, int d)
{
int total;
char price[]={500,1000,1500,2000};
total=a*price[0]+b*price[1]+c*price[2]+d*price[3];
return total;
}
Why are you using a char array,
Use int array

I am getting garbage values for this prob in c [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to execute the code below, but along with my answer I am getting some garbage values. Please help me find where I made an error.
int main()
{
int n,i,j,k=0;
int a[100];
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[++k]=i;
}
}
for(j=0;a[j]!='\0';j++)
{
printf("\t%d",a[j]);
}
}
int a[100];
C does not initialize the array elements by default. So all the elements that are not assigned in your first loop will have garbage.
What you can do is:
int a[100] = {0};
This will initialize all elements to 0
In C array indexing starts from 0. Preincrement ++k will cause to start array indexing from 1. Change it to k++. Also change
for(j=0;a[j]!='\0';j++)
to
for(j=0;j < k;j++)
to print the only values you have entered.
your code should be
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[k++]=i;
}
}
for(j=0; j < k;j++)
{
printf("\t%d",a[j]);
}
Here how you can fix this. Next time please describe what you were trying to achive

What is the correct way to use pointer of pointer in a function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use a function that displays a block of memory's content pointed by a pointer.
But didn't get the desired output, i am new to this, please correct my if i am wrong.
when I input size =3, element = 1,2,3 , I got output = 1 only.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
void merge(int **arr1);
int main(void) {
int size1;
printf("Give me the size of first array\n");
scanf("%d", &size1);
int *arr1 = malloc(size1*sizeof(int));
int *p1=arr1;
printf("Give me the elements of first array\n");
int index1;
for(index1 = 0 ; index1<size1; index1++)
scanf("%d", p1++);
merge(&arr1);
return;
}
void merge(int **arr1) {
while(**arr1) //**arr1 is the content of the passed array, if there
// is an int in it, print that out and increment to next one
{
printf("%d", **arr1); // ** is the content and * is the address i think, right?
*arr1++;
}
}
Your merge() code expects the array to be terminated by zero. The calling code is not doing it, so the behavior is unspecified (I got segfault when I tried your code).
The other issue is that you should put parentheses around *arr1:
(*arr1)++;
When I run your code with this modification and enter zero for the last element, your code runs fine.

Resources