Author Topic: C Lessons  (Read 12948 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Re: C Lessons
« Reply #15 on: August 13, 2011, 09:17:09 AM »

Thanks Peter,
I see you have to cast the p pointer when using with malloc.

Charles

JRS

  • Guest
Re: C Lessons
« Reply #16 on: August 13, 2011, 11:59:48 PM »
It would be nice if a C compiler could deal with type at time of use like SB does. Declaring variables and casting is a PITA. I won't even get started on having to DIM arrays. (PITA-2)  :P



Charles Pegge

  • Guest
Re: C Lessons
« Reply #17 on: August 14, 2011, 12:35:15 AM »

You can do this to some extent in C with macros, pointers and casting. Performance and flexibility are a tradeoff.

Charles

JRS

  • Guest
Re: C Lessons
« Reply #18 on: August 14, 2011, 12:46:10 AM »
I really think you have to look at the application as a whole. There will be parts that most of the time the application is idle and there are parts that would benefit from a call to an extension module function written in C.


Charles Pegge

  • Guest
Re: C Lessons
« Reply #19 on: August 30, 2011, 09:03:12 PM »

Bstrings and some BASIC string functions for C

Code: C
  1.  
  2.  
  3. //=================
  4. //Bstring Functions
  5. //=================
  6.  
  7.  
  8.   #include <stdio.h>
  9.   #include <stdlib.h>
  10.  
  11.   typedef char * bstring;
  12.  
  13.  
  14.  
  15. //------------------------
  16.   bstring NewString(int n)
  17. //========================
  18.   {
  19.   int *p;
  20.   p = (int*) malloc (n+6);
  21.   *p = n;
  22.   p++;
  23.   return (char*) p;
  24.   }
  25.  
  26.  
  27.  
  28. //------------------
  29.   int Len(bstring s)
  30. //==================
  31.   {
  32.   int * p=(int*) s;
  33.   p--;
  34.   return *p;
  35.   }
  36.  
  37.  
  38.  
  39. //----------------------------------------
  40.   void FillString(bstring s, int n, int c)
  41. //========================================
  42.   {
  43.   int i;
  44.   for (i=0 ; i<n ; i++)
  45.   {
  46.   *s = (char) c;
  47.   s++;
  48.   }
  49.   }
  50.  
  51.  
  52.  
  53. //-------------------------
  54.   bstring NullString(int n)
  55. //=========================
  56.   {
  57.   int *p;
  58.   p = (int*) malloc (n+6);
  59.   *p = n;
  60.   p++;
  61.   FillString((bstring) p,n+2,0);
  62.   return (bstring) p;
  63.   }
  64.  
  65.  
  66.  
  67.  
  68. //-------------------------
  69.   int FreeString(bstring s)
  70. //=========================
  71.   {
  72.   free(s-4);
  73.   return 0;
  74.   }
  75.  
  76.  
  77.  
  78.  
  79. //------------------------------------
  80.   bstring Mid(bstring s, int i, int l)
  81. //====================================
  82.   {
  83.   bstring t;
  84.   bstring u;
  85.   int le;
  86.   int les=Len(s);
  87.   int j;
  88.   if (i<0)
  89.   {
  90.     i=les+1+i; //offset from the right
  91.   }
  92.   le=l+i-1;
  93.   if (le>les)
  94.   {
  95.    l=les-i+1;
  96.   }
  97.   if (l<0) l=0;
  98.   t=NullString(l);
  99.   if (i>les) return t;
  100.   u=t;
  101.   s+=i-1;
  102.   for (j=0 ; j<=l ; j++) // includes boundary null
  103.   {
  104.     *u++ = *s++;
  105.   }
  106.   return t;
  107.   }
  108.  
  109.  
  110.  
  111. //------------------------------
  112.   bstring Left(bstring s, int i)
  113. //==============================
  114.   {
  115.   return Mid(s,1,i);
  116.   }
  117.  
  118.  
  119.  
  120. //-------------------------------
  121.   bstring Right(bstring s, int i)
  122. //===============================
  123.   {
  124.   return Mid(s,-i,Len(s));
  125.   }
  126.  
  127.  
  128.  
  129.  
  130. //------------------
  131.   bstring Chr(int i)
  132. //==================
  133.   {
  134.   bstring s=NewString(1);
  135.   *s=(char) i;
  136.   s[1]=(char) 0;
  137.   return s;
  138.  
  139.   }
  140.  
  141.  
  142.  
  143.  
  144. //--------------------------
  145.   short Asc(bstring s,int i)
  146. //==========================
  147.   {
  148.   long le=Len(s);
  149.   if (i<0)
  150.   {
  151.    i=le+i+1; //offset from the right
  152.   }
  153.   if (i<1) return 0;
  154.   if (i>le) return 0;
  155.   i--;
  156.   return (short) s[i] & 0xff;
  157.  
  158.   }  
  159.  
  160.  
  161.  
  162. //-----------
  163.   int main ()
  164. //===========
  165.   {
  166.   int i,n;
  167.   bstring s,t;
  168.  
  169.   printf ("How long do you want the string? ");
  170.   scanf ("%d", &i);
  171.  
  172.   s=NullString(i);
  173.   t=NullString(i);
  174.   if (s==NULL) exit (1);
  175.  
  176.   for (n=0; n<i; n++)
  177.     s[n]=rand()%26+'a';
  178.   s[i]='\0';
  179.  
  180.   t=Mid(s,-2,64);
  181.   printf ("Random string: %s, %s, %i, %s \n",s, t, Asc(t,2), Chr(65) );
  182.   FreeString (s);
  183.   FreeString (t);
  184.  
  185.  
  186.   return 0;
  187.   };
  188.  
  189.  

Charles

Peter

  • Guest
Re: C Lessons
« Reply #20 on: August 31, 2011, 02:42:15 AM »
Hi Charles,

I think the people are visiting this forum, because they are thinking that is a  Basic Forum.
And now they get a shock, if they see is much  stone age C here. It does not matter to me, I am crazy enough to write in all..
I think it will become  more and more Linux than Basic ? 

Some OxygenBasic  examples would be good  to this forum. You can get the feeling,  yes  I am here right. 

Charles Pegge

  • Guest
Re: C Lessons
« Reply #21 on: August 31, 2011, 08:21:14 AM »

Hi Peter,

This dinosaur porridge will eventually become part of my Trans-platform resources. :) and it helps me appraise the possibility of a C emitter.

