Need to understand this If statement [closed] - c

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.

Related

assign zero to item in char array [closed]

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 months ago.
Improve this question
I have some materials showing this code. what does code means by making these assignments?
char inputfilename[128];
inputfilename[0] = 0;
char *argv[128];
*argv[1] = 0;
In C, character arrays are terminated by a null character (value 0). In both cases in your example, the code initializes the strings to "empty" (with a terminator in the first element). This would prove useful in any subsequent string operations (strcat, strcpy, etc.).

What does the character "%XX" mean in C language? [closed]

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.

Integer Overflows in C and Defenses Against Numeric Errors [closed]

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 4 years ago.
Improve this question
I am trying to research various integer overflow scenarios in C and I was wondering does the C language provide any defenses against numeric errors and are there any additional classes or libraries in the C language that can help with that? Also, can anyone give me an example of code that results in an integer overflow in C?
No, there are no defenses.
This overflows:
#include <limits.h>
#include <stdio.h>
const int a = INT_MAX - 2;
const int b = INT_MAX - 2;
printf("%d + %d = %d\n", a, b, a + b);
When I tested it it printed -6, but anything could happen I guess.

C- Comparison operator instead of strcmp [closed]

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 5 years ago.
Improve this question
What will happen if I use comparison operators to compare strings instead of strcmp in C? Will it compare its ASCII value and return results?
It will compare the addresses of the two pointers.
so:
char* a = "hello";
char* b = "test";
char* c = "hello";
char* d = a;
a == d; // true
a == b; // false
a == c; // true or false, depending on the compiler's behavior.
The third example will be true if the compiler decides to recycle the actual string data for "hello", but it has no obligation to do so.

Regarding casting -1 to size_t leading to a stuck loop [closed]

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
an interview question on glassdoor is as follows. With my knowledge, it is hard to deduce anything out of it. What could be an appropriate question?
A macro that computes a size_t number. Putting in a loop, it casts -1
to a size_t number, making the loop impossible to start.
as suggested by Michael Aaron Safyan, following might be the case
operates in the reverse:
for (size_t i = 0; i > ((size_t) -1); i--) {}
For explanation see the answer
The issue is that size_t is unsigned, so casting -1 to it will produce the maximum-valued size_t. One would fix this case by using a signed type (such as int or ssize_t).

Resources