0% found this document useful (0 votes)
35 views

C Programming Notes

Uploaded by

shivamsv167
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)
35 views

C Programming Notes

Uploaded by

shivamsv167
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/ 9

C Programming Notes

Programming in C

History of C

- C was developed by Dennis Ritchie at Bell Labs in the early 1970s.

- It was created to write operating systems and is still widely used.

- C is known for its efficiency, portability, and ease of use for system-level programming.

Introduction to C Programming Languages

- C Programming is a general-purpose language used for creating system applications.

- It is procedural, meaning the code is organized into functions that carry out specific tasks.

Structure of C Programs

A C program generally has the following structure:

#include <stdio.h> // Preprocessor directive

int main() { // Main function

printf("Hello, World!"); // Code to print output

return 0; // Returns 0 to indicate successful execution

Compilation and Execution of C Programs

1. Compilation: The source code (.c file) is converted to machine code by a compiler.

2. Execution: The machine code (usually a .exe file) is run on the computer.

Debugging Techniques
- Debugging is the process of finding and fixing errors in the program.

- Common techniques include:

- Using printf() to print variable values.

- Using tools like gdb (GNU Debugger).

Data Types and Sizes

- C has various data types like:

- int (4 bytes)

- float (4 bytes)

- char (1 byte)

- double (8 bytes)

Example:

int age = 25;

float price = 5.99;

char letter = 'A';

Declaration of Variables

- Variables in C must be declared before use. Example:

int x;

float y;

Modifiers

- Modifiers like short, long, signed, and unsigned adjust the range or storage size of the variable.

Example:

unsigned int x = 10;


Identifiers and Keywords

- Identifiers are names for variables, functions, etc.

- Keywords are reserved words like int, if, return, etc.

Symbolic Constants

- Constants are fixed values defined using #define or const.

Example:

#define PI 3.14

Storage Classes

- Automatic: Default for local variables (stored in stack).

- External: Global variables accessed across files.

- Register: Stored in CPU registers for quick access.

- Static: Preserves variable values between function calls.

Enumerations

- Enum defines a set of named integer constants.

Example:

enum day {Sunday, Monday, Tuesday};

Command-Line Parameters

- Parameters passed to main() when the program is executed from the command line.

int main(int argc, char *argv[]) {

printf("%s", argv[1]);

}
Macros

- Macros are defined using #define and replaced during preprocessing.

Example:

#define MAX 100

The C Preprocessor

- Preprocessor commands like #include and #define are executed before compilation.

Operators

Unary Operators

- Operate on a single operand (e.g., ++, --).

Example:

int x = 5;

x++; // Increment x by 1

Arithmetic & Logical Operators

- Arithmetic: +, -, *, /, %

- Logical: &&, ||, !

Example:

int x = 5 + 3;

if (x > 3 && x < 10) { ... }

Bitwise Operators

- Operate on bits: &, |, ^, <<, >>

Example:
int x = 5 & 3; // Bitwise AND

Assignment Operators

- Assign values: =, +=, -=, *=, /=

Example:

int x = 5;

x += 2; // x = x + 2

Conditional Expressions

- Short if-else: ? :

Example:

int x = (a > b) ? a : b;

Precedence and Order of Evaluation

- Defines the order in which operators are evaluated.

Control Statements

if-else

- Used for conditional execution.

if (x > 0) {

printf("Positive");

} else {

printf("Negative");

switch
- Selects one of many code blocks to execute.

switch (x) {

case 1: printf("One"); break;

case 2: printf("Two"); break;

break and continue

- break: Exits a loop or switch.

- continue: Skips the current iteration.

goto statement

- Used to jump to a label in the code.

goto label;

label: printf("Jumped");

Loops

for

- Repeats a block of code a set number of times.

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

while

- Repeats while a condition is true.

while (x < 5) { ... }

do-while

- Executes the loop at least once, then repeats based on a condition.


do { ... } while (x < 5);

Functions

Built-in and User-Defined

- Built-in functions: printf(), scanf(), etc.

- User-defined functions: Custom functions written by the programmer.

Function Declaration, Definition, and Call

- Declaration:

int sum(int a, int b);

- Definition:

int sum(int a, int b) { return a + b; }

- Call:

int result = sum(5, 3);

Parameter Passing: Call by Value and Call by Reference

- Call by value: Sends a copy of the argument.

- Call by reference: Sends the actual variable address.

Recursive Functions

- Functions that call themselves.

Example:

int factorial(int n) {

if (n == 0) return 1;

return n * factorial(n - 1);

}
Multifile Programs

- Program split across multiple files.

Arrays

Linear and Multidimensional Arrays

- Linear array: List of elements.

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

- Multidimensional array: Array of arrays.

int matrix[3][3];

Passing Arrays to Functions

- Arrays are passed by reference to functions.

void display(int arr[], int size);

Arrays and Strings

- Strings are arrays of characters.

char str[] = "Hello";

Structure and Union

Definition and Differences

- Structure: Groups variables of different types.

- Union: Shares memory for its members.

Self-Referential Structures

- Structures that include pointers to their own type.


Address of (&) Operator

- Used to get the address of a variable.

Pointer to Pointer

- Pointer to another pointer.

int **ptr;

Dynamic Memory Allocation

- malloc(): Allocates memory.

- calloc(): Allocates and initializes memory.

Array of Pointers

- Array where each element is a pointer.

int *arr[5];

Function of Pointers

- Functions can return or take pointers as arguments.

Structures and Pointers

- Pointers to structures allow for dynamic allocation of structure memory.

File Handling in C

- File handling is done using functions like fopen(), fread(), fwrite(), etc.

FILE *fp = fopen("file.txt", "r");

You might also like