+ 1

sizeof funtion with a loop in c!

why the result of using sizeof function in the second example is differing from the first example: int c[5]={1,2,3,4,5}; // Example 1 : for(int i=0;i<5;i++) { printf("c[%d] = %d \n",i,c[i]); } printf("\n\n\n\n"); //Example 2 : for(int i=0;i<sizeof(c);i++) { printf("c[%d] = %d\n",i,c[i]); }

7th Feb 2023, 3:32 PM
Hasna Oulaiz
12 Respostas
+ 4
As a size note, you can use: sizeof(c) / sizeof(int) to get the number of elements in the int array c.
7th Feb 2023, 4:58 PM
Lochard
Lochard - avatar
+ 4
sizeof operator returns array size in bytes. To find length of array : https://www.freecodecamp.org/news/how-to-find-the-size-of-an-array-in-c-with-the-sizeof-operator/amp/
7th Feb 2023, 5:01 PM
Jayakrishna 🇼🇳
+ 3
Jayakrishna 🇼🇳 thank you 🙏
8th Feb 2023, 5:55 PM
Hasna Oulaiz
+ 2
Mirielle thank you for this explanation, I appreciate it 🙏
8th Feb 2023, 5:54 PM
Hasna Oulaiz
+ 2
You're welcome..
8th Feb 2023, 6:22 PM
Jayakrishna 🇼🇳
+ 1
Lochard thank you 🙏
8th Feb 2023, 5:54 PM
Hasna Oulaiz