Understanding C Programming

TL;DR
A compact reference to C programming essentials: comments, variables and data types with their sizes and ranges, arrays, structures, functions and parameters, control structures, operators, input and output, pointers, preprocessor directives and bitwise operators, with examples for each.
On this page
Introduction to C
C is an imperative (procedural) language used for system programming. All code must be inside functions.
Example:
while(98) printf("#cisfun\n");
Comments
Comments in C are crucial for code documentation and clarity. They can be single-line or multi-line.
Examples:
// Single-line comment
/* Multi-line
comment */
Variables and Data Types
C supports various data types for different storage needs, such as int, char, float, double, etc.
Examples of variable declaration:
int age;
char initial;
float salary;
double pi;
Data Types | Integer types (on most 64-bit computers)
| Type | Storage size | Value range |
|---|---|---|
| char | 1 byte | -128 to 127 |
| unsigned char | 1 byte | 0 to 255 |
| short | 2 bytes | -32,768 to 32,767 |
| unsigned short | 2 bytes | 0 to 65,535 |
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| unsigned int | 4 bytes | 0 to 4,294,967,295 |
| long | 8 bytes | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| unsigned long | 8 bytes | 0 to 18,446,744,073,709,551,615 |
Arrays
Arrays in C allow storing multiple elements of the same type sequentially in memory.
Example:
int numbers[10];
char name[50];
Structures
Structures in C allow defining a custom data type that groups variables under one name.
Example:
struct person {
char name[50];
int age;
};
Functions
Functions in C are blocks of code that perform a specific task. They can take parameters and return values.
Example of function definition:
int add(int a, int b) {
return a + b;
}
void greet(char *name) {
printf("Hello, %s!\n", name);
}
Control Structures
C provides control structures like if, else, while, for, etc., for decision-making and looping.
Examples:
if (condition) {
// code block
}
for (int i = 0; i < 10; i++) {
// code block
}
while (condition) {
// code block
}
Operators and Expressions
C supports various operators for arithmetic, logical, bitwise operations, etc., and expressions always evaluate to a value.
Examples:
int result = 10 + 5;
int a = 10;
int b = 5;
int sum = a + b;
if (a > b) {
// code block
}
Input and Output
In C, input and output operations are performed using functions like scanf and printf.
Example:
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d\n", number);
return 0;
}
Pointers
Pointers in C are variables that store memory addresses. They are used for dynamic memory allocation and efficient memory management.
Example:
int num = 10;
int *ptr = #
printf("Value of num: %d\n", *ptr);
Control Flow
The flow of execution in C programs is controlled using loops and conditional statements.
Examples:
if (condition) {
// code block
}
while (condition) {
// code block
}
for (int i = 0; i < 10; i++) {
// code block
}
Function Parameters and Return Values
Functions in C can take parameters as input and return values as output.
Example:
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("Result: %d\n", result);
return 0;
}
Preprocessor Directives
Preprocessor directives in C begin with a # symbol and are used for including header files, macro definitions, etc.
Example:
#include <stdio.h>
#define PI 3.14159
int main() {
printf("Value of PI: %f\n", PI);
return 0;
}
Bitwise Operators
Bitwise operators in C perform operations at the bit-level. They are used for manipulating bits in integers.
Example:
unsigned int a = 5; // binary: 00000101
unsigned int b = 3; // binary: 00000011
unsigned int result = a & b; // bitwise AND
printf("Result: %u\n", result); // Output: 1
Typedef
The typedef keyword in C is used to create custom data type aliases for existing data types.
Example:
typedef struct {
char name[50];
int age;
} Person;
int main() {
Person p;
strcpy(p.name, "John");
p.age = 30;
printf("Name: %s, Age: %d\n", p.name, p.age);
return 0;
}
Enum
Enums in C allow defining named constants which represent integer values.
Example:
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
enum Day today = WEDNESDAY;
printf("Today is %d\n", today); // Output: 3 (index of WEDNESDAY)
Typedef
The typedef keyword in C is used to create custom data type aliases for existing data types.
Example:
typedef struct {
char name[50];
int age;
} Person;
int main() {
Person p;
strcpy(p.name, "John");
p.age = 30;
printf("Name: %s, Age: %d\n", p.name, p.age);
return 0;
}
Memory Management
In C, memory management involves allocating and deallocating memory using functions like malloc and free.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = NULL;
ptr = (int *)malloc(5 * sizeof(int)); // Allocate memory
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use allocated memory
for (int i = 0; i < 5; i++) {
ptr[i] = i * 10;
}
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
free(ptr); // Deallocate memory
return 0;
}
File Handling
C provides file handling operations to read from and write to files using FILE pointers and functions like fopen, fread, fwrite, etc.
Example:
#include <stdio.h>
int main() {
FILE *fp;
char c;
fp = fopen("file.txt", "r"); // Open file in read mode
if (fp == NULL) {
perror("Error opening file");
return -1;
}
while ((c = fgetc(fp)) != EOF) {
putchar(c); // Print each character
}
fclose(fp); // Close the file
return 0;
}
Error Handling
Error handling in C involves checking return values of functions and using techniques like errno and perror.
Example:
#include <stdio.h>
#include <errno.h>
int main() {
FILE *fp;
fp = fopen("nonexistentfile.txt", "r");
if (fp == NULL) {
perror("Error");
printf("Error code: %d\n", errno);
return 1;
}
fclose(fp);
return 0;
}
Conclusion
C is a powerful programming language known for its efficiency and versatility. Mastering C programming involves understanding its syntax, data types, control structures, functions, and more.
By practicing examples and exploring different concepts, you can become proficient in C programming and utilize it for various applications, including system programming, embedded systems, and application development.
Key takeaways 5
- C is a procedural language where all code lives inside functions.
- Data types have fixed sizes and value ranges.
- Arrays and structures organize related data.
- Pointers store memory addresses and power much of C.
- Preprocessor directives and bitwise operators give low-level control.
Frequently asked questions
What is a pointer in C?
A pointer is a variable that stores the memory address of another variable, allowing functions to modify data directly and enabling dynamic memory and data structures.
What is the difference between an array and a structure in C?
An array holds multiple values of the same type in contiguous memory. A structure groups variables of different types under one name.
What do preprocessor directives do?
Directives such as #include and #define are processed before compilation to include header files, define constants and macros and conditionally compile code.
Learn it hands-on at AL Academy
Free practical programs in software, automation and IT, with labs and certificates.
Related articles

Learning C Programming: Variables, Control Structures, and Loops
Diving into the world of programming can be both exciting and challenging, especially when starting with a language as fundamental as C.

What is Pseudocode?
Before diving into the complexities of programming languages, every programmer should start with a clear plan.

Overview of Unix Commands for C Development
Useful Unix commands for C projects: converting spaces to tabs, fixing DOS line endings, enforcing code style and compiling and debugging.
Comments
No comments yet. Start the conversation.