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
I keep hearing that using == operator to compare chars can cause some problems.
My question is - what kind of problems?
For example, is there any difference between using:
if (Text[0] == 'A') { ; }
and
if (!memcmp(Text, "A", 1)) { ; }
You've blurred together two separate ideas.
There's nothing wrong with
if (Text[0] == 'A')
It'll do just what you want.
What you probably heard, and what you can't do, is
if (Text == "A")
That will virtually always come out false, even if Text contains "A".
If you want to compare strings, you generally use strcmp:
if(strcmp(Text, "A") == 0)
Of course this only works on properly-formed, null-terminated strings.
Finally, while you can certainly do
if (!memcmp(Text, "A", 1))
if you really want to, it's a strange and potentially inefficient usage, which doesn't buy you anything.
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 rarely log on and am very new to the C language, therefore I apologize if this is a duplicate question or if this is a silly query.
I'm currently learning C and am hitting a wall with strings. I understand that char arrays are used in place of strings in the language. My question is, is there a better way than to assign an arbitrary value when declaring a char[] for user input(i.e setting the size of the array to one value when the user might enter more or less than that amount of characters)?
If you're on a POSIX system (basically anything that's not Windows), you can use getline(3) to do this. It will automatically allocate a buffer of the right size for you. Otherwise, you'll have to guess a length, allocate that, then read the input up to that length, and if you guessed wrong, use realloc to increase your guess and try again.
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 5 years ago.
Improve this question
I'm programming an OS.
So I want to know how can an expression like (2*(7+8)-9)+2 can be initialised into variable using user defined function like extract() :
extract(char a[])
{
//some code
}
char expr[] = "(2*(7+8)-9)+2";
int value = extract(expr);
I'm doing this for variable initialisation from string.( Any tips/ideas/suggestions? )
StackOverflow has already covered the topic of Infix expression parsing and Recursive Decent parsing using C++ here. Read it over to understand the algorithm, then see if you can convert it to C. That will definitely be "fun" and teach you a little bit about C along the way.
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've heard that lets say:
while(1){
i = !2;
wait(1);
}
is power efficient. Does this while loop stop at i != 2 and is therefore not polling? Let's say:
while(x == 3){
if(c == 3){
x = 4;
}
wait(1);
}
Does this follow a similar concept or is i = !2 a procedure that must be met in order to continue the while loop? Would you say that this is just as power efficient? Is the second example similar to the first in terms of power efficiency?
An example i've been shown using bad power efficient polling is:
while (x == 3) { }
The important thing from an efficiency standpoint is that the code doesn't just continually cycle. In your example, presumably the wait() function is returning control to your OS so that it can immediately dispatch another task.
In short, yes, your second example is power efficient as well, assuming wait() returns control to the the OS.
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
Is it possible in c programming language to check string with if else, and if it is, can someone please explain how, with some basic exampel. I would like to check if string matches a certain word. Thanks very much. For example if(n==0) instead of 0 to be a word.
To check for the contents of a string [whether is matches another], you cannot use == operator. You need to use strcmp() function with if condition.
check the man page here
If the strings match, it will return 0, otherwise, it will return non-zero. However, this is case-sensitive. ["Hello" and "hello" are not same]
Suppose, you want to check whether your string strcontains Hello or not. A pseudo-code will look like
if (! strcmp(str, "Hello"))
printf("String contains Hello\n");
else
printf("No Hello!!\n");
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
Why isn't the following possible?
printf("%d", "%d", 2, 4);
This would be a useful feature. Is it due to technical imitation or design reasons?
How would printf() know where the formatting strings ended and the actual values began? You're not including any such information, which is the entire point of the formatting string: describing the variable number of arguments so the code inside printf() knows how many arguments to process.
And in what way is that better than
printf("%d%d", 2, 4);
?
Also, the printed result ("24", without newline) would be pretty hard to interpret in any way, so you'd might as well add spacing, which will help make the formatting string more readable:
printf("%d %d", 2, 4);
That will print "2 4" (again, without newline).
Note that in C, there's no way for a variable-arguments function (printf() in this case) to somehow determine the amount (or types!) of its argument(s). It has to know, or be able to compute on its own based on some of the arguments (or some other state).
Also, if wanted to print "%s", I wonder how you imagine
printf("%s", "%s");
should work?