CS baba

CS Baba is a learning platform

cs baba

C language Tutorial with programming approach for beginners and professionals, helps you to understand the C language tutorial easily. Our C tutorial explains each topic with programs.

The C Language is developed by Dennis Ritchie for creating system applications that directly interact with the hardware devices such as drivers, kernels, etc.

C programming is considered as the base for other programming languages, that is why it is known as mother language.

It can be defined by the following ways:

  1. Mother language
  2. System programming language
  3. Procedure-oriented programming language
  4. Structured programming language
  5. Mid-level programming language

1) C as a mother language

C language is considered as the mother language of all the modern programming languages because most of the compilers, JVMs, Kernels, etc. are written in C language, and most of the programming languages follow C syntax, for example, C++, Java, C#, etc.

It provides the core concepts like the array, strings, functions, file handling, etc. that are being used in many languages like C++, Java, C#, etc.


2) C as a system programming language

A system programming language is used to create system software. C language is a system programming language because it can be used to do low-level programming (for example driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is written in C.

It can’t be used for internet programming like Java, .Net, PHP, etc.


3) C as a procedural language

A procedure is known as a function, method, routine, subroutine, etc. A procedural language specifies a series of steps for the program to solve the problem.

A procedural language breaks the program into functions, data structures, etc.

C is a procedural language. In C, variables and function prototypes must be declared before being used.


4) C as a structured programming language

A structured programming language is a subset of the procedural language. Structure means to break a program into parts or blocks so that it may be easy to understand.

In the C language, we break the program into parts using functions. It makes the program easier to understand and modify.


5) C as a mid-level programming language

C is considered as a middle-level language because it supports the feature of both low-level and high-level languages. C language program is converted into assembly code, it supports pointer arithmetic (low-level), but it is machine independent (a feature of high-level).

Low-level language is specific to one machine, i.e., machine dependent. It is machine dependent, fast to run. But it is not easy to understand.

High-Level language is not specific to one machine, i.e., machine independent. It is easy to understand.


C Program

In this tutorial, all C programs are given with C compiler so that you can quickly change the C program code.

File: main.c

  1. #include <stdio.h>  
  2. int main() {  
  3. printf(“Hello World!\n”);  
  4. return 0;  
  5. }  

Test it Now

Output:

Hello World!

A detailed description of above program is given in next chapters.

History of C Language

Dennis Ritchie - founder of C language

History of C language is interesting to know. Here we are going to discuss a brief history of the c language.

C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A.

Dennis Ritchie is known as the founder of the c language.

It was developed to overcome the problems of previous languages such as B, BCPL, etc.

Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL.

Let’s see the programming languages that were developed before C language.

LanguageYearDeveloped By
Algol1960International Group
BCPL1967Martin Richard
B1970Ken Thompson
Traditional C1972Dennis Ritchie
K & R C1978Kernighan & Dennis Ritchie
ANSI C1989ANSI Committee
ANSI/ISO C1990ISO Committee
C991999Standardization Committee

Features of C Language

C features

C is the widely used language. It provides many features that are given below.

  1. Simple
  2. Machine Independent or Portable
  3. Mid-level programming language
  4. structured programming language
  5. Rich Library
  6. Memory Management
  7. Fast Speed
  8. Pointers
  9. Recursion
  10. Extensible

1) Simple

C is a simple language in the sense that it provides a structured approach (to break the problem into parts), the rich set of library functionsdata types, etc.


2) Machine Independent or Portable

Unlike assembly language, c programs can be executed on different machines with some machine specific changes. Therefore, C is a machine independent language.


3) Mid-level programming language

Although, C is intended to do low-level programming. It is used to develop system applications such as kernel, driver, etc. It also supports the features of a high-level language. That is why it is known as mid-level language.


4) Structured programming language

C is a structured programming language in the sense that we can break the program into parts using functions. So, it is easy to understand and modify. Functions also provide code reusability.


5) Rich Library

provides a lot of inbuilt functions that make the development fast.


6) Memory Management

