Can we include "strcmp" in printf 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 5 years ago.
Improve this question
Can anyone explain what is the meaning of this line of code
printf("%d",strcmp("strcmp()","strcmp()"))

The code is just a joke...
It just compares two fixed strings. To make it confusing the two strings contain the name of a library function (strcmp) but that - of cause - doesn't make it a function call.
The code
printf("%d",strcmp("strcmp()","strcmp()"))
is similar to
printf("%d",strcmp("A","A"))
It will print 0 (zero) as the strings are identical and strcmp return zero when strings have an exact compare.

strcmp() is responsible for lexicographic comparison of strings(character order).i.e.first character compared with first character if equal then second and so on.
Its signature is as follows:
int strcmp(const char *str1, const char *str2)
http://forgetcode.com/C/1026-strcmp-lexicographically-compares-two-strings
https://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm
This function return values that are as follows:
if Return value < 0 then it indicates str1 is less than str2.
if Return value > 0 then it indicates str2 is less than str1.
if Return value = 0 then it indicates str1 is equal to str2.

Related

What is the meaning of the 2 in calculating the length of a substring as "(right-left+2)"? [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 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am designing a program in C to detect tokens.
I got confused in the 4th line. What does the (right-left+2) do?
char* sub_string(char* str, int left, int right)
{
int i;
char* sub_str = (char*) malloc(sizeof(char)*(right-left+2)); /* 4th line */
}
right-left+2 in this case would be right-left+1+1.
Where righ-left would be the length from left to right, but without counting the left one. Hence one +1.
The other +1 is for making one additional character of space, most likely for adding a 0 termination.
By the way, the function is missing a clean return statement.
E.g. (thanks David C. Ranking for the idea of an example) "this" within "more this than that":
more this than that
0 5 8
The 't' is at left 5, the 's' is at right 8. The length of "this" is clearly 4 (not counting the, absent, 0 terminator). But 8-5 is only 3. So use one +1 to include the additional character (by my counting the 't' is missing...).
If that substring is to be returned via a pointer to malloced memory, which is what I guess the (missing) rest of the function is supposed to do; it should include the 0-terminator in the malloced memory. That needs another character of memory to be malloced; by using the second +1.

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

How can I delete the first zero(s) in a string? (Without using atoi) [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 was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}

Why does the printf of ++**++argv output 'p'? [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
#include<stdio.h>
int main(int argc,char *argv[]) {
printf("%c",++**++argv);
return 0;
}
Suppose the command line arguments passed were:
./a.out one two three
The output is:
p
Can someone please explain me what is happening?
Start from the back of the ++**++argv expression:
argv starts off as a pointer to element zero, i.e. "./a.out" or ""
++argv is char** that points to string "one"
*++argv is char* that points to string "one"'s initial element
**++argv is char that is equal to string "one"s initial element, i.e. 'o'
++**++argv is char that follows 'o'. On most systems that's 'p'.
The last operation modifies program's arguments in place, which is allowed by the standard (Q&A).

Lexicographic Order in C [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
I am working on strings in C and I would like to ask What exactly Lexicographic Order is and how is being used in C. Which is the best way to compare 2 strings . I have read about strcmp and it's lexicographic comparison but I am confused.
strcmp uses lexicographic order to compare strings. That is, it follows the alphabet. In English, F comes directly before G, and Z comes directly after Y. strcmp takes this into account, because the characters in the ASCII table succeed in alphabetical order. A typical strcmp function would be
int strcmp(const char *a, const char *b)
{
for (; *a && *b && *a == *b; ++a, ++b)
;
return *b - *a;
}
It loops through the characters, while the \0 was not seen, and the current characters are equal. As soon as the characters become unequal or the \0 is seen, the loop is broken, and the return expression is evaluated. If the \0 was seen, then both characters are equal, and \0 - \0 must be 0.
Lexicographic order is the order defined by the lexicon, or the order defined by how people communicate. We English-speaking humans agree that in our lexicon the word Apple comes before the word Cat, we call it 'alphabetical order'. In your case, using strcmp, it will be the order defined by the ASCII chart, which also works out to be alphabetical as long as you use a consistent case. An 'A' is less than a 'B' because the 'A' appears before the 'B' in the ASCII chart.

Resources