How can I quote a variable? [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 8 years ago.
Improve this question
How can I quote a variable in C?
I have this:
inputpass[0] = selectorValue;
And doesnt return the expected result.
Works fine with:
inputpass[0] = '1';
I need to put the variable selectorValue between single quotes. ¿How can I do that?
I tried with inputpass[0] = "'" + selectorValue + "'" but didn't work.
I want to make a char array, and selectorValue is an integer variable. That's why I want to put it between single quotes.

I don't know why you want to quot a variable, but I think you possibly are missunderstanding the way in that characters work in C.
In C, a character constant, like '1', has a value of type int, that is, an integer.
If your variable selectorValue is an integer, then nothing is needed to do.
I imagine that selectorValue is an integer like 1, 2, 3, ...
In this case, if you want to convert it to a character, you have to add the ASCII value of '0' to the integer, in this way:
inputpass[0] = '0' + selectorValue;
But this approach only works with values between 0 and 9. So, be carefull.
Anyway, I am guessing, because your question is not very clear.
Finally, in C, what makes the difference between the ASCII code of a character, for example 'A' (which is 64) and the character itself, comes in the moment that you have to show the information.
printf("%c", 'A'); // Prints the character: A
printf("%d", 'A'); // Prints the ASCII value of the character A: 64
The OP explains that he want to "add" single quotes, explicitely.
I think to achieve this could be very complicated, if not impossible.
In this moment I cannot see or remember the way to do that.
(To add double quotes is easy, by using the operator # in macro definition).

Related

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;
}

Not understanding weird behaviours of printf() [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 4 years ago.
Improve this question
I found a question asking for output of the following statement :
printf("%d"+1,123);
The answer given was d, It's explanation was : Since "%d" is a string, +1 here means d.
123 just gets ignored.
My first question is : why 123 gets ignored?
I also ran the following statement
printf("%d"+2,123);
It printed nothing. The code runs but without errors.
My second question is : why did the code compiled without errors?
Third time, I did following:
printf("%d"+0,123);
Output was 123.
So I am getting real confused here. If +1 printed d, then shouldn't +0 print %?
Imagine a string:
char str[] = "%d";
Now we know that:
str[0] == '%'
str[1] == 'd'
str[2] == '\0' = 0x00
str+2 == &str[2] == the address of the byte 0x00 inside the str string == ""
printf("%d", 123); is the same as printf(str, 123)
printf("%d" + 2, 123); if the same as printf("", 123); and it will print "", ie. nothing
A character point with an addition lead to a character point.
printf is just a function
So it takes a variety of parameter - varags
"%d"+1 Will be a string just dn it
"%d"+2 - will be the null byte - nothing
"%d"+0 - Will be %d - hence expected output - see the manual page
Answer to your first question:
123 gets ignored because while writing printf("%d"+1,123), +1 places the pointer at index 1 of %d i.e d. Since, to print 123 we need the pointer to be at % and access %d and not just d. Hence, only d gets printed out in this case.
Answer to your second question
It compiled without error because printf() is just a function and it takes various arguments. For more details about printf() you can visit this link
And in the third case i.e printf("%d"+0,123), the output is 123 because here the pointer's position is at 0 i.e at % and we have access to %d. Hence, we're getting 123 as output.
hope this will help you.

Array access args[0][1]-'0' [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 4 years ago.
Improve this question
I have searched for any reference to this for quite a bit now but I haven't had any success, so I thought I would ask here. Basically, I am trying to understand a C written program for creating a shell in linux and I am having problems with this piece of code
...
else if (args[0][0]-'!'==0){
int x = args[0][1]-'0';
int z = args[0][2]-'0';
...
}
The args is storing the command's entered by the user. For instance, later the address space of the child (parent process reads the commands, child executes them) is replaced using a call to execvp(args[0], args). The definition of args is as follows: char *args[MAX_LINE/2 +1];
What I have been having trouble understanding is the ways in which the array is accessed; specifically what is meant by these expressions in this context:
args[0][0]-'!'==0
args[0][1]-'0';
args[0][2]-'0';
Judging by the name of the variable, args stands for a list/array of arguments.
arg[0] is the first element of that array.
args[0][0] is the first character of the first element of that array.
The expression args[0][0]-'!'==0 checks whether that character is equal to '!'. That could have been written better as args[0][0] == '!'.
It's as if instead of using if ( i == 10 ), you decide to use if (i-10 == 0).
The next two lines
int x = args[0][1]-'0';
int z = args[0][2]-'0';
expect that the second and third characters of the first argument are digits and extract the decimal values they correspond to. If the first argument is "!26", then x will have the value 2 and z will have the value 6.
That logic depends on the guarantee that the encoding used for the characters '0' - '9' are required to be contiguous.
Probably args is a reference to
int main(int argc, char **argv);
Then args[0] is the name of the program and in the following args you would find the Arguments of the Programm, see e.g. Arguments to main in C
Thus args[0[0] is the first character of the name of the program.

Can part of a string be copied onto another string in C? But the begin index for copying isn't zero [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
For example, I have this string:
ABABcct
Can I copy
ABc
begin index = 2, end index = 4
...from that string?
Is there any specific built-in function that can do this?
I know strncpy() can copy a number of characters of a string starting from the beginning of the string onto another. But I don't think it can do what I have just described. As far as I know, its "begin index" is limited to 0.
Actually you can use the function strncpy() with a pointer to anywhere in your string, not only a pointer to its beginning.
Example:
char *src = strdup("ABABcct");
char *dest;
strncpy(dest, src + 2, 3) // Copies 3 characters from your pointer src + 2 to your pointer dest
If you run this code, dest will contain ABc.
While in your case the standard copy functions would eventually work, their use is not allowed when the source and the destination are overlapped, in which case you'll experience an UB.
For such issue the standard C library provide the function memmove designed to handle overlapped areas. It works as using an intermediate buffer to solve any overlapping problem.
see also Meaning of overlapping when using memcpy
With strncpy
#include <stdio.h>
#include <string.h>
int main(void)
{
char p[10],s[10]="abcdefgh";
strncpy(p,s+2,3);
puts(p);
return 0;
}
I know strncpy() ... As far as I know, its "begin index" (as I call it) is limited to 0.
You knew it wrong. It's not about begin index or anything - simply you need to pass relevant char* in src and target which is being done here.
To add further clarification - most standard library functions which you are talking about never put a constraint of passing the pointer to the 0-th index. It doesn't matter which index you send - you can send 100th or 50th doesn't matter as long it follows the things expected by function (null termination etc).
The code posted here is for illustration purpose - didn't include the necessary checks needed to use str*cpy functions.

Store 2 ascii characters into 1 integer 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 8 years ago.
Improve this question
I am currently working on a project involving ascii command strings for a display. I have a string in my program (command string defined by a separate function) that I need to take apart and store into an array. I need to store 2 ascii characters into each element of the array. Here's my problem, the device is looking for ascii in HEX, my hardware will store the variables as decimal ascii. So if I wanted to send the characters 'B' and 'A' to the first element, the display would expect to see 4241 in HEX, it would be 16961 in decimal rather than 6665. If anyone has any suggestions, I would be extremely interested. Thank you
You can just combine the two chars using a shift and bitwise OR:
char ch1 = 'A', ch2 = 'B';
uint16_t buff = ch2 << 8 | ch1; // buff = 0x4241 = 16961
LIVE DEMO
Note on programming style: even though it's not necessary, some people prefer to add parentheses for clarity:
uint16_t buff = (ch2 << 8) | ch1; // buff = 0x4241 = 16961

Resources