C is a foundational programming language used in many applications, from operating systems to embedded systems. This tutorial covers the basics of C programming with examples for each topic.
2. C Get Started
To get started, write your C code in a file with a .c
extension. Then, compile it with a compiler like GCC.
Example:
Compile and run with:
3. C Syntax
C uses a structure where code blocks are enclosed in {}
and statements end with ;
.
Example:
4. C Output
Use printf()
from <stdio.h>
to display output.
Example:
5. C Comments
Add comments to make code more understandable. Example:
6. C Variables
Variables store data values. Example:
7. C Data Types
Define data types for storing specific values. Example:
8. C Constants
Use constants to store unchangeable values. Example:
9. C Operators
Operators perform operations on variables. Example:
10. C Booleans
C uses integers for booleans (0 for false, non-zero for true). Example:
11. If…Else
The if...else
statement controls conditional execution.
Example:
12. Switch
The switch
statement selects code to run based on matching cases.
Example:
13. While Loop
A while
loop repeats as long as the condition is true.
Example:
14. For Loop
A for
loop is used for repeated actions.
Example:
15. Break and Continue
Use break
to exit a loop, and continue
to skip to the next iteration.
Example:
16. Arrays
Arrays store multiple values of the same type. Example:
17. Strings
Strings are arrays of characters. Example:
18. User Input
Use scanf()
for user input.
Example:
19. Memory Address
Use the &
operator to get a variable’s address.
Example:
20. Pointers
Pointers store memory addresses. Example:
21. Functions
Functions organize reusable code blocks. Example:
22. Function Parameters
Functions can have parameters. Example:
23. Scope
Variables have local or global scope. Example:
24. Function Declaration
Declare a function before calling it if it appears later in the file. Example:
25. Recursion
Recursion occurs when a function calls itself. Example:
26. Math Functions
Use <math.h>
for math functions.
Example:
27. Files
File handling allows reading and writing data. Example:
28. Create Files
Open a file for writing with w
.
Example:
29. Write to Files
Use fprintf()
to write data to a file.
Example:
30. Read Files
Read data from a file with fscanf()
.
Example:
31. Structures
Structures group variables of different types. Example:
32. Enums
Enums define named integer constants. Example:
33. Memory Management
Use malloc()
and free()
for dynamic memory.
Example:
34. Reference
In C programming, a reference section typically includes keywords, standard libraries, and header files that provide essential functions and tools. Here’s a closer look at each component in the reference section:
– Keywords
C keywords are reserved words that have a specific purpose and cannot be used as variable names. They define the core language structure and control flow. Examples include:
int
: Declares an integer variable.return
: Exits a function and optionally returns a value.if
,else
,while
,for
: Used for flow control.const
,volatile
: Modify variable behavior, like declaring constants.struct
,enum
,typedef
: Define data structures and types.
A complete list of C keywords can be found in C documentation or through compiler resources.
– Header Files
Header files in C define functions, macros, and constants. Including header files enables you to use prewritten code without rewriting it, improving code efficiency and readability. Header files in C are included with #include <header.h>
.
Here’s a brief overview of some commonly used header files:
<stdio.h>
- The standard input-output library provides functions for data input and output.
- Common functions:
printf()
(to display output) andscanf()
(to read user input). - Example:
<stdlib.h>
- The standard library provides utility functions for memory allocation, control functions, conversions, and more.
- Common functions:
malloc()
andfree()
for dynamic memory management. - Example:
<string.h>
- The string library includes functions to manipulate and handle strings.
- Common functions:
strlen()
(get string length),strcpy()
(copy strings),strcmp()
(compare strings). - Example:
<math.h>
- This library provides functions for mathematical calculations.
- Common functions:
sqrt()
(square root),pow()
(power),abs()
(absolute value). - Example:
<ctype.h>
- The character type library includes functions to test and manipulate individual characters.
- Common functions:
isalpha()
(check if alphabetic),isdigit()
(check if numeric),toupper()
(convert to uppercase). - Example:
Each header file provides a suite of functions that enhances the functionality of your C program, helping with common tasks like mathematical calculations, string manipulation, memory allocation, and character handling. Understanding how to use these libraries and keywords allows you to write more efficient and organized C code.
References
- The C Programming Language” by Brian W. Kernighan and Dennis M. Ritchie
- C Programming Language Documentation
- GNU C Library (glibc)
- TutorialsPoint – C Programming Tutorial
- GeeksforGeeks – C Programming Language
- cppreference.com – C Standard Library
- CProgramming.com