It supports the feature of dynamic memory allocation. In C language, we can free the allocated memory at any time by calling the free() function.


7) Speed

The compilation and execution time of C language is fast since there are lesser inbuilt functions and hence the lesser overhead.


8) Pointer

C provides the feature of pointers. We can directly interact with the memory by using the pointers. We can use pointers for memory, structures, functions, array, etc.


9) Recursion

In C, we can call the function within the function. It provides code reusability for every function. Recursion enables us to use the approach of backtracking.


10) Extensible

C language is extensible because it can easily adopt new features.

How to install C

There are many compilers available for c and c++. You need to download any one. Here, we are going to use Turbo C++. It will work for both C and C++. To install the Turbo C software, you need to follow following steps.

  1. Download Turbo C++
  2. Create turboc directory inside c drive and extract the tc3.zip inside c:\turboc
  3. Double click on install.exe file
  4. Click on the tc application file located inside c:\TC\BIN to write the c program

1) Download Turbo C++ software

You can download turbo c++ from many sites. download Turbo c++

2) Create turboc directory in c drive and extract the tc3.zip

Now, you need to create a new directory turboc inside the c: drive. Now extract the tc3.zip file in c:\truboc directory.

3) Double click on the install.exe file and follow steps

Now, click on the install icon located inside the c:\turboc

install turbo c++

It will ask you to install c or not, press enter to install.

how to install  turbo c++

Change your drive to c, press c.

how to install turbo c

Press enter, it will look inside the c:\turboc directory for the required files.

how to install turbo c++

Select Start installation by the down arrow key then press enter.

how to install turbo c

Now C is installed, press enter to read documentation or close the software. how to install turbo c

4) Click on the tc application located inside c:\TC\BIN

Now double click on the tc icon located in c:\TC\BIN directory to write the c program.

tc

In windows 7 or window 8, it will show a dialog block to ignore and close the application because fullscreen mode is not supported. Click on Ignore button.

Now it will showing following console.

install tc

First C Program

General Overview of a Simple C Program’s Structure:

The general architecture of a simple C program typically consists of several vital components. Below is an outline of the essential elements and their purposes:

  • Header Files:

The #include directives at the beginning of the program are used to include header files. Header files provide function prototypes and definitions that allow the C compiler to understand the functions used in the program.

  • Main Function:

Every C program starts with the main function. It is the program’s entry point, and execution starts from here. The main function has a return type of int, indicating that it should return an integer value to the operating system upon completion.

  • Variable Declarations:

Before using any variables, you should declare them with their data types. This section is typically placed after the main function’s curly opening brace.

  • Statements and Expressions:

This section contains the actual instructions and logic of the program. C programs are composed of statements that perform actions and expressions that compute values.

  • Comments:

Comments are used to provide human-readable explanations within the code. They are not executed and do not affect the program’s functionality. In C, comments are denoted by // for single-line comments and /* */ for multi-line comments.

  • Functions:

C programs can include user-defined functions and blocks of code that perform specific tasks. Functions help modularize the code and make it more organized and manageable.

  • Return Statement:

Use the return statement to terminate a function and return a value to the caller function. A return statement with a value of 0 typically indicates a successful execution in the main function, whereas a non-zero value indicates an error or unexpected termination.

  • Standard Input/Output:

C has library functions for reading user input (scanf) and printing output to the console (printf). These functions are found in C programs and are part of the standard I/O library (stdio.h header file). It is essential to include these fundamental features correctly while writing a simple C program to ensure optimal functionality and readability.

Additional Information:

There is some additional information about the C programs. Some additional information is as follows:

  • Preprocessor Directives:

C programs often include preprocessor directives that begin with a # symbol. These directives are processed by the preprocessor before actual compilation and are used to include header files, define macros, and perform conditional compilation.

  • Data Types:

C supports data types such as int, float, double, char, etc. It depends on the program’s requirements, and appropriate data types should be chosen to store and manipulate data efficiently.

  • Control Structures:

