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.
Related
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'm working on a program to find the anagram to two strings in c. But I end up in a statement
array[1-'a']++;
and I don't quite know the working of that statement.
Anyone out there help me..!
ASCII value of 'a' is 97. 1 - 97 is -96. array[-96] is equivalent to *(array - 96). If array - 96 is a valid address then array[1-'a']++; will give the value at address array - 96 incremented by 1, otherwise dereferencing it will invoke undefined behavior.
'a' is a character constant and it is an integer which is the character code of a (97 if ASCII code is used).
array[1-'a'] is equivalent to *((array)+(1-'a')), which is 1-'a' element after ('a'-1 elements before) the element pointed at by array. ++ is incrementing, which is equivalent to adding 1. array[1-'a']++ is postfix increment, so this expression will be evaluated to the value of array[1-'a'] before incrementing.
An example of possible usage:
#include <stdio.h>
#include <limits.h>
int main(void) {
int data[SCHAR_MAX] = {0};
int* array = &data['a'];
printf("%d\n", data[1]); /* 0 will be printed */
array[1-'a']++;
printf("%d\n", data[1]); /* 1 will be printed */
return 0;
}
array[1-'a']++;
a is ASCII 97.
1 - 97 is -96
array[-96], if a valid address, is 96 elements before the base address of array
++ increments its operand, or array[-96].
This code, however, is highly dubious in its correctness.
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).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
When I run this program it prints a strange character in the terminal. Can someone tell me what that is?
int main(){
char x=1;
printf("%c\n",x);
return 0;
}
This is because you are assigning 1 (ASCII value of a character) to x . Assign '1' to x. It will give output 1.
char x = '1';
printf("%c\n",x);
Its printing character 1 from the ASCII table
(making reasonable assumptions about your platform, that you aren't on an EBCDIC platform or something)
Characters in C should usually be assigned using characters, not by using ASCII encoding. For example:
char x = 'A';
printf("%c\n", x);
Will print the character 'A' on the terminal. By giving the character the ASCII index of 1, you're assigning it the START OF HEADING character (SOH). If you were looking for 'A' or '1', that would be:
char x = 65; // x = 'A'
char y = '1'; // y = '1'
But like I said, this is very awkward and requires ASCII memorization to read, so assigning numbers is very bad practice.
You can find an ASCII table at: http://www.asciitable.com/
It depends on what you want as output. If you think that your code is giving something weird as output, you certainly want '1' as output. And for that, you should replace your statement
char x = 1;
to
char x = '1';
Thats it! Your problem would be solved!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My teacher gave us a question in programming class today and I don't understand how he got the answer. I was hoping someone could explain it to me. We basically have to show what the programs output would be, however I am somewhat confused as to how to get the answer to the question. The Question is as follows:
#include <stdio.h>
void do_something (int , int * );
int main (void)
{
int first = 1, second = 2 ;
do_something(second, &first);
printf("%4d%4d\n", first, second);
return (0);
}
void do_something (int thisp, int *that)
{
int the_other;
the_other = 5;
thisp = 2 + the_other;
*that = the_other * thisp;
return;
}
Answer
35 and 2
The function do_something contains 2 parameters.
normal integer (thisp)
pointer to an integer. (that)
What your teacher wanted you to learn is, pass by value and pass by address.
In pass by value, the original value doesn't change. This is because in the example given.
value of variable second is copied thisp variable.
In pass by address, the original value can be modified within the function.
This is because, the pointer that is pointing to the location of variable first. So if value of that is changed, the value of first will also change.
This is why, value of first is changed in the output and value of second is unaffected.
thisp = 2 + the_other;
*that = the_other * thisp;
Means:
thisp = 2 + 5
*that = 5 * 7
And that contains address of first in main which is overwritten in do_something as 35. Second remains 2.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have doubt in this line flag[str[i]-'a']++; how this line work.
For full program visit http://www.programmingsimplified.com/c/source-code/c-anagram-program
char str[44];
int flag[26],i=0;
gets(str);
while(str[i]!='\0')
{
flag[str[i]-'a']++; // How this line work
i++;
}
i=0;
while(str[i]!='\0')
{
printf("\n%d, %d ",str[i]-'a');
i++;
}
Lowercase 'a' resolves to decimal 97. Subtracting 97 basically allows you to use characters 'a', 'b', 'c', etc. as indexes to the flag array. Once you have that, the ++ is incrementing the appropriate letter slot in the array.
So flag[0] is for the letter 'a', flag[1] is for the letter 'b', and so on.
flag[str[i]-'a']++;
post incrementing flag[someindex] value
someindex vale is counted str[i]-'a'
if str[i]='c' then
someindex='c'-'a' ==> someindex=2
post incrementing flag[2];
i'm guessing you have a lower case letter in str[i] and in that case str[i]-'a' will mean the letter's number, as in
a=0
b=1
c=2
.
.
.
and at the end you have
flag[str[i]-'a']++;
so it's an array of letters, incrementing each iteration the current letter's cell and generally it counts how many times each letter appears
for example if you have the string "aaccdvb
you'll get:
str[0] = 2
str[1] = 1
str[2] = 2
str[3] = 1
str[21] = 1
and all the rest are 0
Without really parsing the rest of the program... flag[str[i]-'a']++ translates to:
Take a value str[i] and subtract it from 'a'. 'a' resolves to 97 in ASCII, so you're taking whatever value is in str[i] and subtracting it from 97.
Take the result of the difference and use it as an index into the flag array.
Apply a post-increment operation of the resulting value determined by the previous two steps.