However I have some more Opengl geometry in the pipeline, based on the Platonic solids and their variations. I hope these will be easier to relate to.

Charles

JRS

  • Guest
Re: C Lessons
« Reply #22 on: August 31, 2011, 08:35:14 AM »
I think C is the foundation of most OS and development tools we use today. Everything is a derivative of C IMHO. Linux is the future as MS waffles along trying to capture market share and caring less what effect if has on their developer base.

If this forum turns into a thinBasic like venue, then we have a problem. Only 10% of the posts there have anything to do with Basic.

Peter

  • Guest
Re: C Lessons
« Reply #23 on: August 31, 2011, 09:20:29 AM »
Only 10% of the posts there have anything to do with Basic.

yes, that is correct, is more for the hobby social philosopher.  :D

Peter

  • Guest
Re: C Lessons
« Reply #24 on: August 31, 2011, 09:45:52 AM »
did you know, if you learn Java, then can you talk with the Aliens!   :D

efgee

  • Guest
Re: C Lessons
« Reply #25 on: August 31, 2011, 11:01:11 AM »
I really welcome Charles' exploration in C as I like to use/learn as much as I can.
(every programming language is welcome...)
This is because I haven't found THE perfect language yet.

Aurel

  • Guest
Re: C Lessons
« Reply #26 on: August 31, 2011, 11:24:06 AM »
Code: [Select]
did you know, if you learn Java, then can you talk with the Aliens! :D :D :D
But hey maby you wrong - you know my uffo basicers code - it's infact some sort
of Alien language ,man.... :D :D :D

And yes ,this time i 100% agree with Pan  ;)
I know that knowlege about C is usefull ...but
And heh ...yes John is right to about thinF - to much dan babeling about
lisp and derivatives... :-\

Charles Pegge

  • Guest
Re: C Lessons
« Reply #27 on: August 03, 2012, 08:02:10 AM »
GCC Windows (MinGW), Late binding, Midi Output (Tune with 2 notes :))

Code: C
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <windows.h>
  7.  
  8.  
  9. int main()
  10. {
  11.   HANDLE li,ha;
  12.   li=LoadLibrary("Winmm.dll");
  13.   //
  14.   int WINAPI (*midiOutOpen)(HANDLE*h,UINT,DWORD_PTR,DWORD_PTR,DWORD);
  15.   midiOutOpen=GetProcAddress(li,"midiOutOpen");
  16.   //
  17.   int WINAPI (*midiOutClose)(HANDLE);
  18.   midiOutClose=GetProcAddress(li,"midiOutClose");
  19.   //
  20.   int WINAPI (*midiOutShortMsg)(HMIDIOUT,DWORD);
  21.   midiOutShortMsg=GetProcAddress(li,"midiOutShortMsg");
  22.   //
  23.   //FARPROC CALLBACK
  24.   HANDLE        hmo;
  25.   int           er;
  26.   long          ch;
  27.   long long     qu1,qu2,fr;
  28.   //
  29.   QueryPerformanceCounter( (LARGE_INTEGER*) &qu1);
  30.   QueryPerformanceFrequency((LARGE_INTEGER*) &fr);
  31.   fr/=1000; // for milliseconds
  32.   //
  33.   er=midiOutOpen(&hmo,0,0,0,0);
  34.   midiOutShortMsg(hmo,0x000EC2); // instrument assign ch
  35.   midiOutShortMsg(hmo,0x503791); // v n (n on)  ch
  36.   Sleep(250);
  37.   midiOutShortMsg(hmo,0x503781); // v n (n off) ch
  38.   midiOutShortMsg(hmo,0x503191); // v n (n on)  ch
  39.   Sleep(3500);
  40.   midiOutShortMsg(hmo,0x503081); // v n (n off) ch
  41.   //
  42.   //MessageBox(0,"Hello","Greeting",0);
  43.   //
  44.   //scanf("%c",&ch);
  45.   //
  46.   QueryPerformanceCounter((LARGE_INTEGER*) &qu2);
  47.   long long ti=(qu2-qu1)/fr;
  48.   //printf("%i %i",ha,frd);
  49.   midiOutClose(hmo);
  50.   //
  51.   FreeLibrary(li);
  52. };
  53.  

Charles
« Last Edit: August 03, 2012, 08:23:49 AM by Charles Pegge »