Unexpected Behaviour of C program [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Below is a C program which wants to multiply an integer by 5 using bitwise operation. But when i run this program it gives unexpected output. I know there is something i messing up which i could not see. Any help regarding this guys and girls ?
#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int FiveTimes(int a)
{
int t;
t = a<<2 + a;
return t;
}
int main()
{
int a = 1, b = 2,c = 3;
PrintInt(FiveTimes(a));
PrintInt(FiveTimes(b));
PrintInt(FiveTimes(c));
return 0;
}

This is a question of "operator precedence": "<<" has a lower priority than "+" - so your code actually calculates a << (2 + a), while it should be (a << 2) + a. The latter is the fix.

Related

Integer Overflows in C and Defenses Against Numeric Errors [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 4 years ago.
Improve this question
I am trying to research various integer overflow scenarios in C and I was wondering does the C language provide any defenses against numeric errors and are there any additional classes or libraries in the C language that can help with that? Also, can anyone give me an example of code that results in an integer overflow in C?
No, there are no defenses.
This overflows:
#include <limits.h>
#include <stdio.h>
const int a = INT_MAX - 2;
const int b = INT_MAX - 2;
printf("%d + %d = %d\n", a, b, a + b);
When I tested it it printed -6, but anything could happen I guess.

why this POW loop keeps the initial values? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm just starting to learn C. I wrote this beautiful little program that was meant to print "11111". But the pow function doesn't actualize the variable, that changes every time, that is being used in it.
I created the loop using if, goto and adding 1's. That's the only way I know to create a loop yet.
Can I overcome this problem using the same tools I've used?
Here's my code:
#include <stdio.h>
#include <math.h>
main (void)
{
int b = 0;
int a = 0;
int c = 0;
up:
b = pow (10,a);
c = c + b;
a = a + 1;
if (a = 6)
{
goto stop;
}
else
{
goto up;
}
stop:
printf("%d\n",c);
}
if (a = 6)
That sets a to 6.
You probably meant:
if (a == 6)

What does predicate mean 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 have the following code:
int (*predicate)(char) = 0;
Can anyone tell me what this code means? What is the meaning of the word predicate in C?
The sentence is a declaration and definition of a pointer to a function taking one argument (char) and returning int. The pointer is initialized to the null pointer value.
The word "predicate" is the programmer's choice for the variable name.
Reference: cdecl
One might use predicate like this:
/* UNTESTED */
int IsLower(char c) { return c >= 'a' && c <= 'z'; }
int main () {
int (*predicate)(char);
predicate = IsLower;
if ( (*predicate)('f') == 1 ) printf("'f' is lower case!\n");
}

C prog that generates/output 3 numbers (any combination from 1 - 20) when added is equal to 20 [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
C prog that generates/output 3 numbers (any combination from 1 - 20) when added is equal to 20. Hope you could help me with this one. Thanks in advance.
int main(){
int num1,num2,num3,sum;
do{
printf("%i+%i+%i=%i\n",num1,num2,num3,sum);
} while(sum=20); getch();
}
Try this one. You should be able to modify it according to your needs.
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=20;i++)
for(j=1;j<=20;j++)
for(k=1;k<=20;k++)
if(i+j+k==20)
printf("%d %d %d\n",i,j,k);
return 0;
}

I need to write a C program that adds two numbers each of 100+ digits? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
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
Improve this question
Need to write a C program that adds two numbers each of 100+ digits..
I don't want the method of using arrays to do this.
Please suggest me how to store this numbers(atleast of 512 bit sized) and do the arithmetic operations?
You could use an arbitrary precision arithmetic library, such as GMP for that.
A quick C example:
#include <gmp.h>
mpz_t a, b;
const char *huge_decimal_num1 = "46819294521564960351683095841209562359068";
const char *huge_decimal_num2 = "6904120584864540916814056801234572451249681";
mpz_init_set_str (a, huge_decimal_num1, 10);
mpz_init_set_str (b, huge_decimal_num2, 10);
mpz_add (a, a, b); // a = a + b
printf("%s + %s = %s\n",
huge_decimal_num1, huge_decimal_num2, mpz_get_str (NULL, 10, a));

Resources