A program that converts 16 bit positive number in binary - c

i am trying to write a program such that if a % 2^j = 0 then it prints 1 , else it prints 0 so at the end i get a 16 bit binary code. But i get no error and after i enter an input number (a) terminal crashes.
Thank you for your help.
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
uint16_t a, j, b, mod;
printf(" Please insert a number between 0 and 65535 \n ");
scanf("%d", &a);
j = 16;
while (j > 0)
;
{
b = pow(j, 2);
mod = a % b;
if (mod == 0) {
printf("%d", 1);
} else {
printf("%d", 0);
}
j = j - 1;
}
return 0;
}

You have undefined behavior as you use the %d format for scanf. This format specifier expects the argument to be a pointer to int.
Mismatching format specifier and argument type leads to UB.
For uint16_t use the macro SCNu16 (as documented in e.g. this reference):
scanf("%" SCNu16, &a);

Related

How do I receive elements into an array using scanf?

I'm learning C programming and I've encountered a problem using scanf for initializing values into the array. In this example, 10, 32 and 20 were input as values for the array; 20 should be in grades[2] but its value is 0.
Why doesn't the program register the last value that is input?
That is the relevant code.
I'll appreciate any help in understanding what went wrong with the program.
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
#define N 50
#define MaximalSTD 10
int main() {
printf("Please enter the grades of the examinees");
printf(" followed by the expected mean\n");
double grades[N], ReqMean;
int numgrade = 0;
for (int i = 0; i < N; i++) {
if (scanf("%lf", &grades[i]) == 1) {
numgrade++;
} else
break;
}
ReqMean = grades[numgrade - 1];
printf("numgrade: %d\nReqMean: %d\n", numgrade, ReqMean);
return 0;
}
printf("numgrade: %d\nReqMean: %d\n" , numgrade,ReqMean);
You are using the wrong format specifier for a double value (ReqMean in this case).
Try Instead.
printf("numgrade: %d\nReqMean: %lf\n" , numgrade,ReqMean);

Why am I getting a character in output when my input is 5 or more than 5? It is pure mathematical equation. If anything is wrong please tell me

I have compiled this code and it works just fine up to value 4 then it starts returning character instead of integer. I am talking about first equation => x= num*2; Here when I enter num value as 5 the output returns a.
#include <stdio.h>
int main(void)
{
int num;
int x; This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num); //
//printf("%d\n", num);
x = num * 2 ;
printf("%x\n", x);
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
}
please tell me if there is a mistake I am a newbie to coding.
As I see you are learning C language, and after reading your explanation, I feel that you want to print the integer value of variable x.
Kindly replace %x with %d in the print statement of variable x,
and you will be successfully able to print the value.
#include <stdio.h>
int main(void)
{
int num;
int x; // This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num);
x = num * 2 ;
printf("%d\n", x); // %d for integer and %x for hexadecimal values
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
return 0;
}
Finally, do read more about format specifiers in scanf and
printf statements.

Why did I get different answers in vscode?

My code:
#include <stdio.h>
#include <math.h>
int main (void)
{
int n = 0;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (int power = 1; power <= n; power++)
{
printf("%d %s ", (int)pow(10, power) - 1, power == n ? "=" : "+");
sum += (int)pow(10, power) - 1;
}
printf ("%d", sum);
return 0;
}
Output in Vs Code with gcc:
Enter a number: 5
9 + 98 + 999 + 9998 + 99999 = 111103
Output in online compilers:
Enter a number: 5
9 + 99 + 999 + 9999 + 99999 = 111105
My question: Why? is this happening?
Possibly an issue with pow and its implementation, perhaps due to different platform or compilers.
Instead of using pow which relies on floating point arithmetic and leads to compounding rounding errors (and possibly contains bugs), why not use simple multiplication? If you start the loop with 10, then you could multiply that by 10 each iteration to get the result you want.
Perhaps something like this:
unsigned sum = 0;
for (unsigned power = 0, value = 10; power < n; ++power, value *= 10)
{
sum += value - 1;
}
[Printing left out]
I just use the function "round" which returns the integer rounding closest to the value specified in parameter
#include <stdio.h>
#include <math.h>
int main (void)
{
int n = 0;
int sum=0;
printf("Enter a number: ");
scanf("%d", &n);
for (int power = 1; power <= n; power++)
{
sum += (int)round (pow(10, power)) - 1;
}
printf ("%d", sum);
return 0;
}

