Introduction to C programming language

10 September 2024

Author - @Bibhabendu Mukherjee

Let's deep dive into the history of c and how it becomes the heart of most of the complex system.

C (programming language)

C is general-purpose programming language.It was created in the 1970s by Dennis Ritchie. C is clearly designed to target the CPU. It has found lasting use in operation systems code, drivers, and protocol stacks, C is commonly used on computer architectures , smallest microcontrollers and embedded systems.

C is a imperative procedural language, supporting structured programming , lexical variable scope, with static type system

  • Imperative Language --> "Focus on actions", C programming emphasizes commands and statements that directly manipulate data.
  • Procedural --> C programs are structured into functions, which are self-contained blocks of code that perform specific tasks.
  • Structured programming --> C uses constructs like if-else, switch, for, while, and do-while to control the execution of code blocks.
  • Lexical variable scope --> The scope of a variable defines where it can be accessed within a program.
  • Recursion --> A function can call itself directly or indirectly, leading to recursive solutions to problems.
  • Static type system --> C requires that the data type of variables and expressions be declared explicitly.

It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support,Since 2000, C has consistently ranked among the top three languages in the TIOBE index,

Overview

c is a semicolons terminate statement , while curly braces are used
to group statements into blocks.

--> The C language has small , fixed number of keywords, including a full set of control-flow primitives : if/else, for , do/while, while, and switch.

--> It has a large number of arithmetic, bitwise, and logic operators: +, +=, ++, &, ||, etc.

--> Data typing is static, but weakly enforced; all data has a type, but implicit conversions are possible.

--> User-defined (typedef) and compound types are possible.

  • Heterogeneous aggregate data types (struct) allow related data elements to be accessed and assigned as a unit.
  • Union is a structure with overlapping members; it allows multiple data types to share the same memory location.
  • Array allows contiguous memory location to store data (int,char,etc).
  • Strings are not distinct data types but conventionally implemented as a
    character array with null-terminator ('/0')

Important Highlight

Many languages are closely borrowed from C, like C++, C#, Java, Unix's C shell , D , Go, these language have inherits some of the control structured and other basic features from C.

History

The origin of C is closely tied to the development of the Unix operating system, it was originally implemented in assembly language on PDP-7 by Dennis Ritchie and ken Thompson.

In 1971 Ritchie started to improve B Language to provide some more features. One significant addition was character datatypes. B language became NB (New B) started to use as a new unix kernel language

At Version 4 Unix , released in November 1973, the Unix kernel was extensively re-implemented in C and this time C implemented some
strong concept like struct

Unix was one of the first operating system kernels implemented in a language other than assembly. Earlier instances include the Multics (time-sharing operating system based on the concept of a single-level memory.) system (which was written in PL/I) and Master Control Program (MCP) for the Burroughs B5000 (which was written in ALGOL) in 1961. In around 1977, Ritchie and Stephen C. Johnson made further changes to the language to facilitate portability of the Unix operating system. Johnson's Portable C Compiler served as the basis for several implementations of C on new platforms.

C standard

The C standard refers to the formalized specification of the C programming language set by the International Organization for Standardization (ISO). It defines the syntax, semantics, and library functions for C. The most common standard is C11, although C99 and C18 are also widely used in various projects for compatibility and modern features.

Reserved words

C89 has 32 reserved words, also known as keywords, ex: auto, extern, char, int
void, return etc then C99 reserved 5 more words total 37 ex: inline , restrict. Then C11 reserved 7 more words in total 44 ex: _Generic , _Noreturn. C23 reserved 15 new words in total 59 ex: alignas , nullptr etc

Operators

C supports a rich set of operators, which are symbols used within an expression to specify the manipulations to be performed while evaluating that expression. C has operators for:

  • arithmetic: +, -, *, /, %
  • assignment: =
  • augmented assignment: +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
  • bitwise logic: ~, &, |, ^
  • bitwise shifts: <<, >>
  • Boolean logic: !, &&, ||
  • conditional evaluation: ?  :
  • equality testing: ==, !=
  • calling functions: ( )
  • increment and decrement: ++, --
  • member selection: ., ->
  • object size: sizeof
  • type: typeof
  • order relations: <, <=, >, >=
  • reference and dereference: &, *, [ ]
  • sequencing: ,
  • subexpression grouping: ( )
  • type conversion: (typename)

"Hello, world" program

# include <stdio.h>

int main(void)
{
    printf("hello, world\n");
}

Data Types

In C, data types define the type and size of data that variables can hold. They are categorized as:

Basic Types:

  • int: Integer values.
  • float: Single-precision floating-point numbers.
  • double: Double-precision floating-point numbers.
  • char: Single character.

Derived Types:

  • Pointers, Arrays, Functions.

Enumeration Types: Defined using enum.

Void Type: Represents the absence of a value.

User-defined Types: Structures (struct), Unions (union).