Guide C
Guide C
9:00 AM to 12:00 PM and from 6:00 PM to 9:00 PM, you can follow a structured plan. This plan
will cover fundamental concepts, practical exercises, and project-based learning to solidify your
understanding. Here's a detailed plan:
Day 3: Loops
Day 4: Functions
Day 5: Arrays
Day 6: Pointers
Day 7: Strings
Navigating Directories
1. Opening the Command Line
• Windows : Open Command Prompt or PowerShell.
• macOS/Linux : Open Terminal.
2. Current Directory
• Command : pwd (Print Working Directory)
• Usage : Displays the current directory you are in.
pwd
ls
4. Changing Directories
• Command : cd (Change Directory)
• Usage : Moves to a specified directory.
cd directory_name
sh
Copy code
cd Documents
cd ..
5. Creating Directories
• Command : mkdir (Make Directory)
• Usage : Creates a new directory.
mkdir new_directory
ls -a
Creating Files
1. Creating a File
• Command : touch
• Usage : Creates an empty file.
touch filename.txt
2. Editing a File
• Command : nano , vim , or vi (Text Editors)
• Usage : Opens a text editor to create or edit a file.
nano filename.txt
vim filename.txt
3. Displaying File Contents
• Command : cat
• Usage : Displays the contents of a file.
cat filename.txt
4. Copying Files
• Command : cp (Copy)
• Usage : Copies a file to a new location.
cp source_file.txt destination_file.txt
5. Moving/Renaming Files
• Command : mv (Move)
• Usage : Moves or renames a file.
mv old_name.txt new_name.txt
6. Deleting Files
• Command : rm (Remove)
• Usage : Deletes a file.
rm filename.txt
Practical Examples
Example 1: Navigate to the Documents Directory and Create a File
cd Documents
touch myfile.txt
ls
mkdir my_new_directory
cd my_new_directory
Example 3: Create and Edit a File Using Nano
nano mytextfile.txt
cp mytextfile.txt newfile.txt
mv newfile.txt renamedfile.txt
rm renamedfile.txt
Conclusion
This guide provides a foundation for using the command line to navigate directories and manage
files. Mastering these basic commands will enhance your efficiency and capability in managing
your computer's file system. For more advanced usage, explore the manual pages of each command
using the man command (e.g., man ls ).
// hello.c
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
How to Run
1. Save the file as hello.c .
2. Open the terminal.
3. Navigate to the directory where hello.c is saved.
4. Compile the program:
Introduction
Understanding the structure of a C program is fundamental to becoming proficient in C
programming. This guide covers the key components of a C program, including functions, the
main function, preprocessor directives, variables, and basic I/O operations.
// Function declaration
void greet();
int main() {
// Variable declaration and initialization
int number = 10;
// Function call
greet();
// Print statement
printf("Number: %d\n", number);
// Return statement
return 0;
}
// Function definition
void greet() {
printf("Hello, World!\n");
}
Components
1. Preprocessor Directives
Example
c
Copy code
#include <stdio.h>
int main() {
greet(); // Function call
return 0;
}
Example
c
Copy code
#include <stdio.h>
int main() {
printf("Hello from the main function!\n");
return 0;
}
4. Preprocessor Directives
Common Directives
• #include for including libraries.
• #define for defining constants or macros.
• #if , #ifdef , #ifndef for conditional compilation.
Example
c
Copy code
#include <stdio.h>
#define PI 3.14
int main() {
printf("Value of PI: %f\n", PI);
return 0;
}
Initialization
c
Copy code
int number = 10;
float price = 99.99;
char grade = 'A';
Example
c
Copy code
#include <stdio.h>
int main() {
int number = 10;
float price = 99.99;
char grade = 'A';
return 0;
}
6. Basic Input/Output
Output using printf
• Syntax : printf(format_string, variables);
• Example :
c
Copy code
printf("Hello, %s!\n", "World");
printf("Number: %d\n", 10);
Example
c
Copy code
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d\n", number);
return 0;
}
7. Control Structures
If-Else
c
Copy code
#include <stdio.h>
int main() {
int number = 10;
if (number > 5) {
printf("Number is greater than 5\n");
} else {
printf("Number is 5 or less\n");
}
return 0;
}
For Loop
c
Copy code
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("i: %d\n", i);
}
return 0;
}
While Loop
c
Copy code
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("i: %d\n", i);
i++;
}
return 0;
}
Conclusion
Understanding the structure of a C program involves knowing how to use functions, the main
function, preprocessor directives, variables, and basic I/O operations. With this foundation, you can
start writing and exploring more complex C programs.
Follow-up Suggestions
a. Practice writing small C programs that utilize different control structures like if-else and
loops.
b. Explore functions in more detail, including passing arguments and returning values.