unexpected result from printf character print [duplicate] - c

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'

Related

How C indexes works for integers in the C [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed last year.
in the online course from my university I met the next interesting fact:
The executing of the next line will produce '\0':
5["Peklo"]
I also tried some different examples by passing different integers and string literals. I know that C strings are const char * pointers therefore that code is valid and will compile, but I can not figure out how the output calculates/depends on the passing integer value of the string indexer. If someone knows, can you explain to me in detail why 0["P"], 0["A"] and 1["A"] produces different results (80, 65, 0)?
5["Peklo"] === "Peklo"[5] == (char[]){'P','e','k','l','o','0'}[5] == 0 == '\0'
[0] [1] [2] [3] [4] [5]

How to explain the output of this program 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 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'.

strcmp giving wrong outputs [duplicate]

This question already has answers here:
Is this the only return value for strcmp() in C?
(4 answers)
strcmp behaviour in 32-bit and 64-bit systems
(3 answers)
Closed 2 years ago.
So I studied that strcmp returns the difference between the asci value of the two characters being compared. But in my case it is giving a value of -1,0 or 1 only.
#include<stdio.h>
#include<strings.h>
int main()
{
char n1[]="Jerry";
char n2[]="Ferry";
printf("%d",strcmp(n2,n1));
return 0;
}
Ideally it should give -4,but dev cpp is giving an output of -1. Why is that?
strcmp gives the relative difference between 2 strings.
negative if the left item is less than right
0 if the left is the same as the right
positive if the left item is greater than the right
It doesn't give a range on the difference.
(HINT: what value for strcmp( "Hello", "Hfllo") ? )

why can't we compare two strings directly in c [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 7 years ago.
why can't we compare two strings in c program directly.For example i have tried the following example
char *str="int";
if(str=="int")
printf("yes");
else
printf("no");
For the above I got output as "no"
I have tried the above code by using the same logic as if for integers
ie
int i=10;
if(i==10)
printf("same");
But when I have modified the above code like the following
if((strcmp(str,"int"))==0)
printf("yes");
I got the output as "yes"
What is the problem in the first stated code?
A "string" in C is just an array of chars. Comparing two arrays with == just compares their addresses, which are different for different arrays. (Literals may or may not be the same, actually, depending on the implementation.)

How to succesfully pass a 2D array to a function? [duplicate]

This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 7 years ago.
GNU GCC compiler
Here is a function: int sumsintriangle(int *a,int n)
where a is a n*n matrix .
for some purpose I added
if(*(a+(i+1)*n+(j+1)) > *(a+(i+1)*n+j))
condition to my code which was working properly ;as the condition is true for the correct values.
but for the same code when I added
sum=sum + *(a+(i+1)*n+(j+1));
then it didn't work (eg;let say sum was initially 1 and *(a+(i+1)*n+(j+1) was 4 ) then summation it should be giving me 5..but it gives me 1 as output...why??
Even ,when I called the same value *(a+(i+1)*n+(j+1)) in printf function,for just an enquiry, it is giving me 4 (original value)as output ...?
Why it is , that *(a+(i+1)*n+(j+1)) is working properly with printf but when I called it with sum it gives me incorrect value?
If you can post your function properly it could be easier to help you. but i think you have an error when you put * before your expression that will give you the content of that expression, so be sure to get the values properly.
example:
int a[]; //declare an array
a[n] // will give you the element in position 9 of the array.
*a // will give you the first element, cause an array can be treated as a pointer (indeed it is).
I hope this answer help you. If not please tell me. Good luck!
Use This code code may be its work.
*(a+(i+1))*n+(j+1)

Resources