How to start C programming

C is a programming language for hardware, OS, embedded devices, and influenced various modern programming languages including C++, C# and Java. I finally got the opportunity to learn C language so I would like to summarize about C.


What is C language

  1. ・ Required compiling
  2. ・ Needed memory management
  3. ・ Fast processing speed

Although the grammar is not complicated, the number of descriptions tend to increase because you have to think and write codes by your own since few functions are prepared compared to other famous languages. Moreover, I felt that management of memory and designation of pointer also make beginners feel difficult to learn C programming.


Development environment (Mac)

Compiling is necessary, but development and execution can be easily done on Terminal. If you are not familiar with command line operations, you can also use Xcode as both an editor and a compiler. If you are a Windows user, Microsoft Visual Studio is one of the options but I will explain about only Mac because I’m a Mac user.

▼ Terminal + Vim

1. Write the code
Make a file named main.c with an editor such as vim. Write the following code and save it.


#include <stdio.h>
int main() {
    printf("Hello, World\n");
    return 0;
}

2. Create an executable file
Type gcc on the Terminal app, followed by a space and the file name you want to compile. If it succeeds, a file named a.out should be created in the same folder.


$ gcc main.c

If there are linked files such as header files, compile them together at one time.


$ gcc header.c main.c

Or, if you want to specify the compiled file name, use -o and then specify the name. In the following case, a file named main without extension should be created.


$ gcc -o main main.c

3. Execute the file
Type ./ with the generated file name without space between them and press the enter key. If it succeeds Hello World will be displayed.


$ ./a.out

 

▼ Xcode

1. Create a new project
Create a new command line project with the file > New > Project > macOS > command line tool (Shortcut key is Shift + Command + N) and select C as the language.

2. Execute the file
After writing some code in main.c file, you can execute the file by pressing the “▶” button in the upper left corner. The shortcut key is command + R.

 


Main grammar

I think there is little confusion because the way of writing C code has much in common with other famous programming languages. Just like Java, the definition of variables and arrays requires data type specification. The major difference from modern languages is that the necessity of pointers when using variables because of memory management, no methods to check the number of arrays, and no provided convenient function to handle character strings.

▼ Variables

variableType variableName = value;


int a = 3;

▼ Arrays

arrayType arrayName [] = {value, value, value, …, value};


int a = {3, 1, 4, 5};

▼Display values

printf (characterFormat, value);
note: %d is a format to print int type. %c for char, %ld for float, %p for pointer, etc.


printf("%d", 1 + 2);


3

▼ Conditionals

if (conditionalExpression1) {
Process when the conditional expression 1 is satisfied
} else if (conditionalExpression2) {
Process when the conditional expression 2 holds
} else {
Process when neither conditional expression 1 nor 2 holds
}


int a = 3;
if (a == 1) {
    printf("a is 1.");
} else if (a == 2) {
    printf("a is 2.");
} else {
    printf("a is neither 1 nor 2.");
}


a is neither 1 nor 2.

▼ Roops

for (int i = 0; i < repeatCount; i++) {
Process wanted to repeat
}


for (int i = 0; i &lt; 10; i++) {
    printf("%d", i);
}


0123456789

▼ Comments

Comments are ignored in the actual code.


// 1 line comment

/*
Multi-line comment
*/

▼ Escape sequences

If you want to use line breaks in print, use \n. Use \t for tabs and%% for%.


printf("Hello\nWorld");


Hello
World

 


My opinion

I thought it is no problem for beginners to learn basic arithmetic operations, conditional branches and how to write comments, or for people who know other programming languages to learn basic things about C language. But when you try to make something complicated, you will have to write the process by yourself and output will not be GUI, thus it may be hard to realize accomplishments. but I think it is not recommended for novice programmers to learn in earnest… However, since C is the original programming language of the mainstream languages now, the understanding of other languages will deepen once you learn C.


References (Japanese)

 

Leave a Reply

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

CAPTCHA