0% found this document useful (0 votes)
4 views5 pages

C Fundamentals

Uploaded by

cjnamoco623
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

C Fundamentals

Uploaded by

cjnamoco623
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C Programming Fundamentals (Detailed

Guide)

1. Introduction to C
 What is C?
o A general-purpose, compiled programming language.
o Very close to hardware (low-level).
o Basis for OS (Linux kernel is written in C).
 How C Works:

1. Write code in a .c file.


2. Compile with GCC:
3. gcc program.c -o program
4. Run:
5. ./program

2. Basic Syntax
✅ Hello World
#include <stdio.h> // input-output functions

int main() {
printf("Hello, C!\n");
return 0;
}

Output:

Hello, C!

🔑 Explanation:

 #include <stdio.h> → lets us use printf.


 main() → program entry point.
 printf → prints text.
 \n → new line.
3. Variables & Data Types
#include <stdio.h>

int main() {
char name[] = "Alice"; // String (char array)
int age = 25; // Integer
float height = 5.7; // Decimal
int isStudent = 1; // Boolean (1 = true, 0 = false)

printf("%s is %d years old, %.1f tall, student: %d\n", name, age, height,
isStudent);
return 0;
}

Output:

Alice is 25 years old, 5.7 tall, student: 1

📌 Note: C uses format specifiers:

 %d → integer
 %f → float
 %s → string
 %c → character

4. Operators
✅ Arithmetic
int a = 10, b = 3;
printf("%d\n", a + b); // 13
printf("%d\n", a - b); // 7
printf("%d\n", a * b); // 30
printf("%d\n", a / b); // 3
printf("%d\n", a % b); // 1

✅ Comparison
printf("%d\n", 10 > 5); // 1 (true)
printf("%d\n", 10 == 5); // 0 (false)

✅ Logical
int x = 1, y = 0;
printf("%d\n", x && y); // 0
printf("%d\n", x || y); // 1
printf("%d\n", !x); // 0

5. Control Flow
✅ If-Else
int age = 18;
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}

✅ For Loop
for (int i = 0; i < 5; i++) {
printf("Number: %d\n", i);
}

✅ While Loop
int count = 1;
while (count <= 3) {
printf("Count: %d\n", count);
count++;
}

6. Data Structures
✅ Arrays
char fruits[3][10] = {"apple", "banana", "cherry"};
printf("%s\n", fruits[1]); // banana

✅ Strings
char name[] = "Alice";
printf("%s\n", name);

📌 Strings in C are just arrays of characters ending with \0.

✅ Structs (like objects in C)


#include <stdio.h>

struct Person {
char name[20];
int age;
};

int main() {
struct Person p1 = {"Alice", 25};
printf("Name: %s, Age: %d\n", p1.name, p1.age);
return 0;
}

7. Functions
#include <stdio.h>

void greet(char name[]) {


printf("Hello, %s\n", name);
}

int main() {
greet("Bob");
return 0;
}

Output:

Hello, Bob

8. Pointers (Unique to C!)


#include <stdio.h>

int main() {
int x = 10;
int* ptr = &x; // pointer stores memory address of x

printf("x = %d\n", x);


printf("Address of x = %p\n", ptr);
printf("Value at pointer = %d\n", *ptr);
return 0;
}

9. Memory Management
#include <stdio.h>
#include <stdlib.h>

int main() {
int* arr = (int*) malloc(3 * sizeof(int)); // allocate memory for 3 ints
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;

printf("%d %d %d\n", arr[0], arr[1], arr[2]);

free(arr); // free memory


return 0;
}

10. Error Handling


C doesn’t have built-in exceptions like C++/Java/Python.
We usually handle errors with return values or errno.

Example:

#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *f = fopen("nofile.txt", "r");
if (f == NULL) {
printf("Error: File not found\n");
return 1;
}
fclose(f);
return 0;
}

📝 Practice Exercises (C Beginner Level)


1. Print your name, age, and hobby in one line using printf.
2. Write a program that takes two integers and prints their sum.
3. Create an array of 5 numbers and print the largest one.
4. Write a function that checks if a number is prime.
5. Define a struct Car with brand, model, and year, then make an object and print it.

You might also like