Adventures in C2 min read

I’ve been working on quite a few languages lately – Python, brushing up on C#, brushing up on PHP, and I’ve decided to also jump into C. I know – learning this many at once is crazy. Really you should go one language at a time, but I’ve hit a blocker with Python (nothing to really script), nothing to really do with C#, and slowly working on some projects with PHP.

So why C?

This is a question I C coming (see what I did there?). Well, why not? It’s very practical because it’s cross platform. That’s one of the big things for me since I’ve made the switch primarily to macOS. The other main reason is that I’ve had the most experience in the C family tree. As I previously mentioned, I’ve had some good knowledge and experience with PHP learning it in high school on my own creating some of my first websites with it. Because of that, it allowed me to move into C# fairly easily. So I figured I’d move right up to the top with C.

Most Windows applications that I know of are written in C# and the cross-platform software I use like Firefox is written in C++.

Early notes

I can tell right off the bat that C is going to be pretty easy for me as it’s very close to what I’ve been working with. So for example in C#:

String  name = "Travis";
Console.WriteLine("Hey! My name is {0}. Nice to meet you!", name);

is the following in C:

char name[] = "Travis";
printf("Hey! My name is %s. Nice to meet you!", name);

and is the following in PHP:

$name = "Travis";
echo("Hey! My name is " . $name . ". Nice to meet you!");

As you can see, they’re very similar with just a few minor syntax changes.

Just a test

One of the examples in the book I’m using has the following code to output the size of the different numeric variable types in bytes.

/* sizeof.c--Program to tell the size of the C variable */
/*            type in bytes */

#include <stdio.h>

int main(void)
{
  printf("\nA char	is %d bytes", sizeof(char));
  printf("\nAn int	is %d bytes", sizeof(int));
  printf("\nA short	is %d bytes", sizeof(short));
  printf("\nA long	is %d bytes", sizeof(long));
  printf("\nA long long is %d bytes", sizeof(long long));
  printf("\nAn unsigned char is %d bytes", sizeof(unsigned char));
  printf("\nAn unsigned int is %d bytes", sizeof(unsigned int));
  printf("\nAn unsigned long int is %d bytes", sizeof(unsigned long));
  printf("\nAn unsigned long long is %d bytes", sizeof(unsigned long long));
  printf("\nA float is %d bytes", sizeof(float));
  printf("\nA double is %d bytes", sizeof(double));
  printf("\nA long double is %d bytes", sizeof(long double));

  return 0;
}

And this outputs:

Anyways, I’ve done my hour for the day. More to follow.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.