Popular
Data Science
Technology
Finance
Management
Future Tech
C is a powerful and vеrsatilе programming languagе, useful in developing systеm softwarе and gamеs and еtc. It allows direct access to hardwarе and mеmory, making it ideal for performing critical tasks. C is a foundational programming language for more complex languages like C++, Java, еtc.
In this C programming tutorial, you will learn about the history and fеaturеs of C, how to sеt up your development environment and write your first “Hеllo World” program. Wе will also covеr kеy topics likе variablеs, data typеs, control statеmеnts, functions, arrays, pointеrs, еtc. By the еnd of this tutorial, you will have a solid background in understanding a C program.
Dennis Ritchie at Bell Labs developed the C programming language in the early 1970s. This language was an improvement over the B language, which was based on an earlier language called BCPL. This flexibility allowed data handling and enabled efficient manipulation of hardware resources.
C quickly became popular since its code could easily be moved to different machines. It was ideal for writing operating systems, including UNIX, among other major systems implemented using C at its inception. With time, various standards, such as ANSI-C and ISO-C, have helped maintain its consistent implementation across platforms.
However, C now influences much more than where it started off. Nevertheless, today, it remains one of the basic languages for the computer science education industry, which still uses it for system programming, embedded systems, high-performance applications, etc.
Learning C programming offers numerous benefits:
C is known for its unique features that make it a popular choice among programmers:
This section covers how to configure the development environment for compiling and executing C codes on your machine, writing the first C program, and the code compilation process.
The first thing you need to do before you can start programming in C is to set up a development environment.
Let’s start with a simple “Hello World” program in C.
Code:
The first line of code imports the ‘studio.h’ header file. When you run the code, it will start execution of the code from the main() method. The printf() function is defined in the ‘stdio.h’ header file, which is used to print the string in the output console.
Output:
Several operations are performed behind the scenes when you compile a C program.
Comments in ‘C’ are used to explain any piece of code so that it becomes clearer. These comments do not affect the execution of the program because they are not read by the compiler.
You can use below two different ways to add comments in C:
Code:
In the code below, we have added the single-line and multi-line comments.
Output:
In C, variables are used to store data that can be changed during program execution while constants hold values that do not change.
To define a variable in the ‘C’ language, we need to mention its type, like char, float, int, etc., along with its name.
data_type variable_name;
To define constant variables, you need to use the const keyword before the data type of the variable.
const data_type constant_name = value;
Here, we have defined the ‘age’ integer type variable and the ‘PI’ constant variable of the float data type variable.
You can see variable values in the output console.
By understanding how to declare and use variables and constants, you can effectively manage data within your C programs.
In C, data types dеfinе thе typе of data a variablе can hold. They determine the memory size, layout, thе rangе of valuеs, and thе sеt of opеrations that can bе pеrformеd on thе data.
Data Type | Size (Bytes) | Range | Format Specifier |
int | 4 | -2,147,483,648 to 2,147,483,647 | %d |
unsigned int | 4 | 0 to 4,294,967,295 | %u |
short int | 2 | -32,768 to 32,767 | %hd |
unsigned short int | 2 | 0 to 65,535 | %hu |
long int | 4 | -2,147,483,648 to 2,147,483,647 | %ld |
unsigned long int | 4 | 0 to 4,294,967,295 | %lu |
long long int | 8 | -(2^63) to (2^63)-1 | %lld |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 | %llu |
signed char | 1 | -128 to 127 | %c |
unsigned char | 1 | 0 to 255 | %c |
float | 4 | 1.2E-38 to 3.4E+38 | %f |
double | 8 | 1.7E-308 to 1.7E+308 | %lf |
long double | 16 | 3.4E-4932 to 1.1E+4932 | %Lf |
When you use C, the standard library functions are used for the input and output operations. Through these functions, you can read data from a keyboard and display data on a screen.
Below code uses ‘%d’ format specifier to take numeric input using scanf() function and store it in integer num.
The printf() function also uses the ‘%d’ format specifier to print the numeric value in the output console.
Operators in C are special symbols that perform operations on variables and values. They are essential for manipulating data and variables within a program.
In the code below, we use the ‘+’ operator to perform the addition operation and use the ‘++’ operator to perform the increment operation.
Control statements are decision-making statements that decide whether to execute a set of code or not. They allow users to execute some parts of their codes only when certain conditions are met.
Example:
In the code below, the integer variable num is declared and initialised with 10. The ‘if’ statement checks whether the value of ‘num’ is greater than 0, and it is. So, it will execute the code of ‘if-blocks’.
Output:
Example:
In this code, we execute the for loop 5 times and print 0 to 4 values.
Output:
Syntax
while (condition) {
// Code to be executed
}
Syntax
do {
// Code to be executed
} while (condition);
A function in C is a collection of statements grouped together because they perform a specific task. Functions make your program more modular, which improves its organisation.
Example:
‘int add(int a, int b);’ declares a function named add that takes two integer parameters and returns an integer.
‘sum = add(num1, num2);’ calls the add function with num1 and num2 as arguments and stores the result in sum.
‘int add(int a, int b) { return a + b; }’ defines the add function that returns the sum of a and b.
Output:
Arrays and strings are important data structures in C that help us manage collections of data.
An array is a collection of variables of the same type stored in contiguous memory locations. Mainly, it is used when we need to store multiple values in a single variable.
In this example, we use a for loop to iterate through each element of the array and print it out.
Output:
A string in C is an array of characters terminated by a null character (‘0’). Strings are used to store text.
Example:
In the code below, we have declared a string with 6 characters, including the null character at the end.
Output:
Arrays and strings support various operations, including:
Pointers are similar to the variables, but rather than storing values, it stores the memory address of the variable. They are a powerful feature in C, enabling dynamic memory allocation, efficient array handling, and the creation of complex data structures like linked lists and trees.
Example:
In the code below, the ‘ptr’ pointer stores the memory address of the ‘num’ variable.
Output:
Structures in C allow you to group different data types together into a single unit. They are used to represent a record or a complex data type that consists of multiple fields.
Example:
To define a structure, use the struct keyword followed by the structure name and the definition of its members.
You can use the variable name followed by a dot followed by a structure name to access any variable’s value.
Output:
Storage classes in C define the scope, visibility, and lifetime of variables and functions. They help control how and where variables are stored, their default values, and their accessibility throughout the program.
Here, we have covered different types of storage classes.
Memory management in C is crucial for efficient program execution. It involves the allocation and deallocation of memory during the program’s runtime.
Dynamic memory allocation allows you to allocate memory at runtime using four standard library functions:
The C preprocessor processes your source code before compilation, performing text substitution and file inclusion to make the code easier to manage.
Here, we have covered some C preprocessor directives.
File handling in C allows you to store data permanently by reading from and writing to files. This is essential for data persistence beyond program execution.
Function | Description |
fopen() | To open a file and get a file pointer. |
fclose() | Closes an opened file. |
fread() | Reads data from a file. |
fwrite() | Writes data to a file. |
fprintf() | Writes formatted output to a file. |
fscanf() | Reads formatted input from a file. |
fgets() | Reads a string from a file. |
fputs() | Writes a string to a file. |
The file opening modes allow you to define how you do for what purpose you want to open the file.
Mode | Description |
r | Open for reading. |
w | Open for writing (creates a new file or truncates an existing file). |
a | Open for appending. |
r+ | Open for reading and writing. |
w+ | Open for reading and writing (creates a new file or truncates an existing file). |
a+ | Open for reading and appending. |
We have covered the pros and cons of C programming Language in the table below.
Pros | Cons |
C programs are fast and use minimal resources. | Requires explicit handling of memory allocation and deallocation. |
Provides low-level access to memory and hardware. | It Can be difficult to master C due to pointers and manual memory management. |
Extensive standard library for various functions. | String handling requires additional code. |
Understanding C helps in learning other languages like C++, Java, and Python. | Easier to make mistakes such as buffer overflows and memory leaks. |
Supports modular programming through functions and libraries. | Basic error handling mechanisms. |
C language is adaptive to numerous domains due to its efficiency, control, and portability. Below are some main use cases:
Learning the C programming language is an invaluable asset for programmers or computer science students. Its efficiency, and low-level abilities in development of other languages make it an essential skill. By understanding C, you gain insight into how software interacts with hardware, which is crucial for developing high-performance applications.
From its historical background to practical examples, including advanced topics, this C programming tutorial has provided a comprehensive overview of C. Now, you can explore more about each concept of C programming language using the different tutorials. Regardless of whether one’s interest lies in embedded systems development, game development, or even system programming, one should learn C programming.
The DevOps Playbook
Simplify deployment with Docker containers.
Streamline development with modern practices.
Enhance efficiency with automated workflows.
Popular
Data Science
Technology
Finance
Management
Future Tech
Accelerator Program in Business Analytics & Data Science
Integrated Program in Data Science, AI and ML
Certificate Program in Full Stack Development with Specialization for Web and Mobile
Certificate Program in DevOps and Cloud Engineering
Certificate Program in Application Development
Certificate Program in Cybersecurity Essentials & Risk Assessment
Integrated Program in Finance and Financial Technologies
Certificate Program in Financial Analysis, Valuation and Risk Management
© 2024 Hero Vired. All rights reserved