Want to learn C language? Its Structure and basic introduction6 min read

C language is worth learning because it is a base of all languages.  C language is everywhere, your mobile OS is written in C, 99.99% embedded system’s code has written in C, all operating system is written in C. Do you know Why? Surprisingly, it is very powerful language. This tutorial is a basic introduction of C language.  Also, this article contains so many helpful questions with answers.

इस लेख को हिंदी में पढ़ने के लिए यहां क्लिक करें।

C language basics

C language is a fundamental language in the process of learning programming. The greatest thing is C is a small language, consisting of only 32 English words referred to as keywords, easy to remember.

Just think the device you are using now, possibly OS has written in C. (most Operating Systems are written in C) Just think its POWER.

At the starting of the programming era, languages are designed for some specific purpose. for instance, FORTRAN (Formula Translator) mainly utilized in the scientific applications, COBOL ( Common Business Oriented Language ) for business applications. From here the primary step towards C was put forward by Dennis Ritchie.

As nicely said that,

“Necessity is a mother of inventions”.

C language was developed in 1970 at Bell laboratories by Dennis Ritchie. Initially, it had been designed for programming in UNIX but later the entire UNIX OS is rewritten using C language.


How to learn the C language?

  • Learning isn’t a 1-day process, it requires our dedication and punctuality.
  • To urge mastery in anything it depends upon where you’re spending most of your time in a day.
  • I suggest you study one article per day from this website and complete the assignments.
  • Do programming for a minimum of 3 hr.
  • If you’re having any query I surly give an on-point reply as soon as possible.
  • Most noteworthy, C is a very very easy language.

Whats a secret behind the name: C language? 

  • The C language was derived from B language which was written by Ken Thompson at AT&T Bell laboratories.
  • The B language was adopted from a language called BCPL(Basic Combined Programming Language), which was developed by Martin Richards at Cambridge University.
  • lastly, as we look at our English alphabets after B, character C is coming that’s why the name is C language.

Structure of a C language

C is a structure-based programing language. Beneath image displays its components very well.

C language basic code structure including command so easy to understand

— — — — —

Comments

Comments play an important role in the code, It makes code analysis easy, Error debugging easy, and if you are sharing your written code to another user then it is very easy for them to understand.

KEEP THIS THING IN MIND.

Comment Comment Comment   |    EVERY TIME, EVERY LINE, EVERYWHERE.

There are two types of comments.

1)  Single line comments.

// your comment

2)  Multiline comments.

/* your comment */

— — — — —

Preprocessor directives

Preprocessor directives tell to the compiler just preprocess this chunk of code before the compilation. Preprocessor directives always start with ‘#’.

Always write preprocessor directives on the starting of a file. That’s not a rule but some compilers are having some inbuilt rules of compilation so for good practice always keep your preprocessor directives on the starting of your code.

Examples:

#include

#define

#pragma

— — — — —

Global variables and local variables

Those variables are declared or defined outside of the main function are known as a global variable. This means it is visible throughout the program.

Those variables are declared inside the block are known as a local variable. This means it is visible to that block only.

— — — — —

Function declaration and Function defination

It is not necessary to write function under the main function but it is general practice to write function definition under main.

only those chunk of code can use that function, written below of function, otherwise, it will generate an error in the compilation stage.

To overcome this issue just declare that function in starting. In compilation, the compiler will think ohh! It is already defined somewhere below and then it will look for it.


My recommendations

 

q? encoding=UTF8&MarketPlace=IN&ASIN=8183330487&ServiceVersion=20070822&ID=AsinImage&WS=1&Format= SL250 &tag=dwijmistry 21ir?t=dwijmistry 21&l=am2&o=31&a=8183330487

–  It is the best book among all the C programming books available.

–  It proves to be very useful for a beginner as well as advanced programmers.

–  I suggest that you must have to read this book and go through all the topics one by one on this website, as the name suggests it covered all the concepts in great depth.

–  It is worth buying it. It is as good as of Dennis Ritchie, leaving no stone unturned.

 

 

 


Why C Language used in Embedded?

  1. As explained earlier, C is neither low-level nor high-level language. You can closely work with the registers of the CPU. You can embed assembly code in your C program.
  2. Embedded Systems in which the processor might be a Microprocessor, Microcontroller, DSPs, DIPs, and even FPGAs can be programmed with C.
  3. Also, there is SystemC (a variant of C) which can replace Hardware Description Languages like VHDL and Verilog.
  4. C can utilize the Hardware to its maximum by multiprocessing and multithreading APIs provided by POSIX, VxWorks.,
  5. Bare metal (No OS) programming is possible with C for given computer architecture. This bare-metal programming is possible with the help of Cross-compilation for a particular architecture. For Ex: Arduino and other development boards. The boot-loader is another part of code depending on the particular architecture which again can be coded in C or assembly.

Why C++ is not used in embedded system programming instead of C language?

C++ is another competitive language that can replace C in embedded. But the issue with C++ is that it is relatively complex for programmers to use efficiently and the code memory is relatively large.

Sometimes, a C++ program can be written efficiently faster than a C program.

Other popular languages like Python and Java use Interpreters which occupy part of the memory for Java Virtual Machine and Python engine. They are relatively very slow(10x and 20x slower for most algorithms). There are Just-in-Time (JIT) compilers available for Python and Java, but they are used less.

The ARM architecture has Jazelle technology which directly executes the Java bytecode. It is proprietary to ARM and is used in some modern smartphones.


Role of C language in RTOS

C is very fast. Most RTOS (Real-Time Operating Systems) that are prevalent are coded with C. FYI: Alongside your phone’s OS like IOS and Android, there is a second OS for the baseband processor (phone calls and network operation) which is an RTOS. There are several RTOSs like VxWorks, ThreadX, QNX., etc.

Last but not the least, Most servers and databases are optimized using C and C++. Although they aren’t much used in embedded systems.

The possibilities are endless with C.


Assignment

  1. Setup the environment. As given in my previous post.
  2. Learn about VIM
  3. Practice Linux commands and vim commands  |  Just play with it.

 

 

 

Leave a Comment