How to explain the output of this program in C? [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 2 years ago.
Improve this question
I've been working on this problem:
#include<stdio.h>
int main()
{
printf("%c", "abcdefgh"[4]);
return 0;
}
This gives e as the output, however I couldn't understand that how an array named "abcdefgh" with 4 elements is getting printed.
In nutshell, please tell me how this program works.

The string "abcdefgh" doesn't have 4 elements. It's an array with 9 elements (the characters plus the null terminator, and square brackets are the array subscript operator.
So "abcdefgh"[4] is getting the element at index 4 from "abcdefgh" which is 'e'.

In C, a string is sequence of character terminated by null character (\0).
When you declaring a string:
"abcdefgh"
it's actually:
char array[9] = "abcdefgh";
and you are accessing 5th character from the array, since array is 0-based data-structure.
you are actually doing:
printf("%c",array[4]);
therefore, it points to 5-th character in array;
a b c d e f g h
0 1 2 3 4 5 6 7

A string literal like "abcdefgh" is an array expression - its type is "9-element array of char" (8 characters plus the string terminator). The subscript operator works on it like any other array expression, so "abcdefgh"[4]" evaluates to the 5th element of the string, which is the character 'e'.

Related

how to print permutations of array of strings in c without recursion? [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 2 years ago.
Improve this question
Is anyone here who can explain me how to print the permutations of a array of strings in iterative method without using recursion. Please explain with code.
If the string is "abcd", put all of the "a" chars in position 0 for the first n-1! arrays, in position 1 for the next n-1! arrays, etc. Then put all of the "b" chars in position 1 for the first n-2! arrays, etc, all of the "c" chars in position 2 for the first n-3! arrays, etc, and all of the "d" chars in position 3 for the first n-4! arrays, etc, using modulo n arithmetic in each case to move from position 3 to position 0 as you are filling out the arrays.

how does array[1-'a'] work in c programming? [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'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.

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.

unexpected result from printf character print [duplicate]

This question already has answers here:
Accessing arrays by index[array] in C and C++
(2 answers)
Closed 4 years ago.
#include<stdio.h>
main()
{
printf("%c\n",1-3+2["nexus"]);
}
The result is v. How does it turn out?
What does the indentation (square ones) do?
2["nexus"] is probably leading us to 3 element of the nexus considering it as an
array of characters[arr[2]=x in our case]
following is 1-3viz -2 added to x character ie added to to ascii value
and the corresponding element is v
cant exactly figure out what the square identation really mean but according to the oput this is possible way .
2["nexus"] is same as "nexus"[2], which correspond to the 2nd index or the 3rd element of this string. ( Which is 'x' here ).
In equation, 1-3+ 2["nexux"] is like 1-3+'x' that represent 'v'.
Note
When a string is declared like "nexus".
nexus[0] is 'n'
nexus[1] is 'e'
nexus[2] is 'x'
nexus[3] is 'u'
nexus[4] is 's'

Insert string to double 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 years ago.
Improve this question
I got help here a few days ago in the construction of a function that gets a string of the like:
"1 2.2 3 4.4 5 6.6 7 8.5"
and insert it to array[2][4] (in this case) in the following form:
1 | 3 | 5 | 7
2.2|4.4|6.6|8.5
the function that do that the array is:
//'function' is char*,'array' is double array
for(p = function; *p != '\0' ;p++){//convert double by strtod
item = strtod(p, &p);
++div;
if((div%2)!=0)
{
array[0][j]=item;
}
else
{
array[1][j++]=item;
div=0;
}
}
the problem in the code that the function insert the values only to array[0][j] and not to array[1][j] the meaning the array seems like(if the array initialized to 0 in the begin):
1 | 3 | 5 | 7
0 | 0 | 0 | 0
the values insert only to the first line of the array,
if i call to the function with string:
"0.5 2 1 4 3 6 0.5 8"
so item always be 0 and i don't know why :\
The previous post:
Try to insert string to array to check if the string describe function
thanks
Initially, your code works. But then your pointer, p, is being incremented past the end of the string and you are seeing whatever is in memory after that, and overwriting the contents of your array. The problem is here:
for(p = function; *p != '\0' ;p++) {
item = strtod(p, &p);
// (other code)
}
After calling strtod for the very last number (8.5), the pointer p already points to the NULL terminator. But then, your for loop calls p++ which increments it one past the NULL terminator. Your function continues running for some time after that, filling the array with garbage and possibly corrupting memory.
A solution is:
for(p = function; *p != '\0' ;) {
item = strtod(p, &p);
// (other code)
}
In this case, on most iterations, p points to the space before each number. That is okay, since strtod explicitly ignores whitespace. After the last iteration, p points to the NULL terminator and your loop ends before corrupting the array and possibly other parts of memory.
Dealing with pointers this way is error-prone. I’d recommend using the parsing facilities provided in the language, starting with strtok().

Resources