C Programming Language Tutorial


C Programming language

C Programming Tutorial

C is a general-purpose programming language that has played a pivotal role in shaping modern software development. Known for its efficiency and close-to-hardware capabilities. C programming is an excellent language to learn to program for beginners.

Our easy to understand C programming tutorial will lead you through the process of learning C programming one step at a time.

This C programming language tutorial is designed for beginner programmers, that gives enough understanding on fundamental concepts of C programming language.

This C programming tutorial explains the fundamental concepts in C language like history of C language, identifiers and keywords, data types, storage classes, variables, decision making, functions, control statements, string, structures, preprocessor directives etc with c programs and sample input output.

C language is a general-purpose, imperative popular computer programming language. It supports structured programming, variable scope, recursion, provide low-level access to memory etc. C language become one of the most widely used programming languages of all time.

C programming language is the mother for all programming languages.

C Programming Tutorial Topics


Brief History of C Language

  • C programming language was originally developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs in USA.

  • B was the precursor language of C. B was created by Ken Thompson at Bell Labs and was an interpreted language used in early versions of the UNIX. Over the time, they improved B language and created its logical successor C which is a compiled programming language.

  • The development of C programming language was closely tied to the development of the Unix operating system. Unix kernel was one of the first operating system kernels to be implemented in C.

  • The portability and architecture neutrality of UNIX was the main reason for the initial success of both C and UNIX.

  • In 1978, Dennis Ritchie and Brian Kernighan published the first edition of The C Programming Language.

  • In 1990, the ANSI C standard was adopted by the International Organization for Standardization.

Advantages of C Programming Language

  • Easy to learn
    C is a very easy to learn middle level language for expressing ideas in programming in a way that most people are comfortable with.

  • Structured programming language
    A structured programming language breaks and abstract a program into small logical components which are responsible for performing a specific task. C's main structural components are functions or subroutines. It makes the program easier to understand and modify.

  • Powerful programming language
    C language provides a wide variety of inbuilt data types and ability to create custom data types using structs. It also provides a large set of commonly used Input/Output, Mathematical, String etc, related functions as C standard library. C has a rich set of control statements, arithmetic operators, loops etc which provides a powerful tool for programmer to implement his logic as a C program.

  • Low-level Language Support
    C language is reasonably close to assembly machine. It support features like pointers, bytes and bit level manipulation. C allows the programmer to write directly to memory. C structures, pointers and arrays are designed to structure and manipulate memory in an efficient, machine-independent fashion. It is generally used to create hardware devices, OS, drivers, kernels etc.

  • Produces portable programs
    C language produces portable programs, they can be run on any compiler with little or no modifications. One of the main strengths of C is that it combines universality and portability across various computer architectures.

  • Memory Management
    C language provide support for dynamic memory allocation. In C, we can allocate and free the allocated memory at any time by calling library functions like malloc, calloc and free.

  • Produces efficient programs
    C language is a compiled programming language, which creates fast and efficient executable files. It also provides a set of library functions for common utilities. C provides a lot of inbuilt functions that makes the development fast.


Disadvantages of C Programming Language

  • C Programming Language doesn't support Object Oriented Programming(OOP) features like Inheritance, Encapsulation, Polymorphism etc. It is a procedure oriented language. In C, we have to implement any algorithms as a set of function calls.

  • C doesn't perform Run Time Type Checking. It only does compile time type checking. At run time, C doesn't ensure whether correct data type is used instead it perform automatic type conversion.

  • C does not provides support for namespace like C++. Without Namespace, we cannot declare two variables of same name.

C is a Middle Level Programming Language

C is often called a middle level programming language because it supports the feature of both high level and low level language. C being a mid level language doesn't mean that, it is less powerful or harder to use than any high level language.

C combines the best elements of high level language with the control and flexibility of low-level language(assembly language).

Like assembly language, C provide support for manipulation of bits, bytes and memory pointers at the same time it provides abstraction over hardware access


Uses of C Programming Language

C is one of the most popular languages out there for programming lower level things like operating systems and device drivers, as well as implementing programming languages.

C was originally developed for creating system applications that direct interacts to the hardware devices. Below are some examples of uses of C programming language.

  • Operating System Kernel
  • Programming Language Compilers
  • Assemblers
  • Text Editors and Word processors
  • Print Spoolers
  • Network Drivers
  • Modern Softwares
  • Database and File systems
  • Embedded systems
  • Real Time softwares


Your First C Program

Printing "Hello World" program is one of the simplest programs in C programming languages. It became the traditional first program that many people write while learning a new programming language.

/* Hello world Program in C */ 
#include <stdio.h>

int main(){
    printf("Hello World");
    return 0;
}
Output
Hello World

How to Write and Compile a C Program

  1. Write
    • Open a text editor and type the above mentioned "Hello World" code.
    • Save this file as HelloWorld.c.

  2. Compile
    • Open command prompt and go to your current working directory where you saved your HelloWorld.c file.
    • Compile your code by typing gcc HelloWorld.c in command prompt. Your program will compile successfully, If your program doesn't contain any syntax error.
    • It will generate an a.out file.

  3. Execute
    • Now run your program by typing a.out in command prompt.

  4. Output
    • You will see "Hello World" printed on your console.

Description of Hello World program
  • First line is program is a comment placed between /* and */. Comments are used in a C program to write an explanation or for documentation purpose. When a program becomes very complex, we need to write comments to mark different parts in the program. These comments will be ignored by the compiler at compilation time.

  • The next line #include is a preprocessor directive to load the stdio.h(Standard Input/Output header file) library. This library provides some macros and functions for input or output which we can use in our program(like printf and scanf). We need stdio for printing "Hello World" using printf function.

  • The next line is int main(){.The int is what is called the return value. Every C program must have a main() function, and every C program can only have one main() function where program execution begins. The two curly brackets {} are used to group all statements together.

  • The next line is printf("Hello World") which is used for printing "Hello World" string on the screen.

  • The next line is return 0. int main() declares that main must return an integer. Returning 0 means informing operating system that program successfully completed, whereas returning 1 means error while running program.