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};
Related
This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
No out of bounds error
(7 answers)
How many members calloc allocates in C [duplicate]
(4 answers)
Closed 1 year ago.
Here I have declared an integer array of size 2 but I am able assign more than 2 elements to the array and also print them but the array size is indicated as of size 8bytes before and also after assigning a value to it.
int main()
{
int a[2];
printf("size is %d\n",sizeof(a));
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
printf("%d %d %d %d\n",a[0],a[1],a[2],a[3],a[4]);
printf("size is %d\n",sizeof(a));
}
Output
size is 8
1 2 3 4
size is 8
So is it possible to assign more elements than the specified size to an array? If yes is it only applicable if you declare the array in this fashion and can this be applied to any type of array in c?
This question already has answers here:
Printing leading 0's in C
(11 answers)
Closed 2 years ago.
my question is short, if I have the following 2 lines of code:
int var = 01;
printf("%d", var);
the output is : 1
how do I get 01 rather than 1?
Use left padded format string.
Solution
int var = 1;
printf("%02d", var);
This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 3 years ago.
I know this is a very basic question in C, so I apologize beforehand if this is naive: I'm just learning the basics a few days in.
int main() {
// initialize the variables
int num_whitespaces, num_other;
int num_digits[10];
printf("%lu", sizeof(num_digits));
}
This prints 40, which seems odd to me. Why wouldn't this print 10, which would be the length of the array in javascript or so.
sizeof returns the size in memory. Not the length of something.
Same for C++ and this link has the same example :) https://en.cppreference.com/w/cpp/language/sizeof
size of empty class: 1
size of pointer: 8
size of array of 10 int: 40
size of array of 10 int (2): 40
length of array of 10 int: 10
length of array of 10 int (2): 10
size of the Derived: 8
size of the Derived through Base: 4
Getting the length is pretty difficult in C: array_length in C
sizeof returns the size of an object in bytes
An int is 4 bytes. sizeof(int[10]) is 40.
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
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.