Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am finding difficulties to solve an expression please help me to do so.
Declare four variables of type char. Initialize one variable to ‘z’. Initialize the other variables to the ASCII integer value for ‘z’, the ASCII octal value for ‘z’, and
the ASCII hexadecimal value for ‘z’.
Just declare a char variable and assign the value. To assign hex prefix with 0x, for octal use 0 and for decimal just write the number with no prefix.
The decimal ascii value of 'z' is 122.
#include <stdio.h>
int main() {
char a = 122;
char b = 0x7a;
char c = 0172;
char d = 'z';
putchar(a);
putchar(b);
putchar(c);
putchar(d);
}
All these char variables have the same value so four zs will be printed.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm having trouble finding what does %XX mean in C
an example would be like: printf("%XX\n", num);
edit: num is an int
print num converted to unsigned int as a hexadecimal number and then print char 'X'
In a printf format string, %XX is %X followed by X.
The %X says to format an unsigned int argument as hexadecimal, using uppercase (ABCDEF for the “digits”).
The X is simply a literal X that says to print an “X” after the hexadecimal numeral.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to understand more about this if condition the two sides of comparison and how it is compared:
int main()
{
unsigned short i;
if (i == '9' * 256 + '5')
{
/* Do stuff */
}
}
How are these compared?
Formally the behaviour of your code is undefined as you are reading the uninitialised variable i.
'9', 256, and '5' are all int types in C. So the right hand side is evaluated in int arithmetic, with the potential for overflow (it will not overflow with ASCII encoding).
i will be converted to an int type prior to the comparison.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've found this function on the internet and I find it to be very useful
but I am new to programming, and could someone please explain briefly what does it exactly do
#include <stdio.h>
int diffcount(char* s)
{
unsigned char seen[127];
int cnt=0,i;
for(i=0;i<127;i++)
seen[i]=0;
for(i=0;s[i];i++)
{
if(!seen[(int)s[i]])
{
cnt++;
seen[(int)s[i]]=1;
}
}return cnt;
}
int main(void) {
char string[20];
scanf("%s",string);
printf("Razlicitih znakova: %d\n", diffcount(string));
return 0;
}
First of all we init an empty array of zeros int seen[127];
"seen" array is used to find out whether char with code i has been met in the array s : if seen[i]==1 than (char)i was in the string s.
After that we make a loop through char* s and check if char s[i] has already been met by looking at the value of seen[s[i]]; and if it is false we put seen[s[i]]=true (because we met it) and increase our counter.
The result of the function is the value of variable cnt
This may also help:
each char has it's code between zero and 127. For example, (int)'a' = 97.
bool in the C is just the same as int, that's why we sometimes use 0 and 1 instead of true and false
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently working on a project involving ascii command strings for a display. I have a string in my program (command string defined by a separate function) that I need to take apart and store into an array. I need to store 2 ascii characters into each element of the array. Here's my problem, the device is looking for ascii in HEX, my hardware will store the variables as decimal ascii. So if I wanted to send the characters 'B' and 'A' to the first element, the display would expect to see 4241 in HEX, it would be 16961 in decimal rather than 6665. If anyone has any suggestions, I would be extremely interested. Thank you
You can just combine the two chars using a shift and bitwise OR:
char ch1 = 'A', ch2 = 'B';
uint16_t buff = ch2 << 8 | ch1; // buff = 0x4241 = 16961
LIVE DEMO
Note on programming style: even though it's not necessary, some people prefer to add parentheses for clarity:
uint16_t buff = (ch2 << 8) | ch1; // buff = 0x4241 = 16961
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is there any way how to put integer sized 100 000 into 4 elements of char array? If I use sprintf or itoa, array has 6 elements. I tried to use this, but it didnt work. And is there any way how to put these 4 elements back to integer?
char *s;
int value = 100000;
*((int *)s)=value;
Note that:
int value = 100000;
char *s;
*((int *)s)=value;
dereferences uninitialized pointer s, which causes undefined behavior. You could do:
int value = 100000;
char s[4];
*((int *)&s[0])=value;
just note that this stores value in the memory block "occupied" by charr array (at memory level) unlike sprintf, which would print the value in a form of string (characters representing the number).