How is this possible? [closed] - c

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
In following code there is declaration even before the block beginning of the main function. Is this allowed?
long long n,u,m,b;
main(e,r)
char **r; //<<<Is this possible???
{
for( ; n++ || (e=getchar()|32)>=0 ; b="ynwtsflrabg"[n%=11]-e?b:b*8+n)
for( r=b%64-25 ; e<47&&b ; b/=8)
for( n=19; n ; n["1+DIY/.K430x9G(kC["]-42&255^b||(m+=n>15?n:n>9?m%u*~-u:~(int)r?n+!(int)r*16:n*16,b=0))
u=1ll<<6177%n--*4;printf("%llx\n",m);
}
Source: I found this code on ioccc.org

Yes, C allows declarations outside of functions. These declarations define global or static variables (you need a static modifier for that).
Re-formatting your program produces this:
long long n,u,m,b;
main(e,r)
char **r; // Pre-ANSI parameter declarations; do not do that in new programs!
{
for( ; n++ || (e=getchar()|32)>=0 ; b="ynwtsflrabg"[n%=11]-e?b:b*8+n)
for( r=b%64-25 ; e<47&&b ; b/=8)
for( n=19; n ; n["1+DIY/.K430x9G(kC["]-42&255^b||(m+=n>15?n:n>9?m%u*~-u:~(int)r?n+!(int)r*16:n*16,b=0))
u=1ll<<6177%n--*4;printf("%llx\n",m);
}
There's some serious obfuscation going on here, but syntactically it's valid code.

Related

Using arrays in C (error in the output) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I had this task :
Given the following array, write a program that reverses all array elements then prints them.
int x[] = {1,2,3,4,5,6,7,8,9,10};
then i wrote that code :
int main() {
int x[] = {1,2,3,4,5,6,7,8,9,10};
int y[10] ;
int i;
i=0 ;
for(i=0 ; i<10 ; i++) {
x[i]=y[9-i] ;
}
printf("\r\n The reversed array is : \r\n{ ") ;
i=0 ;
for(i=0 ; i<10 ; i++) {
printf("%d \t ,",y[i] ) ;
}
printf("}") ;
return(0) ;
}
and the output was this
what is wrong with the code ?
The problem is in this line:
x[i]=y[9-i] ;
You are assigning to x; however, x is your input data. The y array is uninitialized, so when you assign values from it to x, you get garbage data. This is why generic variable names like x and y are generally discouraged; it's easy to mix them up and make mistakes like this.
Anyway, if you switch that around, and assign the values from x to y instead, it should fix your problem.

Bugs in a code snippet [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Spot all bugs in the code snippet below
uint arr[100]
for (uint i=99; i >=0; i--)
arr[i] = 0;
This is a question for the test, can anybody point me to all bugs in this snippet
uint isn't a type.
The first line is missing a semicolon.
i >= 0 is always true.
arr[0U - 1] is undefined behavior because it access outside the bounds of the arr array.
It's not clear that this snippet is running as part of a function. If it is not, then the entire for-loop is a syntax error.
Additionally, if this class is taking place before 1999, then:
You can't declare variables in a for loop. Instead, the uint i should be declared before the loop.
This code should probably be rewritten as simply:
unsigned arr[100] = {0};

Array and loops [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
I have an array contain 100 elements. Can anyone help me to figure out how to write loops that perform this:
data[0] = 1/(2*3*4)
data[1] = 1/(4*5*6)
data[2] = 1/(6*7*8)
...
data[99] = 1/(200*201*202)
data[0]-data[1]+data[2]-data[3]+data[4]-data[5]+...+data[98]-data[99]
I just can't understand how to start. Any suggestions would be appreciated!
Try this
double c=0;
for (int i=0;i<100;i++)
{
c=i*2+2;
data[i]=1/(c*(c+1)*(c+2));
}
for (int i = 0; i < 100; i+=2)
{
op+= data[i] - data[i+1];
}
My suggestions how to start, if you really want them and want to manage this by your own:
Generalize your algorithm:
Find a function f(x) such that data[i] = f(i)
Just write the algorithm in your native language.
Then learn basic operators of C language, including loop construct.
Write your "native language algorithm" in C language.
Just in one loop:
int total = 0;
for(size_t i=0; i<100; ++i){
int temp = (i+1)*2;
data[i] = 1/(temp*(temp+1)*(temp+2));
total = total + (i%2==0?data[i]:-data[i]);
}

What does predicate mean in C? [closed]

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 the following code:
int (*predicate)(char) = 0;
Can anyone tell me what this code means? What is the meaning of the word predicate in C?
The sentence is a declaration and definition of a pointer to a function taking one argument (char) and returning int. The pointer is initialized to the null pointer value.
The word "predicate" is the programmer's choice for the variable name.
Reference: cdecl
One might use predicate like this:
/* UNTESTED */
int IsLower(char c) { return c >= 'a' && c <= 'z'; }
int main () {
int (*predicate)(char);
predicate = IsLower;
if ( (*predicate)('f') == 1 ) printf("'f' is lower case!\n");
}

C array print segmentation fault? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
Why am I getting Segmentation fault? Could you give me some understadable explanation? Thanks in advance.
#include <stdio.h>
int main()
{
int i,j;
char* ips[1000];
char ip[15] = "192.34.132.52";
char port[4] = "4003";
for (i = 0; i < 10; i++) {
sprintf(ips[i], "%s:%d", ip, port);
}
for (j = 0; j < 10; j++) {
printf("[%d] = %s\n", j, ips[j]);
}
return 0;
}
You didn't allocate the memory for ips[i], sprintf doesn't do it for you. Add a line in the first for loop, before the sprintf:
ips[i] = malloc(sizeof(ip)+sizeof(port)+2);
EDIT: as huseyin tugrul buyukisik noted, port isn't big enough to hold 4 characters and a null terminator. And you should use the %s modifier for it as port is a string as well.

Resources