A beginner-friendly guide to learning C programming, covering fundamental concepts with example codes and explanations. In this two-hour session, we will quickly revise the essential C programming concepts, focusing on building a solid foundation in:
- Basic Input/Output
- Simple arithmetic and ASCII operations
- Matrices
- Functions
- Arrays
- String Operations
- Pointers
- Recursion
- Structures
- Practice Questions
This session is perfect for beginners. By the end, you’ll be ready to implement these key concepts in your projects and exams!
It's important to have a clear idea of how logic works in C. The main aim of this session is to help you learn how to convert your logic into executable code. For reference, check out the ACM foundations docs here!
Compiling and Running
- Open a terminal in the folder where the
.c
file is located. - Compile using:
cc -o output_name file_name.c ./output_name
Before we start coding, ensure you include the necessary libraries in your program:
#include <stdio.h> // For standard input/output functions
#include <stdlib.h> // For standard library functions like malloc, atoi, etc.
#include <string.h> // For string handling functions
#include <math.h> // For math operations like sqrt(), pi, etc.
Understanding the various methods for taking input and displaying output is fundamental. Here's a quick reference for the format specifiers: // Format Specifier for Different Data Types
Format Specifier | Data Type |
---|---|
%d or %i | int |
%c | char |
%f | float |
%lf | double |
%s | string |
In C, basic input/output operations are handled using the stdio.h
library. We use functions like printf()
to display output on the screen and scanf()
to take input from the user. Understanding how to use these functions effectively is crucial for writing interactive C programs.
In this example, we demonstrate how to take input and output simple data types such as integers, floats, and strings.
In this example, we take multiple types of input and print them out. We use gets and puts functions here.
Now, it's your turn to try! Write a program that takes a student's ID (integer), name (string), and age (integer), then prints out the entered details.
In this section, we will practice performing basic arithmetic operations and working with ASCII values in C.
- Arithmetic operations: Addition, subtraction, multiplication, division
- ASCII values: Each character in C is represented by a unique integer value (ASCII).
This program calculates the sum and difference of two numbers.
Write a program that takes input as marks for 6 courses and calculates the average:
Write a program that reads two numbers and an operator, then performs the corresponding arithmetic operation (+, -, *, /):
Write a program that calculates the sum of the digits of a number:
In this section, we will work with matrix operations. Let's start by adding matrices and then move on to extracting diagonal elements.
This program demonstrates how to add two matrices as well as taking input for the matrices.
Write a program that prints only the diagonal elements of a matrix:
Functions in C are blocks of code designed to perform specific tasks. They help in breaking down a large program into smaller, manageable pieces and improve code reusability.
Functions can either return a value or not, and they may take parameters as input. In C, a function is declared with the following syntax:
return_type function_name(parameters) {
// body of the function
}
Example 1: Add Two Numbers
This function takes two integers as input, adds them, and prints the result.
Example 2: Find the Factorial of a Number
This function takes an integer as input and calculates the factorial of that number using a loop.
Write a function to find the larger of two numbers.
FindLarger.c
In this section, we will learn how to handle arrays in C. Arrays allow you to store multiple values of the same type in a single variable, making it easier to manage large datasets.
This program initializes an array of integers and prints its values.
This program takes an array of integers and finds the largest element in the array.
Write a program that takes a list of integers from the user, stores them in an array, and prints the array in reverse order.
String operations in C involve manipulating sequences of characters that end with a null terminator (\0
). Several built-in functions in <string.h>
simplify common string-related tasks like copying, comparing, finding lengths, and concatenating.
String Function | Description |
---|---|
strcpy(dest, src) |
Copies the content of the source string into the destination string. |
strcmp(str1, str2) |
Compares two strings: |
- Returns an integer < 0 if str1 < str2 . |
|
- Returns an integer > 0 if str1 > str2 . |
|
- Returns 0 if str1 == str2 . |
|
strlen(str) |
Returns the length of the string (excluding the \0 character). |
strcat(str1, str2) |
Concatenates str2 to str1 , storing the resultant string in str1 . |
Note: Include
<string.h>
to use these functions. Always allocate enough space for the null character (\0
) in your strings. For example:char string[5]; // This can store up to 4 characters, with the 5th slot reserved for '\0'.
For a demonstration of all the above functions, please take a look at the file: all_funcs.c
Pointers are variables that store the memory address of another variable. They are powerful tools in C that allow you to directly manipulate memory, create dynamic data structures, and optimize program performance.
- Declaration:
int *p;
declares a pointer to an integer. - Initialization: Use the address-of operator (
&
) to assign an address to a pointer:p = &var;
. - Dereferencing: Access the value stored at the memory location using the dereference operator (
*
):*p
.
This example demonstrates the basic use of pointers to store and access a variable's address and value.
PointerBasics.c
This example shows how pointer arithmetic can be used to traverse an array.
PointerArithmetic.c
This example demonstrates how pointers can be used to swap the values of two variables.
SwapWithPointers.c
- Pointers and Arrays: Arrays and pointers are closely related. Pointer arithmetic can be used to navigate arrays.
- Swapping with Pointers: Pointers allow direct manipulation of values, making swapping efficient.
- Safety: Always ensure proper initialization of pointers and avoid dereferencing uninitialized or null pointers.
Recursion is like a magical spell where a function calls itself! It’s like standing between two mirrors and seeing infinite reflections—but with an exit strategy. In programming, recursion breaks down a problem into smaller instances of itself until it reaches a simple base case (the "exit strategy").
Here’s a simple way to think about it:
- You solve the smallest version of a problem (base case).
- Use that to build up the solution to the bigger problem.
Calculate the factorial of a number n
using recursion.
Factorial.c
Generate the nth Fibonacci number using recursion.
Fibonacci.c
Find the sum of digits of a number using recursion.
SumOfDigits.c
- The function keeps calling itself, solving smaller chunks.
- When the base case is reached, the "unwinding" happens (like reverse dominoes).
- The answers combine into the final solution.
Structures in C allow you to combine variables of different data types into a single entity. This is especially useful for organizing complex data like employee records, student information, etc.
- A structure groups related variables together under a single name.
- Each variable in the structure is called a "member."
- Structures can hold multiple data types.
Example 1: Basic Student Structure
This example demonstrates how to define a structure for student details and use it to store and display data.
Basic Student Structure
Example 2: Employee Database Using Structures and Arrays
This example showcases how to use structures and arrays together to manage an employee database.
Employee Database
Here are 5 more challenging practice questions that mix different topics. They will test your understanding of functions, recursion, pointers, and string operations. Try solving them!
Write a function to reverse a string using recursion. You should not use any built-in functions like strrev
. For example, given the input "hello"
, the output should be "olleh"
.
Write a function that calculates the length of a string without using the strlen
function. For example, given the input "abcdef"
, the function should return 6
.
Write a recursive function to calculate the power of a number. For example, if the input is 2
and the exponent is 3
, the output should be 8
. (i.e., 2^3 = 8
)
Write a function that calculates the sum of all elements in an array using pointers. For example, given the array {1, 2, 3, 4, 5}
, the function should return 15
.
Write a function that counts the number of occurrences of a given character in a string. For example, given the string "programming"
and the character 'r'
, the function should return 2
.