Complete Guide to Basic C Programming Concepts with Examples
C is one of the most widely used programming languages and serves as the foundation for many modern languages such as C++, Java, and Python. If you’re just getting started with programming or want to strengthen your understanding of C, the following topics are essential.
1. Structure of a C Program
Before diving into the specifics of the language, it is crucial to understand the basic structure of a C program. Every C program follows a standard structure, which ensures that the code is readable and maintainable.
Basic Structure:
#include <stdio.h> // Preprocessor Directive
// Function Declaration
int main() {
// Variable Declaration
printf("Hello, World!\n");
return 0;
}
Explanation:
#include <stdio.h>: Includes the Standard Input Output library.int main(): Entry point of the program.printf(): Function used to display output.return 0: Ends the function and returns control to the operating system.
Key Components:
- Preprocessor Directives
- Function Declaration and Definition
- Variable Declaration
- Statements and Expressions
- Return Statement
Extended Example:
#include <stdio.h>
// Function to print a welcome message
void greetUser() {
printf("Welcome to C Programming!\n");
}
int main() {
greetUser();
int age = 20;
printf("Your age is: %d\n", age);
return 0;
}
This example adds a user-defined function to show modular programming in C.
2. Tokens in C
A token is the smallest individual unit in a C program. The compiler breaks the entire code into these meaningful elements for further analysis.
Types of Tokens:
- Keywords
- Identifiers
- Constants
- Operators
- Special Symbols
- Strings
Example:
int sum = a + b;
In this line:
intis a keywordsum,a,bare identifiers=and+are operators;is a special symbol
Additional Token Example:
char letter = 'A';
float pi = 3.14;
printf("Character: %c, PI: %f\n", letter, pi);
This example contains multiple tokens, including constants, keywords, and format specifiers.
3. Reserved Words in C
Reserved words (or keywords) are words that are part of the C language syntax and cannot be used for other purposes like variable names.
List of Common Reserved Words:
int, float, char, return, void, if, else, for, while, do, break, continue, switch, case, default, sizeof, typedef, struct
Example:
float temperature;
if (temperature > 30.0) {
printf("It's hot today!\n");
} else {
printf("It's a normal day.\n");
}
Here, float, if, and else are reserved words.
Invalid Use Example:
int return = 10; // Error: 'return' is a reserved word and cannot be used as variable name
Avoid using these reserved words as identifiers to prevent compilation errors.
4. Keywords in C
Keywords are a subset of reserved words and have predefined meanings. They help define the structure and control of the program.
Key Characteristics:
- Cannot be redefined.
- Written in lowercase.
- Always used for specific operations or declarations.
Common Keywords with Meaning:
| Keyword | Meaning |
|---|---|
int |
Declares integer variables |
return |
Returns value from a function |
if |
Conditional execution |
while |
Looping conditionally |
void |
No return value (functions) |
char |
Declares character variables |
float |
Declares floating-point variables |
for |
Used for loops |
break |
Terminates loops or switch blocks |
continue |
Skips to next iteration in a loop |
Example:
int main() {
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
return 0;
}
This prints: 0 1 2 4. The continue skips printing 3.
5. Constants in C
Constants are fixed values that do not change during the execution of a program. They improve readability and make code easier to manage.
Types of Constants:
- Integer Constants:
10,-50 - Floating-point Constants:
3.14,-0.99 - Character Constants:
'a','Z' - String Constants:
"Hello","C Language" - Symbolic Constants (using
#define) - Const Keyword:
const int age = 25;
Examples:
Symbolic Constant:
#define MAX 100
printf("Max value is: %d\n", MAX);
Const Keyword:
const float PI = 3.14159;
printf("Value of PI: %.2f\n", PI);
Attempting to modify PI would cause a compiler error.
Practical Constant Use:
#define DISCOUNT 0.1
float price = 500;
float final_price = price - (price * DISCOUNT);
printf("Final Price: %.2f\n", final_price);
Bonus: Practical Program Combining All Concepts
#include <stdio.h> // Preprocessor Directive
#define PI 3.14159 // Constant
// Function to calculate area
float calculateArea(float r) {
return PI * r * r;
}
int main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius); // Input from user
float area = calculateArea(radius);
printf("Area of the circle: %.2f\n", area);
return 0;
}
Explanation:
- Uses all foundational concepts.
- Modular design with a user-defined function.
- Makes use of symbolic constants, data types, control structures.



Se você procura um casino online confiável e com ótimos jogos, experimente o 7 games casino. Já joguei várias vezes e sempre me diverti muito. E o suporte ao cliente é muito atencioso.