KEMBAR78
C Prog | PDF | Integer (Computer Science) | Pointer (Computer Programming)
0% found this document useful (0 votes)
39 views7 pages

C Prog

Uploaded by

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

C Prog

Uploaded by

Ebei Jacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1: Introduction to C Programming

What is C?

- C is a general-purpose, procedural programming language developed in the early 1970s by Dennis


Ritchie at Bell Labs.

- It is one of the most widely used programming languages and forms the foundation for many other
languages like C++, Java, and Python.

Why Learn C?

- Efficiency: C is fast and allows low-level memory manipulation.

- Portability: Programs written in C can run on different platforms with minimal changes.

- Foundation: Learning C helps you understand how computers work at a deeper level.

Basic Structure of a C Program

#include <stdio.h> // Preprocessor directive

int main() { // Main function

printf("Hello, World!\n"); // Print statement

return 0; // Return statement

```

- **Explanation:**

- `#include <stdio.h>`: Includes the standard input/output library.

- `int main()`: The main function where program execution begins.

- `printf("Hello, World!\n");`: Prints text to the console.

- `return 0;`: Indicates successful program termination.


Variables and Data Types

Variables

- A variable is a named location in memory used to store data.

- Syntax: `data_type variable_name = value;`

Data Types

- int: Stores integers (e.g., `int age = 25;`).

- float: Stores decimal numbers (e.g., `float pi = 3.14;`).

- char: Stores single characters (e.g., `char grade = 'A';`).

- double: Stores larger decimal numbers (e.g., `double bigNum = 12345.6789;`).

Example:

#include <stdio.h>

int main() {

int age = 25;

float pi = 3.14;

char grade = 'A';

double bigNum = 12345.6789;

printf("Age: %d\n", age);

printf("Pi: %.2f\n", pi);

printf("Grade: %c\n", grade);

printf("Big Number: %.4lf\n", bigNum);

return 0;

}
Explanation:

- `%d`: Format specifier for integers.

- `%f`: Format specifier for floats.

- `%c`: Format specifier for characters.

- `%lf`: Format specifier for doubles.

---

Control Structures

Conditional Statements

- if-else: Executes a block of code based on a condition.

int num = 10;

if (num > 0) {

printf("Positive number\n");

} else {

printf("Negative number\n");

- switch-case: Executes one of many blocks of code.

int day = 3;

switch (day) {

case 1: printf("Monday\n"); break;

case 2: printf("Tuesday\n"); break;

case 3: printf("Wednesday\n"); break;

default: printf("Invalid day\n");

}
Loops

- for loop: Repeats a block of code a specific number of times.

for (int i = 1; i <= 5; i++) {

printf("%d\n", i);

- while loop: Repeats a block of code while a condition is true.

int i = 1;

while (i <= 5) {

printf("%d\n", i);

i++;

- do-while loop: Similar to `while`, but the block is executed at least once.

int i = 1;

do {

printf("%d\n", i);

i++;

} while (i <= 5);


Functions

What is a Function?

- A function is a block of code that performs a specific task.

- Syntax:

return_type function_name(parameters) {

// Code to execute

return value;

Example:

#include <stdio.h>

// Function to add two numbers

int add(int a, int b) {

return a + b;

int main() {

int result = add(5, 3);

printf("Sum: %d\n", result);

return 0;

```

- Explanation:

- `int add(int a, int b)`: Defines a function named `add` that takes two integers as parameters and
returns their sum.

- `add(5, 3)`: Calls the function with arguments `5` and `3`.
Arrays and Pointers

Arrays

- An array is a collection of elements of the same type.

- Syntax: `data_type array_name[size];`

int numbers[5] = {1, 2, 3, 4, 5};

for (int i = 0; i < 5; i++) {

printf("%d\n", numbers[i]);

Pointers

- A pointer is a variable that stores the memory address of another variable.

- Syntax: `data_type *pointer_name;`

int num = 10;

int *ptr = &num; // ptr stores the address of num

printf("Value: %d\n", *ptr); // Access value using pointer

```

Example:

#include <stdio.h>

int main() {

int numbers[3] = {10, 20, 30};

int *ptr = numbers; // Pointer to the first element of the array

for (int i = 0; i < 3; i++) {


printf("Value: %d, Address: %p\n", *ptr, ptr);

ptr++; // Move to the next element

return 0;

Explanation:

- `int *ptr = numbers;`: Points to the first element of the array.

- `ptr++;`: Moves the pointer to the next element.

You might also like