C- Comparison operator instead of strcmp [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 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.

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.).

Need to understand this If statement [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 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.

How do I get a substring in C? [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 6 years ago.
Improve this question
For example if:
z = "xxxx; yyyy";
How can I get the substrings so that
x = "xxxx"
and
y = "yyyy"
where "xxxx" and "yyyy" can be any string of any length?
You don't get much of built-in strings in C, let alone substrings. When you need a substring, you build it yourself by copying relevant portions of the string into a properly allocated memory buffer, and then you null-terminate the result.
Here is an example:
char *c = "xxxx; yyyy";
char x[5], y[5];
memcpy(x, &c[0], 4);
x[4] = '\0';
memcpy(y, &c[6], 4);
y[4] = '\0';
Demo.

Is it possible to make a string such as "G" equal a specific number like 50? [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 6 years ago.
Improve this question
For example if I had the letter "B" in a array and want to count how many there are,could I make "B"=1 so I can easily count the number of b's.I do not think this is typecasting since I am do not want to make "B" itself = int B
You don't need to assign to a string, just use an ordinary variable.
int b_count = 0;
char *string = "This is a B and this is another B";
for (char *p = string; *p != 0; p++) {
if (*p == 'B') {
b_count++;
}
}
printf("There are %d B's in the string\n", b_count);

How to extract specified character string from string [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
char string[]="DGS021J0W0S1000.0S20000S3000.0S4000.0S50.00S60.00F";
how to get S[1-5]
thanks!
Use strncpy() standard function
char S[6] = {0};
strncpy(S, string+1 , 5);
If you want to copy from the beginning of the string to the 5th charachter, then your question should be
how to get S[0-4]
and not S[1-5] because array index in C start from 0 and not from 1. and the solution for this case will be
char S[6] = {0};
strncpy(S, string , 5);
I think you are looking for substring methods.
You can do it in two for loops in C.

Resources