IF statement. Variables [duplicate] - c

This question already has answers here:
string comparison inside if condition malfunctioning [duplicate]
(2 answers)
Closed 6 years ago.
I am trying to implement an IF STATEMENT
in my C-Server.
read(client_fd,received_data,100);
printf("%s",received_data);
I want to print "ooo" if the received data equals to "test"
if (received_data == "test") {
printf('ooo');
}
?
The above example results in errors. (can't compile)
I don't know much C.
UPDATE :
Now I can compile fine ( after changing 'ooo' to "ooo" )
But although I am sending "test" to it.. if statement is not doing anything i think.

The == doesn't work for const char *.
Use strcmp instead.

Related

Return string after changing it in C [duplicate]

This question already has answers here:
Function returning address of local variable error in C
(3 answers)
returning a local variable from function in C [duplicate]
(4 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 4 months ago.
I know, there are multiple questions about this on StackOverflow but I couldn't find a fitting solution for my problem.
I want to generate product codes (e.g. 12a, important: numbers and letters) and return them and printf. But when I do it in my main I get no result. When I printf("%s\n", pcode) in my generator function it works, but after return pcode I won't get the result.
char* generate(int num) {
char pcode[3];
... // generating code
printf("%s\n", pcode); // correct output
return pcode;
}
int main(void) {
printf("%s\n", generate(12)); // wrong output
}

Using an Int in the if condition [duplicate]

This question already has answers here:
why negative number is taken as true in condition statement? [duplicate]
(5 answers)
Closed 2 years ago.
In my current class I am seeing a lot of times where my teacher puts a number as the condition of an if statement then asks for the output. I'm confused as to how these statements are being evaluated.
Example:
if(-1) {
x = 35;
}
or
if(0) {
x = 35;
}
Why does it go inside the if statement instead of just ignoring it? What is making if(-1) evaluate true? And why does if(0) evaluate false?
This is an online class so I can't get help directly from the teacher usually. I have searched for a few hours on various websites about C for loops, but I haven't found anything relevant yet to my question.
C doesn't have a "real" boolean type. When evaluating integers, 0 is a "false", and anything else is a "true" value.

usage of !!, __warned and __ret_warn_once as int in WARN_ON_ONCE macro definition [duplicate]

This question already has answers here:
What is "!!" in C? [duplicate]
(7 answers)
!! c operator, is a two NOT?
(4 answers)
Closed 3 years ago.
I was going through the WARN_ON_ONCE macro definition. I have doubt regarding the following line, what is the use of !! before condition. If we remove !! then also same will be stored in __ret_warn_once.
int __ret_warn_once = !!(condition);
What will happen when compiler executing the following source line.
static bool __section(.data.unlikely) __warned;

How to retrieve specific index value from web_reg_save_param arrray [duplicate]

This question already has answers here:
LoadRunner web_reg_save_param, ord=all, paramName_count issues
(4 answers)
Closed 6 years ago.
I am using the web param function to retreive certain values. I want to get the value of a specific index from the array item and store it in a parameter to be used in a web_link call.
char * tempVal;
web_reg_save_param("dynArray","LB=/EmployeeProfile/","RB=\">","ORD=ALL",LAST);
tempVal = "{dynArray_2}";
There is no error for the above statements but when accessing tempVal it is giving error
vuser_init.c(143): web_link("emp") started [MsgId: MMSG-26355]
vuser_init.c(143): Warning: The string 'tempVal' with parameter delimiters is not a parameter.
vuser_init.c(143): Error -27995: Requested link ("Text={tempVal}") not found [MsgId: MERR-27995]
You have a problem which is C language based. You cannot simply equate two items as you have done.
take a look at the strcpy() function combined with lr_eval_string()
strcpy (destination_C_variable, lr_eval_string( "{LoadRunner_source_variable}" ) );

Is there any function in C similar to LIKE in SQL to compare 2 strings? [duplicate]

This question already has answers here:
Regular expressions in C: examples?
(5 answers)
Closed 7 years ago.
I meant, something that we can use this way:
char string1[] = "???, buddy*\0";
char string2[] = "Hey, buddy, hello!\0";
if (like(string1, string2)
puts("strings are similar!");
else
puts("string are different!");
You want to use a regular expression library. See this question for the ANSI library information: Regular expressions in C: examples?

Resources