C is a general-purpose, procedural, high-level programming language that is widely used for developing software and applications, system programming, games, web development, and more. C was developed by Dennis M. Ritchie at the Bell Telephone Laboratories in 1972. It is a powerful and flexible language that has influenced many other programming languages, such as C++, Java, Python, etc. C is also the best choice to start with programming as it gives you a foundational understanding of how computers work.
In this content, you will learn the basics and advanced concepts of C programming, such as variables, data types, operators, control statements, functions, arrays, strings, pointers, user-defined data types, memory management, file handling, and more. You will also see examples and exercises to practice your skills and test your knowledge.
To start learning C, you will need a C compiler and an editor. A compiler is a program that converts your C source code into executable code that can run on your computer. An editor is a program that allows you to write and edit your C source code. There are many C compilers and editors available, such as GCC, Visual Studio, CodeBlocks, etc.
c
// This is a comment
#include <stdio.h> // This is a header file that contains input and output functions
int main() // This is the main function where the program execution begins
{
printf(“Hello World!\n”); // This is a statement that prints a message to the screen
return 0; // This is a statement that returns a value to the operating system
}
To compile and run a C program, you need to save it with a .c extension, such as hello.c, and then use the compiler command, such as gcc hello.c -o hello, to generate an executable file, such as hello.exe. Then, you can run the executable file by typing its name, such as hello, in the command prompt or terminal. You should see the output of your program, such as Hello World!, on the screen.
C is a case-sensitive language, which means that uppercase and lowercase letters are treated differently. For example, int and INT are two different identifiers in C. C also follows some rules and conventions for naming identifiers, such as variables, constants, functions, etc. For example, identifiers must start with a letter or an underscore, and cannot contain spaces or special characters, such as @, #, $, etc.
C has various data types that define the size and range of values that can be stored in a variable. For example, int is a data type that can store integer values, such as 1, 2, 3, etc. char is a data type that can store character values, such as ‘a’, ‘b’, ‘c’, etc. float is a data type that can store floating-point values, such as 1.23, 4.56, 7.89, etc. There are also modifiers, such as short, long, unsigned, signed, etc., that can modify the size and range of data types. For example, long int is a data type that can store larger integer values than int.
C has various operators that perform different operations on operands, such as arithmetic, logical, bitwise, relational, assignment, etc. For example, + is an arithmetic operator that performs addition, such as 2 + 3 = 5. && is a logical operator that performs logical AND, such as 1 && 1 = 1. << is a bitwise operator that performs left shift, such as 1 << 2 = 4. == is a relational operator that performs equality check, such as 2 == 2 = 1. = is an assignment operator that assigns a value to a variable, such as x = 5.
C has various control statements that control the flow of execution of the program, such as decision-making, looping, branching, etc. For example, if is a decision-making statement that executes a block of code if a condition is true, such as if (x > 0) { printf(“Positive\n”); }. for is a looping statement that executes a block of code repeatedly for a specified number of times, such as for (i = 0; i < 10; i++) { printf(“%d\n”, i); }. break is a branching statement that terminates the current loop or switch, such as break;.
C has various functions that are blocks of code that perform a specific task and can be reused in the program. For example, main is a function that is the entry point of the program, such as int main() { … }. printf is a function that prints a formatted output to the screen, such as printf(“%s\n”, “Hello World!”);. You can also define your own functions, such as int add(int x, int y) { return x + y; }. You can call a function by using its name and passing the required arguments, such as add(2, 3);.
C has various data structures that are ways of organizing and storing data, such as arrays, strings, pointers, structures, unions, etc. For example, an array is a data structure that can store multiple values of the same data type in a contiguous memory location, such as int arr[5] = {1, 2, 3, 4, 5};. A string is a data structure that can store a sequence of characters, such as char str[6] = “Hello”;. A pointer is a data structure that can store the address of another variable, such as int *p = &x;.
C has various features that allow you to manipulate the memory and files of the system, such as memory management, file handling, preprocessor, etc. For example, memory management is a feature that allows you to dynamically allocate and deallocate memory at runtime, such as int *p = malloc(sizeof(int)); free(p);. File handling is a feature that allows you to create, read, write, and delete files, such as FILE *fp = fopen(“test.txt”, “w”); fprintf(fp, “Hello World!\n”); fclose(fp);. Preprocessor is a feature that allows you to perform some operations before the compilation of the program, such as #include, #define, #ifdef, etc.
These are some of the basic and advanced concepts of C programming that you will learn in this content. You will also see examples and exercises to practice your skills and test your knowledge. By the end of this content, you will be able to write efficient and elegant C programs for various purposes. I hope you enjoy learning C and have fun with it.
Happy coding!
Indian Institute of Embedded Systems – IIES