Two chars less:

Sorry Adad..your code is of 93 characters:--

reduced code:-

#define X scanf("%d",&
main(s,n,t){
  for(X t);t--;){
    X n);
    n>0?s+=n:0;
  }
  printf("%d",s-1);
  return 0;
}

code is now of 88 characters.

Now I'm curious what they have in the smallest programs.

I am also very curios to know that how the guy have submitted his code in 63 characters. what skills he has used.

In my very limited experience with these kinds of programs, they love to explore the limits - and frequently venture outside the limits of modern C, into a deprecated version that won't compile at all on any modern compiler (even turbo C).

In the above program, you haven't defined the data type for the s, n, or t variables. Even though a compiler might accept it as OK, the standard for C would not allow it - making it "undefined behavior". (which means it might run on your compiler, but it might not run, or might produce the wrong results, on another standard C compiler).

Did you try compiling and running this program? On what compiler?

Some obfuscated programs are really legal and quite ingenious and short. Others are outrageous crap that will crash on 95% of the systems and compilers.

Only by testing them sometimes, can you tell which is which.

Did you try compiling and running this program?

yes, I did.

On what compiler?

I am using Dev-C++ 4.9.9.2.

surprisengly it even compiles without errors on vc++ 2010 Express

1>------ Rebuild All started: Project: test3, Configuration: Debug Win32 ------
1> stdafx.cpp
1> test3.c
1>c:\dvlp\test3\test3\test3.c(2): warning C4131: 'main' : uses old-style declarator
1>c:\dvlp\test3\test3\test3.c(2): warning C4431: missing type specifier - int assumed. Note: C no longer supports default-int
1>c:\dvlp\test3\test3\test3.c(3): warning C4013: 'scanf' undefined; assuming extern returning int
1>c:\dvlp\test3\test3\test3.c(7): warning C4013: 'printf' undefined; assuming extern returning int
1> test3.vcxproj -> c:\dvlp\test3\Debug\test3.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

Is there any alternative to n>0?s+=n:0; which can reduce the characters..???

It seems obvious that there is such code - look at the shorter programs to verify that.

I don't know any way to shorten that up, but I'm thinking it must be done with a completely different algorithm. What that is, I have no clue.

The problem with it is, if you were to make the very smallest code possible, your boss would tell you to NEVER code like that, again. ;)

It's great having a short function or program, but if it's hard to understand - cryptic or overly clever, then it's a nightmare to have to maintain or extend (as is so very common).

There are websites that specialize in this kind of code, so studying and asking this question there, would be helpful. After this amount of time, I believe everyone here who has an interest in this (even a slight interest), has already given your their best idea.

adak :- Will you help me with such websites..???

Not sure if tabs and newline characters count but its been trimmed a bit too

#define X scanf("%d",&
main(s,n,t){for(X t);t--;){X n);s+=n;}printf("%d",s-1);}

<< White space like tabs, and newlines, don't count >>

I can help you to find a couple on Google, but I can't help you learn what they're doing - I simply don't code that way, and have no intention to start studying to program that way.

Life is short, so make sure what you are doing is worth your time.

Anyway, this is the famous obfuscated contest web site, that was already mentioned:

http://www.ioccc.org/

and this is one example of a problem, and the road to a winning solution:

http://home.datacomm.ch/t_wolf/tw/misc/contest.html

Note how the definition of what is and is not allowed, becomes all important. If you don't know the standard that they're following for the rules of the contest VERY well, you could easily waste a good deal of time.

I have a question. In your question it states that t must be <1000 and N must be >=-1000 and <=1000.

Shouldn't the program check for violation of these conditions?

@Soubhik This is not programming!

This is the right hand reaching for a knife to fight off an alligator, while your left hand is is running a chemical analysis of the water in the swamp.

It's nuts, but it can be fun, and some of the obfuscated code is A) hilarious, and B) very creative.

go to http://www0.us.ioccc.org/main.html
and get some of their past champs - they're a laugh riot and a creative marvel. But they're sure NOT any kind of serious code. No way. ;)

1)If brk() function is used in place of return 0, it will reduce some characters.
But I dont know how to use brk() function.

Does it even need a return? I ran the code that I posted and it works fine. Just a bunch of warnings.

@sfuo: Dear your code is giving wrong answer.
answer will be the summation of only positive numbers.

and return 0 is necessary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.