C Tutorial
C Language Tutorial for beginners to learn C online for free.
C programming is the widely used System programming language to create applications. C provides a base for all programming languages. It is easy & simple to learn.
C Tutorial covers C language Syntax, variables, data types, functions, pointers, file handling and other concepts.
In this online C course, you will learn how to write & execute C programs step by step. You will also find useful exercise, codes, Interview Questions to enhance your knowledge at the end of each chapter.
C Introduction
C is a general purpose programming language. It was created by Dennis Ritchie at Bell Labs in the early 1970s, to write the UNIX operating system. More than 50 years later it…
C Features
C has been used for decades, because of a few strong features. Here are the main ones. Main features of C Feature Meaning Simple and small Only 32 keywords in the standard…
C Installation
To run C programs on your computer you need a C compiler. The most popular free compiler is GCC. Mac also has Clang, which works in the same way. Install on Windows…
C Basic Program Structure
Every C program follows a simple structure. Let us understand it with one small program. Output: Parts of the program Part Meaning #include <stdio.h> Adds the standard input output header file, so…
C Comments
Comments are notes written inside the code for humans. The compiler ignores them. We use comments to explain what the code does, or to stop one line from running for a while….
C Keywords
Keywords are reserved words that have a special meaning for the compiler. You cannot use them as names of your variables or functions. All keywords are in small letters. List of 32…
C Variables
A variable is a name given to a memory location. It stores a value that can change while the program runs. In C you must tell the type of the variable before…
C Data Types
A data type tells the compiler what kind of value a variable will store, and how much memory it needs. Basic data types Type Used for Example Format char Single character ‘A’…
C Constants
A constant is a value which cannot be changed after it is set. We use constants for fixed values like pi or the number of days in a week. Using the const…
C printf and scanf
printf() prints output on the screen and scanf() reads input from the keyboard. Both are defined in the stdio.h header file. printf() Output: scanf() scanf() needs the address of the variable, so…
C Escape Sequences
Some characters like a new line or a tab cannot be typed inside a string directly. For these we use escape sequences. An escape sequence starts with a backslash \ followed by…
C Format Specifiers
A format specifier tells printf() and scanf() what kind of value to print or read. It starts with %. Common format specifiers Specifier Used for Example value %d or %i int 25…
C Type Casting
Type casting means converting a value from one data type to another. It can happen automatically (implicit) or you can do it yourself (explicit). Implicit conversion The compiler converts the smaller type…
C Operators
Operators are symbols that do some operation on values (called operands). C has many types of operators. Arithmetic operators Operator Meaning Example + Add a + b – Subtract a – b…
C If Else Statement
Decision making statements run a block of code only when a condition is true. The if statement is the most basic one. if statement Output: if else statement Use else to run…
C Else If Ladder and Nested If
When there are more than two choices, we use the else if ladder. The conditions are checked from top to bottom. The first true one runs, and the rest are skipped. Else…
C Switch Statement
The switch statement chooses one block from many options, based on the value of a variable. It is cleaner than a long if else ladder when you compare one variable with fixed…
C While Loop
A loop repeats a block of code many times. The while loop checks the condition first, and runs the body while it is true. Use it when you do not know how…
C Do While Loop
The do while loop is like the while loop, but the condition is checked at the end. So the body always runs at least one time. Syntax Do not forget the semicolon…
C For Loop
The for loop is used when you know how many times to repeat. It keeps the start, the condition and the change in one line. Syntax initialization runs once at the start….
C Nested Loops
A loop inside another loop is called a nested loop. For each round of the outer loop, the inner loop runs completely. Nested loops are used for tables, patterns and two dimensional…
C break Statement
The break statement ends the nearest loop or switch immediately. The program continues with the line after the loop. Stop a loop when a value is found Output: Without break the loop…
C continue Statement
The continue statement skips the remaining code of the current round and jumps to the next round of the loop. Skip even numbers Output: Add only positive numbers Output: break vs continue…
C goto Statement
The goto statement jumps to a label in the same function. A label is a name followed by a colon. Syntax and example Output: A fair use: leave nested loops Output: Next:…
C Functions
A function is a block of code with a name, which does one job. You write it once and call it whenever needed. Every C program has at least one function, main()….
C Function Arguments and Return Value
A function can take values (arguments) and can give a value back (return value). Based on this there are four types of functions. Type Arguments Return value 1 No No 2 Yes…
C Call by Value and Call by Reference
There are two ways to pass arguments to a function. Call by value A copy of the value is sent. Changes inside the function do not change the original variables. C uses…
C Recursion
Recursion is when a function calls itself. Every recursive function needs a base case, which stops the calls. Without it the function calls itself forever, and the program crashes. Factorial Factorial of…
C Scope and Storage Classes
Scope tells where a variable can be used. Storage class tells how long it lives and where it is stored. Local and global variables Type Declared Can be used Default value Local…
C Arrays
An array stores many values of the same type under one name. Instead of making 5 variables for 5 marks, we make one array with 5 elements. Declare and initialize Each element…
C Two Dimensional Arrays
A two dimensional (2D) array is like a table with rows and columns. It is used for matrices, marks of many students in many subjects and game boards. Declare and initialize This…
C Passing Arrays to Functions
You can pass a whole array to a function. In the function parameter we write the array name with empty brackets. We should also send the size, because the function does not…
C Strings
C does not have a separate string type. A string is an array of characters which ends with a special null character ‘\0’. The null character tells where the string ends. Declare…
C String Input and Output
There are many ways to read a string from the user. Each has some different behaviour. scanf with %s It reads only one word. It stops at the first space. Output: No…
C String Functions
The header file string.h has ready functions to work with strings. We must include it to use them. Common string functions Function Work strlen(s) Returns the length of the string strcpy(dest, src)…
C strlen() Function
strlen() returns the number of characters in a string. The null character is not counted. Syntax Example Output: Spaces are counted, so “I love C” has 8 characters. Write your own strlen…
C strcpy() Function
strcpy() copies the source string (with its null character) into the destination array. Syntax Example Output: Why we need it In C you cannot write dest = src; to copy a string….
C strcat() Function
strcat() adds (concatenates) the second string at the end of the first string. Syntax Example: full name Output: The destination array (full) has space of 30 characters, so the joined text fits….
C strcmp() Function
strcmp() compares two strings character by character. Return value Result Meaning 0 Both strings are equal Less than 0 First string comes before the second Greater than 0 First string comes after…
C strstr() Function
strstr() searches for a substring in a string. It returns a pointer to the first place where it is found, or NULL if it is not found. Syntax Example Output: p -…
Reverse a String in C
Many old books use strrev() to reverse a string. But this function is not part of standard C. It works in some Windows compilers only, and gives an error in GCC and…
C Convert String to Lowercase and Uppercase
strlwr() and strupr() are not standard C functions. They work only in some compilers. The portable way is to use tolower() and toupper() from ctype.h on each character. Convert to uppercase Output:…
C Array of Strings
To store many names we need an array of strings. In C there are two common ways. Way 1: 2D char array Each row is one string. All rows have the same…
C Pointers
A pointer is a variable which stores the memory address of another variable. Pointers look hard at first, but they are only addresses. They make C fast and powerful. Two operators Operator…
C Pointer Arithmetic
You can do some arithmetic on pointers. When you add 1 to a pointer, it moves to the next element of its type, not to the next byte. So p + 1…
C Pointer to Pointer
A pointer to pointer (double pointer) stores the address of another pointer. It is written with two stars, like int **pp. How it looks Variable Stores num The value (for example 50)…
C Pointers and Arrays
In C, pointers and arrays are very closely related. The name of an array is the address of its first element. Array name as pointer Output: The expression a[i] is exactly the…
C Pointers and Functions
Pointers and functions work together in three ways. 1. Pass a pointer to change values Output: 2. Get more than one value from a function A function can return only one value,…
C Dynamic Memory Allocation
Normally the size of an array is fixed when we write the program. With dynamic memory allocation we ask for memory while the program runs, so we can use exactly what we…
C Structures
An array stores many values of the same type. A structure (struct) stores values of different types together. For example, a student has a name, roll number and marks. Define and use…
C Nested Structure
A nested structure means one structure is a member of another structure. For example, a student has an address, and the address has a city and a pincode. Example Output: To reach…
C Array of Structures
When we need many records of the same type, we make an array of structures. It works like a small table: each element is one record. Example: three students Output: Find the…
C Pointer to Structure
A pointer can store the address of a structure. To reach members through the pointer we use the arrow operator ->. Example Output: p->roll is the short form of (*p).roll. Use the…
C Union
A union is like a structure, but all its members share the same memory. So only one member can hold a valid value at a time. The size of a union is…
C typedef
typedef gives a new name (alias) to an existing data type. It does not create a new type. It only makes the code shorter and easier to read. Simple typedef Output: typedef…
C enum
An enumeration (enum) is a set of named integer constants. It makes the code easy to read, because you use names like MONDAY instead of numbers like 1. Define and use Output:…
C Preprocessor and Macros
The preprocessor runs before the compiler. It handles the lines that start with #. It copies header files, replaces macros and removes code which is not needed. Common directives Directive Work #include…
C File Handling
With file handling a program can save data in a file, so the data stays after the program ends. In C we use a FILE pointer and the functions of stdio.h. Steps…
C Read From a File
Suppose the file students.txt has this text. Read line by line with fgets Output: fgets() returns NULL at the end of the file, so the loop stops there. Read formatted data with…
C Command Line Arguments
We can give values to a program when we start it from the terminal. These are called command line arguments. To receive them, main() takes two parameters. argc is the number of…
Features of C Language
C is a general purpose, structured programming language. C programming is widely used in computer Technology. Low Level Features C programming provides Low level features that are generally provided by the Lower…