C provides control structures like if-else, while, for, and switch-case that allow you to make decisions and control the flow of the program.

  • Error Handling:

Robust C programs should include error-handling mechanisms to handle unexpected situations gracefully. Techniques like exception handling (using try-catch in C++) or returning error codes are commonly employed.

  • Modularization:

As programs grow in complexity, it becomes essential to modularize the code by creating separate functions for different tasks. This practice improves code reusability and maintainability.

Remember, the architecture and complexity of a C program can vary significantly depending on the specific application and requirements. The outline is a general overview of a simple C program’s structure.

Explain the First C program:

To write the first C program, open the C console and write the following code:

Code:

  1. #include <stdio.h>    
  2. int main(){    
  3. printf(“Hello C Language”);    
  4. return 0;   
  5. }  

Let us first study the various parts of this C program:

#include <stdio.h>:

In this line, the program includes the standard input/output library (stdio.h) due to the preprocessor directive. For input and output tasks, the stdio.h library contains methods like printf and scanf.

int main() { … }:

It is the main function which is the entry point of the C program. The program starts executing from the beginning of the main function.

printf(“Hello World!\n”);:printf(“Hello World!”);:

Use the printf() function to print formatted output to the console. In this example, the string “Hello, C Language” is printed, followed by a newline character (n) which moves the pointer to the following line after the message is displayed.

return 0;

When the return statement is 0, the program has been completed. When determining the state of a program, the operating system frequently uses the value returned by the main function. A return value of 0 often indicates that the execution was successful.

After compilation and execution, this C program will quit with a status code 0 and output “Hello, C Language” to the terminal.

The “Hello, C Language” program is frequently used as an introduction to a new programming language since it introduces learners to essential concepts such as text output and the structure of a C program and provides a rapid way to validate that the working environment is correctly set up.

To write, compile, and run your first C program, follow these steps:

Step 1: Open a text editor

Open a text editor of your choice, such as Notepad, Sublime Text, or Visual Studio Code. It will be where you write your C code.

Step 2: Write the C program

Now, copy and paste the following code into the text editor:

  1. #include <stdio.h>  
  2. int main() {  
  3. printf(“Hello, C Language”);  
  4. return 0;  
  5. }  

Step 3: Save the file

After that, save the file with a .c extension such as first_program.c. This extension indicates that it is a C source code file.

Step 4: Compile the program

Now, compile the program in the command prompt.

Step 5: Run the program

After successful compilation, you can run the program by executing the generated executable file. Enter the following command into the terminal or command prompt:

  1. ./first_program  

The program will execute, and you will see the output on the console:

Output:

Hello, C Language

How to compile and run the C program

There are two ways to compile & run the c program by menu and by shortcut.

By Menu

  • Now click on the compile menu, then compile sub-menu to compile the c program.
  • Then click on the run menu and the sub-menu to run the c program.

By shortcut

  • Or, press the ctrl+f9 keys to compile and run the program directly.
  • You will see the following output on the user screen.
  • You can view the user screen any time by pressing the alt+f5 keys.
  • Now press Esc to return to the turbo c++ console.

Conclusion:

Finally, the first C program introduces the C programming language and its fundamental structure. It illustrates the necessary components for writing, compiling, and running a C program.

The program contains the standard input-output library (stdio.h), which includes routines for output operations such as printf(). The main() function is the program’s entry point, from which execution begins. The printf() function is used within the main() method to print the message “Hello, C Language” to the console.

C compiler such as GCC is required to compile the program. The code is stored in a text file with the .c extension, and the compiler is started by typing gcc, followed by the names of the input and output files.

The compilation process converts machine-readable instructions from human-readable C code. Once the program has been successfully constructed, it may be started by double-clicking the resultant executable file. In a terminal or command prompt, the executable is called by its file name, followed by ./. After that, the program is performed, and the “Hello C Language” output is shown on the console.

By following these instructions, you will get a basic grasp of developing, constructing, and running a C program. It offers the groundwork for further investigating more complex ideas and developing more sophisticated applications using the C programming language.