How to convert decimal to binary in 64 bits?

so I have this code
int main()
{
int n, c, k;
printf("Enter an integer\n");
scanf("%d", &n);
printf("%d in binary is:\n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
It converts decimal into binary but only in 32 bits. When I change it into 64 bits it doesn't work (it seems like it just doubles the result from 32 bits). At the same time it works fine with 8 or 4 bits etc.
What am I doing wrong?
It converts decimal into binary but only in 32 bits. When I change it into 64 bits it doesn't work (it seems like it just doubles the result from 32 bits).
The problem is here.
int n, c, k;
printf("Enter an integer\n");
scanf("%d", &n);
n is an int which can be as small as 16 bits. It could be 64 bits, but it's probably 32 bits. When you try to enter a 64 bit number you'll get garbage.
#include <stdio.h>
int main() {
int n;
printf("sizeof(int) == %zu\n", sizeof(int));
printf("Enter an integer\n");
scanf("%d", &n);
printf("n = %d\n", n);
}
$ ./test
sizeof(int) == 4
Enter an integer
12345678901
n = -539222987
Instead, you can use a long long int which has a minimum size of 64 bits or int64_t from stdint.h which is exactly 64 bits. I have a preference for using the explicit width types in code that requires a specific width to make it more obvious to the reader.
long long int uses %lld for scanf and printf while int64_t uses macros from inttypes.h. The code below takes advantage of C automatically concatenating constant strings; "foo" "bar" and "foobar" are equivalent.
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main() {
int64_t n;
printf("Enter an integer\n");
scanf("%"SCNd64, &n);
printf("n = %"PRId64"\n", n);
}
$ ./test
Enter an integer
12345678901
n = 12345678901
The problem is your n & k variables' data type. They are integer. Check your platform's data type size using sizeof(int).
When you change to 64-bits, these variables can't hold 64-bit values.
HTH!
the following code is one way to handle the problem:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
long long unsigned int n;
long long int c;
long long unsigned int k;
printf("Enter an integer\n");
if( 1 != scanf("%llu", &n) )
{
perror( "scanf for 64 bit number failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
printf("%llu in binary is:\n", n);
for (c = 63; c >= 0; c--)
{
k = n >> c;
if (k & 1)
putc('1', stdout);
else
putc('0', stdout);
}
printf("\n");
return 0;
} // end function: main
and here is the output from a typical run of the program:
Enter an integer
6789097860397846
6789097860397846 in binary is:
0000000000011000000111101010011000000110010100000111101100010110

How does %n work with variables [duplicate]

This question already has answers here:
What is the use of the %n format specifier in C?
(12 answers)
Closed 7 years ago.
So I have looked around and I have found little information on %n in general and no info on how to use it with a variable.
As far as I can tell the code I am using should work but I do not know what it is not. The particular line that I am having trouble with is:
printf("%d %n", num[x], &c);
Below is the entire code.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
//seed rand, declare arrays, declare variables
srand(time(NULL));
int num[10];
int c = 0;
int total = 0;
int x;
printf( "%s%14s%20s\n", "Value", "Characters", "Total Characters" );
//Loads the num array with random numbers.
for(x = 1; x < 10; x++)
{
num[x] = 1 + rand() % 1000;
}
for (x = 1; x < 10; x++)
{
printf("%d %n", num[x], &c);
printf("%14d", c);
total = total + c;
printf("%20d\n", total);
}
}
From the C Standard
n The argument shall be a pointer to signed integer into which is
written the number of characters written to the output stream so far
by this call to fprintf. No argument is converted, but one is
consumed. If the conversion specification includes any flags, a field
width, or a precision, the behavior is undefined.
The same is valid for printf
Here is a demonstrative program
#include <stdio.h>
int main(void)
{
int n1, n2;
printf( "%s%n%s%n\n", "Hello", &n1, " World", &n2 );
printf( "%d\t%d\n", n1, n2 );
return 0;
}
The program output is
Hello World
5 11

Resources