What happens when we convert string literal to integer in C? [duplicate] - c

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.

Related

Why is the printed result 5452853 [duplicate]

This question already has answers here:
Multi-character constant warnings
(6 answers)
Closed 11 months ago.
#include <stdio.h>
int main()
{
int a = '\12345';
printf("%d",a);
return 0;
}
Why is the printed result 5452853? I wonder what happened?
I combined phuclv's and Mark Dickinson's answers and I seem to understand that first of all '\12345' is equivalent to three characters '\123' = 1*8*8+2*8+3*1 = 83; '4' = 52 ; '5' = 53. Then the result is equal to 83*256*256+52*256+53 = 5452853
– HuangGuojun

How do i find these lengths of this integer arrays? [duplicate]

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.

Why am I getting 0 as the output? Shouldn't it be 40? [duplicate]

This question already has answers here:
Why printf() isn't outputting this integer as float number? [duplicate]
(8 answers)
Closed 4 years ago.
Why does the following code output 0 instead of 40?
#include <stdio.h>
int main()
{
int volume;
int length = 5;
int width = 8;
volume = length * width;
printf("%f", volume);
return 0;
}
The variable "volume" is Integer , you need in the printf function to change from %f that is for float variable, to %d that is for print Integer variable.
Declaration of a variable is int volume; and you are printing it by format specifier %f which belongs to float type of variable. Type casting requires (float)volume. In C programming it happens a lot because compiler dependency comes in picture.

C float decimal not being memorized [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 8 years ago.
I have a piece of code that looks like this:
float nb = 100 / 42;
printf("%.2f", nb);
which I expect to print out 2.38, but instead it prints out 2.00.
The 42 is just an example. In the original code it's a variable.
You need to specify the numbers as floating point themselves. Try this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
float nb = 100.0/42.0;
printf("%.2f\n", nb);
return 0;
}

Program doesn't show expected output [duplicate]

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

Resources