Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
48 views
C Programming (Hand Written Notes)
Uploaded by
Biswaranjan Swain
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save c Programming ( Hand Written Notes ) For Later
Download
Save
Save c Programming ( Hand Written Notes ) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
48 views
C Programming (Hand Written Notes)
Uploaded by
Biswaranjan Swain
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save c Programming ( Hand Written Notes ) For Later
Carousel Previous
Carousel Next
Save
Save c Programming ( Hand Written Notes ) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 25
Search
Fullscreen
1. Introduction to C Programming 1.1. What ic C? Welaberation: C ic a powerful, general-purpose programming language known for its ePiciency and flexibility. Tt was developed in the early 17700 by Dennis Ritehie at Bell Labs. C provides low-level access to memory, making it suitable for ramming, but it alco supports high-level constructs for application prog PP opp development. It has a rich history and has influenced many other programming languages. Example: Here's a cimple “Hello, World!” program in C: World! \n'); yp Your Development Environment: start programming in C, it's important to cet up your it. Thie involvec installing a C compiler, such ac GCC (GU 1 a code editor or integrated development environment nr about the basic structure of aC ¢, the main function, and the section, youll have a working 1.1 serves as a practical exempleChapter 2: Basic Syntax and Data Types In thie chapter, we'll delve into the fundamental building blocks of C programming. You'll learn about the syntax uced in C programs and explore various data types, which are eccential for storing and manipulating information. 2.1. Variables and Constants Variables: Variables are named ctorage locations that hold data. They are declared using a data type followed by a name. Example: int age; declares an integer variableCharacter Data Type: The char data type is used to store single characterc. Example: char grade = ‘A> 2.3. Operators and Expressions Arithmetic Operators: C supports arithmetic operators like +, -, /, and % for basic mathematical operations. Example: int recult = 10 + 5; Relational Operators: Relaticseveral control structures u to execute a block of code if a ig fale, the code inside the if block ic : The elce ctatement can be used in conjunction with if to execute block of code if the condition ic Falce. int num = 5; (oum % 2 == 0) printf The number ig even.\n'); J elce { printf(The number i¢ odd.\n"): i Switch Statement: The ewitch ctatement ic used when you have multiple conditions to evaluate. It provides a way to chooce between ceveral different code blocks based on the value of an expression. Example: char grade = 8:e while loop repeatedly executes a block of cade as long as a is Crue. count = 1; while (count <= 5) { printf Count: %d\n', count); count++; i | For Loop: The for loop is commonly used for iterating over a range of values or performing a specific number of iterations. Example: for int i = 4; 1 <= 5; ite) { printf Iteration $d\n", i);4. [Farctions and Modular Programming] Function Basics En C programming, a function ic a self-contained block of code that performs a specific tack. Functions allow you to break your program into omaller, manageable pieces, making your code more organized and easier to t takes two integers ac parameters and lo add and prints the recult. before ite actual definition. It about the function's name, return are usually placed at theint add(int a, int 6); main() { add(5, 3): ult: d\n", result); for add tells the compiler what to expect when it the main function. where a function calle itself to colve a ant way to solve certain typec of problems.printf Factorial of fd ic d\n", n, result); return 0; I thie example, the factorial Function caleulates the factorial of a number using d Lifetime of Variables : C have a scope (where they can be accecced) and a lifetime (how ict). Understanding variable scope and lifetime ic crucial for writingwith 5 elements .e firct element (index 0) 16 2 to the second element (index 1) l Arrays = idimensional arrays are arrays of arrays, forming a structure. They are useful for representing tables, grids, and matrices. Example: int matri[3][3] = {{1, 2, 3}, 4, 5, 68, ¢#, & Wf: // Accessing elements int element = matrix{1][2]; //Retrievec the value 6 from the matrix 5.3 Strings in C : Tn © strings are represented ac arrays of characters. They are null-terminated, meaning they end with a null character (\0') to cignify the end of the string. Example: char greeting[6] = “Hello”: //Tneludes space for the null character char name] = “John’: Auffmatically sized based on the string lengthrecult); /Dutput: “Hello World” | strlen(recult); /fbet the length of the reculting string rrintt Length: Bd\n", length); //Output: "Length: 17” return 0;6. [Pointers Pointere are a fund. work directly with + for efficient memory § tasks. 6.1 Understanding Pointers What Are Pointers? Pointers are variablec that store y They are denoted by an asterisk (*). For int x= 10; Regular variable int “ptr = Bx; ffbinter to int Here, ptr stores the memory addrece of x. Pointer Arithmetic : You can perform arithmetic operations on pointers, such and decrementing them. For instance: int arrl5] = {1, 2, 3, 4, 5: int “p = arr[2]; prt: (Moves p to the next element (arr[3]) 6.2 Dynamic Memory Allocation (malloc, calloc, free) malloc and calloc : malloc and calloc are functions used for dynamic memory allocation. malloc allocates a block of memory, while calloc initializes the allocated memory to zero. Here's an example: int Aare = (int “)malloo(5 * cizeoflint)); //Allocatec memory for an array of 5 intsAfter you're d it using free to free(arr); //Releasec Example: Dynamic Array Creating a dynamic array int size = 5; int “dynamicArr = (int “)malloc(size for (int i = 0; i < gives ine) f dynamicArr[i] = i * 10; i free(dynamicArr); //Don't forget to free the mer 6.3 Pointers and Arrays : Arrays and Pointers : Arrays and pointers are clocely related in C. An array n a pointer to ite first element: int mumbere[] = {1, 2, 3, 4, SI; int “ptr = number; (ptr points to the firct element of numbers Pointer-to-Array - You can create pointers to entire arrays, allowing you to manipulate arrays more flexibly: int arr[3] = (10, 20, 30); int Cptr)[3] = &arr; /jptr ic a pointer to an array of 3 intsChapter #: [Structures and Unions] 2.1 Defining and Using Structures : Structures in C allow you to group together variables of different data types under a cingle name. They are fundamental for organizing data in complex programs. Example: U/ Define a structure for a point in 2D space struct Point { inl x;Unions are similar to structure but allow you to store different data types in the came memory location. Thie can be uceful when you want to save memory, and you only need to accece one member at a time. Example: // Define a union for storing integers and floats union Data {declarations. Example: U/ Define a typedef for a complex number typedef struct { double real; + BAIP\n’, num.real, nurm.imag):Chapter 8: [File Handling] File handling in C ie eccential for reading and writing data to and from files. Thie chapter explorec how to work with filec, perform input and output operations, and handle errore effectively. 8.1 File I/O Operations : Opening a File: Before you can read from or write to a file, you muct open it. You can uce the fopen function for thie purpose. Here's an example: FILE “flePointer; flePointer = fopen(‘example.txt’, 'r’); if (flePointer == NULL) £ printl(File could not be opened.’); return 7; i Reading Fromprintf File could not be created."); return 1; J PorintfoutputFile, "Hello, World!’); feloce(outputFile); Clocing a File: Always cloce a file when you're done with it to free up recources. Feloce(FlePointer); 8.2 Error Handling with Fileo : Checking for File Existence: You can use the a file exicte before attempting to open it. if (accece(‘example.txt’, FOK) 4 - printf File existe.\n");mescages. FILE “fle = fopen(‘nonexistent.txt’, 'r’): if (Ble == NULL) £ perror(Error opening file’); i Thic chapter equips readere with the knowledge and practical ckille needed to manipulate files in C. Understanding fle I/0 and error handling i¢ crucial for various real-world applications, cuch ¢ processing and file management.programming, delv 2.1 Preproceccor Dir The C prepri before the actual compilation conditional compilation and macro Conditional Compilation: Conditional exclude parte of your code based on pr used to create code that behaves differently #inclade Hdefine DEBUG 1 int main() £ it DEBUG printf Debug mode ic enabled.\n’); ffelee printl' Debug mode ic dicabled.\n"); Hendif return 0; i Macros: Macros are a way to define reucable code cnippets. They are often used for constants or simple functions. Hdefine SQUARE (x) (x * x) int main() { int result = SQUARE (5);macros that can be uced input/output, math, string + Example: Using Standard Hinclude int main() £ double num = 16.0; double squareRoot = cart (num); printl(The square root of KIf i¢ Kiln", nui, return 0; i 7.3 Command-Line Arguments : C programs can accept command-line arguments, allowing user provide imput when running the program Example: Command-Line Arguments flinclude int main(int arge, char “argv[]) { if (arge 42) £ printf(Usage: s\n", argv[o]); return 7; iExample: Bit Mas #inclade int main() { unsigned char flags = Ox0A; flags [= (1 << 2); Set the third flags &= ~(1 << 1); Cleof/the printt( Flags: fx\n’, flags); //Output: return 0; iChapter 10: [C in Practice In thie chapter, we will explore the programming language. We will and efficient C code, provide offer concrete examples to illustrate 10.1 Best Practices : Writing good C code involves readability, maintainability, and ¢| guidelines: Naming Conventions: Use meaning! and symbol names. For example, ins reprecent the number of elements in Code Formatting: Consistently format | spacing conventions. Thig makes your for (int i = 0; 4 < 10; ist) £ printf Tteration d\n’, i); i Commenting: Add comments to explain 6 important functions. Comments improve ¢ instance: /* Caleulate the Factorial of a number “/ int factorial(int m) { Bhke case irc dt return 7;J Uf Recursive cage return n * factorial(n - 1): J Error Handling: Properly error codes, return values, oF 10.2 Case Studie = Th thie section, we will examine Let's look at one example: Case Study: File Compression Imagine you are tacked with d This case study will cover the de: such a tool. Well delve into data performance optimization techniques, ‘ practical applications. 10.3 Real-world Examples + To illustrate the concepte diccucced in code examples. Here's an example that #include int main() £ FILE “fle = fopen(‘non_exictent.txt”, 'b')- if (Ble == NUL) { perror(Error opening file’); exit(1); WW File procecsing code goec here... feloce(le);return 0; J In thie example, we cuccecsfully, and handle «
You might also like
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6125)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (627)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brene Brown
4/5 (1148)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (932)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8214)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (631)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1253)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (8365)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (860)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (877)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (954)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (2922)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (484)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (277)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (4972)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (444)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Tóibín
3.5/5 (2061)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4281)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (447)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1987)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1068)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (1993)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2641)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (1936)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (125)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4074)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (75)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (901)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2530)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (790)
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4/5 (105)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
3.5/5 (109)
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Principles: Life and Work
From Everand
Principles: Life and Work
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Steve Jobs
From Everand
Steve Jobs
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Yes Please
From Everand
Yes Please
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
The Outsider: A Novel
From Everand
The Outsider: A Novel
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
John Adams
From Everand
John Adams
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
Little Women
From Everand
Little Women
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel