This question already has answers here:
Printing array elements
(6 answers)
Closed 9 years ago.
The expected output of the following C program is to print the elements in the array. But when actually run, it doesn't do so.
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);//printing the array
return 0;
}//looks simple but no result
What's going wrong? Why am I not getting any output?
In the comparison
d <= (TOTAL_ELEMENTS-2)
TOTAL_ELEMENTS has type size_t so d is converted to unsigned. For, say, sizeof(size_t)==4, this makes the test
0xffffffff < 5
which fails, causing the loop to exit.
If you really want to start your loop counter from -1
d <= (int)(TOTAL_ELEMENTS-2)
would work
Related
This question already has answers here:
C sizeof a passed array [duplicate]
(7 answers)
Closed 1 year ago.
I want the output to be 6 and 4 four array A and B but I cant figure out where I made a mistake. I read a tutorial on StackOverflow where u can calculate a int array length by doing length = sizeof(array)/sizeof(array[0]), but it just doesn't work in my program. The values of sizeof(array) and sizeof(array[0]) stay constant regardless if I change the array as shown below.
#include<stdio.h>
#include<string.h>
int arraychecker(int array[])
{
int Length =sizeof(array)/sizeof(array[0]);
int arraylength = sizeof(array);
int arraylength0 = sizeof(array[0]);
printf("%d,%d,%d\n", Length, arraylength, arraylength0);
}
int main()
{
int a[]= {2,1,3,4,9,33};
int b[]={2,55,3,2};
arraychecker(a);
arraychecker(b);
return(0);
}
output: 2,8,4
2,8,4
Adding to Eugene's comment, some common practice is to also store the length in the array.
This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 3 years ago.
My code for the following simple coding exercise produces a "division by zero" warning, and I'm wondering why.
#include <stdio.h>
int main() {
for(int i = 0; i < 100; i++) {
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
}
return 1;
}
temps.c: In function ‘main’:
temps.c:6:45: warning: division by zero [-Wdiv-by-zero]
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
I realised while writing this question that it's because I should have written 5.0/9.0, since C handles division with integers in a way that I didn't expect. Posting this anyway since I couldn't find this particular error linked to this particular problem on SO.
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:
Multiple characters in a character constant
(3 answers)
Closed 6 years ago.
Consider a C program:
#include <stdio.h>
int main (void)
{
int x = 'a';
printf("%d", x);
}
Here the output is 97 as per the ASCII value table.
But in the example below:
#include <stdio.h>
int main(void)
{
int x ='aa';
printf("%d", x);
}
The output is 24929.
Can anyone please explain how the literal has been converted to this integer value?
int x ='aa';
This is valid but value of x is implementation defined. And btw, this is not a string literal. String literal would be this "aa".
You assigned a value to the int using octets : 'a' is 0x61.
So writing int x = 'aa' is like writing int x = 0x6161.
Edit: but do not write that. Just write int x = 0x6161 or int x = 24929.
This question already has answers here:
Strange behaviour of macros C/C++
(5 answers)
Closed 8 years ago.
I have the following piece of code.
#include <stdio.h>
#define d 10+10
int main()
{
printf("%d",d*d);
return 0;
}
As 10+10=20, I thought that d would be 20 everywhere in the program. But when I execute d*d, I have expected the result to be d*d=20*20=400. But the result gets printed as 120. Can anyone give me an explanation for this behavior?
Preprocessor is just doing simple "find & replace", so this code:
printf("%d",d*d);
changes to
printf("%d",10+10*10+10);
which is 10+100+10 = 120
That's why it's so important to add parens in defines:
#define d (10+10)
printf("%d",d*d);
Here d is replaced as printf("%d",10+10*10+10). so in this case it first executes 10*10 and adds with 10+100+10. so results 120.
To eliminate this-
#define d (10+10) // FIX
int main()
{
printf("%d",d*d);
return 0;
}
Change
#define d 10+10
to
#define d (10+10)
What you get without the proper parentheses is 10+10*10+10 which is the same as 10 + (10 * 10) + 10
printf("%d",d*d) will evaluate (after preprocessing) to printf("%d", 10+10*10+10);