Programming in C for pets: Structure of the program
2026-04-01
Let's take a closer look at the example
#include <stdio.h>
int main(int argc, char* argv[])
{
puts("Meow, she-master!");
return 0;
}The basic part of any C program is function. If we have no functions we have nothing to run, so any program has at least one function.
The execution begins from a function named main.
This function is invoked with command line arguments and must return an integer value. Zero on success and nonzero on error. This is important is someone wants to use our program in their scripts.
It's pretty clear that int means integer. In our program we can see it twice:
right before main where it defines the type of return value, and before argc,
where it defines the type of the argument.
argc by itself is the name of the argument. It can be arbitrary, as well as argv.
You could write:
int main(int number_of_parameters, char* parameters[])But it's better to use generally accepted argc and argv.
The language allows simplified declarations of main, for example:
int main()or even
void main()In the latter case the function does not need return 0; statement.
But the best way to declare main is as in the initial example because:
- arguments are necessary in most cases
- a program with
void mainreturns undefined value which means such a program is not good for scripting.
The function body is enclosed in curly brackets and composed of statements, In our example it consists of two statements only which are executed sequentially from top to bottom.
The first statement prints the string (strictly speaking, it writes it to the standard output stream which is displayed on the screen by the operating system). The second statement forces the function to finish and return zero.
Unlike Python with its mandatory indentations, C does not impose any formatting constraints and true artists are absolutely free in their creativity. A program may look like this, for example:
#define A write
extern long A();extern double
O[];int a,b,c ,d,v,E=0;extern
int nanosleep(); extern int gettimeofday();
void w(char*_,long __){A(1,_,__);}unsigned char r(){v^=8;return
++v%8;}int L(int _){return((char*)&O[_])[r()];}void P(int
_){w(((char*)(O+14))+ _*2,2);}void S(int _){w(((char*)(O+
16))+_*3,3);}int Q( int _,int __){return
_/(*((char*)(O+18)+__))%10+48;}void u(){char _[16];gettimeofday(_,0l);v=
_[0];}void g(){unsigned long _;ioctl(1,TIOCGWINSZ,&_);b=_&65535;a=_>>16&65535;
c=a/2-12;d= b-4;}void e() {w("\033[",2);}void i (){e();
w("1m",2);} void j() {e();w( "0m",2);}void k(){e();w("?1049h",6);e();
w("2J",2);e();w("H",1);}void l(int _,int __){e();w((char []){Q(_
,2),Q(_,1), Q(_,0)},3);w(";", 1);_=__;w((char[]){Q(_,2),Q(_,1),
Q(_,0)},3);w("H",1);}void m(int _){e();P(_);w("m",1);}void n(){for(int _=0;_<
18;_++){l(d+ _/6,c+(_%6)*4);m(((char*)&O)[E++]);w(((char*)&O)+(_
<<2)+24,4);}}void o(){char _[16]={0};_[11]=1;nanosleep(_,0l);}void q(int _)
{if(_<3)_=2;int x[]={3,0,1,2,0,1};if( r()<2){i(); m(x[_-2]);}else
m(x[_+1]);}void p(int _,int __,int
x,int*y,int* z){switch (_){case 0:if(x<=2||__<4){*y=L(12);*z=0;}else
if(x<15){*y=L(12);*z=-(x%2);}else{*y=L(12);*z=L(12)
/2;}break;case 3:*y=L(12)*1.5;*z=L(13);break;case 4:*y=L(12)/
2;*z=L(12)/2;break;default:*y=L(13)*1.5*(1-(_-1)*2);*z=L(13)/2;
break;}} void t(int _,int __,int x,int y){int A,B,C,D = 5;
while(y-->0) {C=35-y;p(x,y ,C,&A,&B);
if(B>0&&__ >d-2)B--;if((y<3
)||(x<3&& y<10))t(_,__,4,
y);else if(x==0&&y%3==0)
{if(D<=0){D=3;t(_,__,(E++
%2)+1,y+5);}}D--;_+=A;__
+=B;if(_<0)_=0;if(__
<0)__=0 ;l(__,_);
q(x);S(x) ;j();o();
}}int main() {g();u();
k();n();t(c+10,d-1,0,35);
l(b,0);}double O[]={
2.5673396845218159e-289,
2.5673475286552589e-289, 5.0789948392480145e-321, 4.4759385888332619e-091,
2.2662500784516041e-085, 6.0135556610268572e-154, 6.0138114327429894e-154,
6.0134700169990685e-154, 6.0134703504179883e-154, 2.5663272049433059e+151,
2.5673651826636406e+151, 6.0134700183229171e-154, 5.3767695968532491e-299,
7.2911289729117876e-304, 6.1257806499948415e-062, 4.6555645993480313e+025,
6.5632629331317560e+299, 7.9070799242303223e-101, 3.2391739187041928e-317};However, true art is always undervalued so it's better to stick to the simple techno style, understandable by masses. The essense of programming is neither algorithms nor languages. It's the ability to bring your ideas to other programmers without racking their brains.
That's why humans developed style guides. Different projects have their own guides. For example, in Linux https://docs.kernel.org/process/coding-style.html they prefer tabs, and in Python (yes, Python is written in C) they use spaces https://peps.python.org/pep-0007. But both styles have one common point: the opening curly bracket must be on the next line. Not on the same line as in Go:
func main() {
}In Go this style is nailed, and the opening bracket cannot be on the next line at all. That's strange, given that Go was written by C guys.
But why the opening bracket should be on the new line? Because if visually separates function body from its declaration, while keeping the whole code block solid. Let's compare three variants:
int main(int argc, char* argv[]) {
puts("Meow, she-master!");
return 0;
}
int main(int argc, char* argv[]) {
puts("Meow, she-master!");
return 0;
}
int main(int argc, char* argv[])
{
puts("Meow, she-master!");
return 0;
}The first snippet looks messy. The second one looks better, but the empty line breaks it into two parts. The third looks ideally, doesn't it?
Aesthetic in programming is often more important than algorithms.
Let's finish with our example.
The very first line #include <stdio.h> is a preprocessor directive.
It instructs the compiler to insert the content of stdio.h file into our program.
That file is somewhere in the standard library.
The compiler knows where exactly, but we can do without that knowledge for now.
Yes, that's how it's done in C: one file gets inserted to another. No modules as in other languages. Everything is plain and simple.
The preprocessor is a part of compiler, but it can be run standalone if we specify command line argument -E:
cc -E myprogram.cTry and get stunned how much it pukes out. But sometimes this helps to locate mistakes.
But why do we need this stdio.h at all?
Because puts is declared there and without that declaration the compiler fails.
Meow.
Pet has limited explanatory capabilities and might miss something. Litte kitties may ask questions on Mastodon and pet will improve this article.