Similarities and Differences between Ruby and C language

Similarities between Ruby and C

There are many similarities between C and Ruby, and some of them are:

Like C, in Ruby also…

  • A programmer is able to program procedurally if they like to do. But still, behind the scenes, it will be object-oriented.
  • Both the languages have the same operators, for example, compound assignment and bitwise operators. But Ruby doesn’t have ++or – with it like C.
  • Both of them have got __FILE__ and __LINE__with them.
  • There is no special const keyword but still we can have constants.
  • In both C and Ruby, strings are written in double quotes i.e. “ ”.
  • Both of them contains the mutable strings.
  • Using the ri command, most of the docs can be read in your terminal, just like man pages.
  • Same sort of the command-line debugger is available in both of them.
  • Differences between Ruby and C

    Ruby C
    In Ruby, there is no need to compile the code, it can be run directly. In C, compilation of code is necessary because it cannot be run directly.
    It’s require ‘foo’ instead of #include or #include “foo”. Nothing like that is required in C.

    There are no variable declarations in Ruby. Variable declaration is necessary in C.

    In Ruby, there’s no macros or pre-processor, no casts, no pointers, no typedefs, sizeof, nor enums available. Whereas, they are present in C.
    Arguments to methods (i.e. functions) are passed by value, where the values are always object references. In C functions are passed by value as well as passed by reference.
    Parentheses for method (i.e. function) calls are often optional. This is not optional in C.
    There is no char—they are just 1-letter strings. Char is used in C for one character.
    Array literals go in brackets instead of braces in Ruby. Array literals go in braces in C.
    You cannot drop down to assembly. In C you cannot drop down to assembly.
    In Ruby, objects are strongly typed. In C, objects are not strongly typed.
    You go without parentheses for if and while condition expressions. Parentheses are needed in C with if and while expressions.
    Strings don’t end with a null byte in Ruby. While strings ends with a null byte in C.

    If you add two arrays, you get back a new and bigger array (of course, allocated on the heap) instead of doing pointer arithmetic. There is a need of pointer arithmetic in C.

    In Ruby, arrays just automatically get bigger when you stuff more elements into them. In C, automatically array cannot gets bigger.
    All variables live on the heap. Further, you don’t need to free them yourself—the garbage collector takes care of that. In C, we need to free them ourself because garbage collector is not present in C.
    You don’t usually use braces—just end multi-line constructs (like whileloops) with an end keyword. Braces are required because ignoring braces will cause syntax error.
    All the functions and classes are defined in the main source code files as there are no header files present in ruby. Header files are present in C.
    There’s no semicolons ending lines. There is ending lines.
    There’s no #define in ruby. Just use constants. #define is there in C.
    The do keyword is for so-called “blocks”. There’s no “do statement” like in C. Do statement is used in C with while to make a loop like do-while.

    Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.