How to print binary content of bytes pointed by a pointer? [duplicate] - c

This question already has answers here:
Is there a printf converter to print in binary format?
(57 answers)
Closed 8 years ago.
I have a pointer returned from malloc. Which points to first byte of allocated size. For EX:
void* p=malloc(34);
How to print the binary values contained in those 34 bytes. ?

You probably want this :
int i ;
void* p=malloc(34);
for (i = 0; i < 34; i++)
{
unsigned char c = ((char*)p)[i] ;
printf ("%02x ", c) ;
}
It doesn't print in binary (010011011) but in hexadecimal, but that's probably what you want.
But as stated in the comments you will get garbage values.

Related

How can I get unsigned char array? [duplicate]

This question already has answers here:
Finding length of array inside a function [duplicate]
(7 answers)
How do I determine the size of my array in C?
(24 answers)
Closed 1 year ago.
I writing a function that will print out hexadecimal inside unsigned char array. But I can't get the size of the array. I tried using sizeof() but it seems to print out the size of the type which is 4.
if(raw_instr[0]==0x68){
printf("%d\n",sizeof(raw_instr));
for(int i=0; i<sizeof(raw_instr);i++){
printf("%x ",raw_instr[i]);//expect to print 68 10 3f 0 0 but it print only 68 10 3f 0
}
printf("\n");
}
This is the array that I will take in for loop.
unsigned char inst1[5] = {0x68,0x10,0x3f,0x00,0x00};

How can I get a string from a 2d character array in C? [duplicate]

This question already has answers here:
What's the rationale for null terminated strings?
(20 answers)
Closed 3 years ago.
The 2d array:
char c[4][3]={{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'}};
As I wanted to get 'def' after running the program, I tried this code:
printf("%s\n",c[1]);
However, the result was 'defghijkl\262'. What's wrong with my code?
You can print def in two ways:
char c[4][4]={{'a','b','c','\0'},{'d','e','f','\0'},{'g','h','i','\0'},{'j','k','l','\0'}};
printf("%s\n",c[1]);
So, basically printf needs null termination to know where to stop printing
or you can print using a loop without null termination like:
char c[4][3]={{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'}};
for(int i = 0; i < 3; i++)
{
printf("%c", c[1][i]);
}

printing the char variable value using %d [duplicate]

This question already has answers here:
C/C++ unsigned integer overflow
(4 answers)
Closed 4 years ago.
I was trying to get into c programming, but in a question a get stuck, please explain this.
int main()
{
char c = 255;
c=c+10;
printf("%d",c);
return 0;
}
the output it gave is
> 9
kindly explain this to me.
The maximum value of a char is 255.
By adding 10 to that number you get 265.
Because that value is not a suitable value for a char it will do 265 % 256 resulting 9
That's why your result is 9

Specifying the precision when printing a float/double in C using printf [duplicate]

This question already has answers here:
printf string, variable length item
(2 answers)
Closed 4 years ago.
I wanted to create a function that would print my array of doubles using a specified floating point precision as an argument to the function.
Let's say i have the following code:
void printDoubleArrayPrecision(int length, double *arr, int precision) {
printf("[");
if (length > 0) {
printf("%.2lf", arr[0]);
for (int i = 1; i < length; i++) {
printf(",%.2lf", arr[i]);
}
}
printf("]\n");
}
Is there any way of replacing the 2 in printf(",%.2lf", arr[i]); with the given argument precision?
There is actually a possibility in printf:
printf("%.*lf", precision, arr[i]);
will print arr[i] with the given precision.
Apparently i should've searched for my solution by looking for printing strings with variable length instead of doubles.
A solution for my question would be:
printf(",%.*lf", precision, arr[i]);

array,pointer address value confusion [duplicate]

This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 10 years ago.
in running the code below , i got this output
num= 2359120, addr of num=2359120, *num=10,addr of num[0]=2359120
I can't comprehend how num and &num have the same value. any help, please? i know that the name of an array is a pointer itself
#include <math.h>
#include<stdio.h>
main()
{
int num[]={10,20,30,40,50};
printf("num= %d, addr of num=%d, *num=%d,addr of num[0]=%d\n",num,&num,*num,&num[0]);
}
name of array num is same as the address of the array &num which is same as the address of the first element &num[0] and hence, your output.

Resources