by Joel Limardo
Home | Twitter | LinkedIn | Consulting | About | RSS | Map
Areas:
Process
 
Technical
 
Projects:


Thu, 02 Feb 2017

C-Repl

This is a tool you can get using:

 sudo apt-get install c-repl

It has a really small footprint but it is a finnicky tool that is a bit of fun to play with. For instance, you can normally just do the following to test out making different types of arrays in C (store this in a file called simplearrays.c):



#include <stdio.h>
int main() {

char sentence1[] = "This is a sentence!\n";
  int m[10] = {1,2,3,4,5,6,7,8};
  int i;
  for (i = 0; i<=9; i++) {
      printf("This is the element: %d\n", m[i]);
  }
  puts(sentence1);
  return 0;
}

And to compile just do this on the command line:


     make -f simplearrays
     ./simplearrays

Should produce:


This is the element: 1
This is the element: 2
This is the element: 3
This is the element: 4
This is the element: 5
This is the element: 6
This is the element: 7
This is the element: 8
This is the element: 0
This is the element: 0
This is a sentence!

Now, in c-repl when you try to directly create an array in that fashion the thing crashes:


/usr/lib/c-repl/codesnippet.rb:19:in with_semi': undefined method+' for nil:NilClass (NoMethodError)
    from /usr/lib/c-repl/codesnippet.rb:46:in parse'
    from /usr/lib/c-repl/codesnippet.rb:14:ininitialize'
    from /usr/lib/c-repl/codesnippet.rb:57:in new'
    from /usr/lib/c-repl/codesnippet.rb:57:inparse'
    from /usr/bin/c-repl:161:in c_command'
    from /usr/bin/c-repl:196:inblock in inputloop'
    from /usr/bin/c-repl:184:in loop'
    from /usr/bin/c-repl:184:ininputloop'
    from /usr/bin/c-repl:203:in `
'

Bamph. But you can get around this by including a header and using a simple trick:



c-repl
.h string.h
char foo[];
<stdin>:3:6: warning: array ‘foo’ assumed to have one element [enabled by default]
strcpy(foo,"This is a string");
puts(foo);
This is a string

To see what c-repl is doing use the .d flag to toggle debugging:


.d
debug is on
.t

#include <stdio.h>
#include "string.h"

extern char foo[];
void dl4() {
  #line 1
  printf("repl is ok\n");
}
gcc -xc -g -fPIC -shared -o /tmp/c-repl.uZqS0iwFXV/dl4.so  -
repl is ok

As you can see, c-repl creates a shared library in the /tmp/c-repl.uZqS0iwFXV/ directory for every instruction, increments the name of the routine (dl1...N), includes some variables previously used during the session, and then runs it.

Whenever you see c-repl in examples in this section you'll know what it is referring to.



posted at: 21:36 | path: /technical/C | this entry | top


Wed, 18 Jan 2017

Learn C

Learning C can be a pain because virtually every resource tries to dumb it down. You then think you know the language, crack open a program, and are almost instantly lost. Here I'm attempting to show techniques actually used in real programs.



posted at: 22:55 | path: /technical/C | this entry | top

Linking Headers

Headers can get big. The bigger they are the more you will want to separate the code portion from the method declarations. You can do this is C by putting the code in a file with a C extension and the declarations in a header file with the normal header extension.

Here is an example:

There are several ways to compile this and link it. I just used the easiest one for now.



posted at: 22:26 | path: /technical/C | this entry | top


Sun, 08 Jan 2017

Simple Class Example

The simple clock example exemplifies how to write up classes and bundle them into header files that can be used later on. Then you can then simply use a make file to build (not stricly necessary but you want to get into the habit): simple make file.



posted at: 15:11 | path: /technical/CPP | this entry | top

C++ Projects

  1. Simple Object


posted at: 14:11 | path: /technical/CPP | this entry | top


Fri, 30 Sep 2016

Simple Input

The code sample shows how to write to and input a simple numeric value using scanf and printf. There are things like buffer overflows to think about but at such an early stage you can ignore that stuff for now.



posted at: 14:41 | path: /technical/C | this entry | top


Fri, 16 Sep 2016

Hello World in C


For me Hello World for a language is essential. You want that instant gratification from running something so basic that you really should not be able to screw it up.


#include <stdio.h>
 void main() {
 printf("hi there %s", "American");
 }


posted at: 15:07 | path: /technical/C | this entry | top

© 2015-2017. All Rights Reserved. All original code appearing on this site is MIT Licensed unless otherwise noted.