Nil369 / Akash_C_Code_NOTES

WORK IN PROGRESS...This My Coding Notes. I have made this to help others to grasp the basic concept of C as well as as revise VERY EASILY & systematically.If anyone wants to contribute they can

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

image

C_Notes

Author : Akash Halder

These are my C crisp notes revising them will be very benefitial. But for clear understanding of each & every concept refer my souce code based notes

Refer my Source Code for better understanding of these concepts

1. Introduction:

C icon

1. Developed by Dennis Ritchie at Bell Labs in the early 1970s.
  1. Known for its efficiency, simplicity, and low-level system access.

  2. Follows a procedural programming paradigm.

  3. Focuses on functions and structured programming.

  4. Highly portable language, allowing programs written in C to run on different platforms with minimal modifications.

  5. C uses a syntax that includes variables, data types, operators, and control structures (loops and conditionals).

  6. To run C/C++ in your system you need 2 things:

    ► MinGW compiler => Just search mingw compiler downloader in google => Install it & then set the environment variable by giving the compiler path

    ► Code Editor/ IDE => Download VS Code

    I know many people will suggest you to install DevC++/ Turbo C ... But all of these are discontinuos software

    ► Make a file in that editor =>with ".c " extension in the file name.

    ► Install Code Runner Plugin if u r using VS code => Will run the code easily in the IDE or you have to write some individual code in the terminal.

2. How is it different from C++?

► The syntax of C++ is almost identical to that of C, as C++ was developed as an extension of C.

► In contrast to C, C++ supports classes and objects, while C does not.

► C gives most of the control to the hand of users. Things like memory allocation and manipulation are totally in the hands of the programmer. Being a flexible language, it provides more access to the programmer because of which it is more efficient.

► C is POP(procedure oriented programming) whereas c++ is OOP(Object oriented programming)

3. Basic Structure & Syntax

Programming in C involves following a basic structure throughout. Here’s what it can be broken down to.

• Pre-processor commands

• Functions

• Variables

• Statements

• Expressions

• Comments

Pre-processor commands

Pre-processor commands are commands which tell our program that before its execution, it must include the file name mentioned in it because we are using some of the commands or codes from this file. They add functionalities to a program. One example could be,

#include <stdio.h>

# include<conio.h>

#include <math.h>

We include math.h to be able to use some special functions like power and absolute. #include is how we include them into our programs. Detailed explanations of everything else in the structure will follow in the later part of the tutorial.

Header files

· Collection of predefined/built in functions developed

· It is always declares on heading side of program hence it is called header file

· It is identified with the extension(.h)

· It gets installed while installing IDE(integrated development environment)

· It stores functions as per their categories hence they are called library

C logo

Syntax

An example below shows how a basic C program is written.

Declaration of header file   		//name of the header files of which functions are been used
main()	                           /*it is called main function which stores the execution of program*/
{	                                              //start of the program
              //program statements
}                                                //end of the program
  • Here, the first line is a pre-processor command including a header file stdio.h.
  • C ignores empty lines and spaces.
  • There is a main() function then, which should always be there.
  • "//(Single Line)" || "/* */ (Multiline Comment)"
  • This a comment in C language, meaning it will be ignored by the compiler while compiling but it will remain in your source code for other developers to see what does the code do.
  • In the souce code Notes/ Imp points are written like this refer that.

A C program is made up of different tokens combined. These tokens include:

  • Keywords
  • Identifiers
  • Constants
  • String Literal
  • Symbols

Keywords

Keywords are reserved words that can not be used elsewhere in the program for naming a variable or a function. They have a specific function or task and they are solely used for that. Their functionalities are pre-defined. One such example of a keyword could be return which is used to build return statements for functions. Other examples are auto, if, default, etc. Whenever we write any keyword in IDE their colour slightly changes and it looks different from other variables or functions for example in turbo c all keywords are turns into white colour .

Identifiers

Identifiers are names given to variables or functions to differentiate them from one another. Their definitions are solely based on our choice but there are a few rules that we have to follow while naming identifiers. One such rule says that the name can not contain special symbols such as @, -, *, <, etc.

C is a case-sensitive language so an identifier containing a capital letter and another one containing a small letter in the same place will be different. For example, the three words: Code, code, and cOde can be used as three different identifiers.

Rules for naming identifier-

  1. One should not name any identifier starting with numeric value or symbol. It should start only with underscore or alphabet
  2. They should not contain space
  3. Giving logical names is recommended as per our program

Constants & Variables in C

Variables are containers for storing data values. In C, there are different types of variables. All these are mentioned in source code download refer that

Constants are very similar to a variable and they can also be of any data type. The only difference between a constant and a variable is that a constant’s value never changes. We will see constants in more detail in the upcoming tutorial.

String Literal

String literals or string constants are a sequence of characters enclosed in double quotation marks. For example, “This is a string literal!” is a string literal. C method printf() utilizes the same to format the output.


4. Quick Reference on fundamental concepts of C language:

C icon

Variables:

► Variables are storage locations in a program that hold values.

► They have a data type (e.g., int, float, char) that determines the type of data they can hold.

► Variables can be assigned values and used to perform computations.

Data Types:

► Data types define the type of data that a variable can hold.

► Examples include int (for integers), float (for floating-point numbers), char (for characters), and more.

► User-defined data types like structures and unions can also be created.

Loops:

► Loops are control structures that allow the execution of a block of code repeatedly as long as a specified condition is met.

► Common types of loops in C are for, while, and do-while.

Conditional Statements:

► Conditional statements allow the execution of different code blocks based on certain conditions.

► The primary conditional statement in C is if-else, which allows for branching based on true/false conditions.

Arrays:

► Arrays are collections of elements of the same data type under one name.

► Elements in an array are accessed using an index, which starts from 0.

► Arrays are useful for storing and manipulating sets of data.

Strings:

► In C, a string is an array of characters terminated by a null character \0.

► String manipulation functions like strlen(), strcpy(), and strcat() are used for handling strings.

Functions:

► Functions are blocks of code that perform a specific task.

► They provide modularity, allowing code to be organized into reusable pieces.

► Functions can have parameters (input) and return a value (output).

Pointers:

► Pointers are variables that store memory addresses.

► They allow direct access to memory locations and facilitate dynamic memory allocation.

Structures and Unions:

► Structures allow the grouping of different types of variables under a single name.

► Unions, like structures, can hold multiple variables, but they share the same memory location.

File Handling:

► C provides functions to interact with files for reading from and writing to external storage.

► Operations like opening, closing, reading, and writing files are supported.

Preprocessor Directives:

► These are special commands preceded by a # symbol. ► They are used to include files, perform conditional compilation, and define constants.

Dynamic Memory Allocation:

► C provides functions like malloc(), calloc(), and realloc() to allocate memory dynamically during program execution.

► This is useful for managing memory for data structures like linked lists and trees.

Bitwise Operators:

► C supports bitwise operations (AND, OR, XOR, etc.) that work at the bit level.

► These operations are useful in tasks involving low-level hardware manipulation.

These are some of the foundational concepts in the C programming language. Understanding these concepts is crucial for writing effective and efficient C programs.



C Programming Language Quick Reference

Welcome to the C Programming Language Quick Reference! This README.md file serves as a concise guide to important topics in C programming, providing simple code examples with comments for better understanding.

Table of Contents

  1. Introduction
  2. Variables and Data Types
  3. Control Flow
  4. Functions
  5. Arrays
  6. Pointers
  7. Structures
  8. File Handling

Introduction

C is a powerful and versatile programming language known for its efficiency and low-level control. It is widely used in systems programming, embedded systems, and software development.

Variables and Data Types

#include <stdio.h>

int main() {
    // Integer variable
    int num = 10;

    // Floating-point variable
    float floatNum = 3.14;

    // Character variable
    char character = 'A';

    // Display values
    printf("Integer: %d\n", num);
    printf("Float: %.2f\n", floatNum);
    printf("Character: %c\n", character);

    return 0;
}

Control Flow

#include <stdio.h>

int main() {
    // If statement
    int x = 10;
    if (x > 0) {
        printf("Positive number\n");
    } else if (x < 0) {
        printf("Negative number\n");
    } else {
        printf("Zero\n");
    }

    // Switch statement
    char grade = 'B';
    switch (grade) {
        case 'A':
            printf("Excellent\n");
            break;
        case 'B':
            printf("Good\n");
            break;
        default:
            printf("Average\n");
    }

    return 0;
}

Functions

#include <stdio.h>

// Function declaration
int add(int a, int b) {
    return a + b;
}

int main() {
    // Function call
    int result = add(5, 7);
    printf("Sum: %d\n", result);

    return 0;
}

Arrays

#include <stdio.h>

int main() {
    // Array declaration
    int numbers[5] = {1, 2, 3, 4, 5};

    // Accessing array elements
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Pointers

#include <stdio.h>

int main() {
    // Pointer declaration
    int num = 10;
    int *ptr = &num;

    // Accessing variable through pointer
    printf("Value of num: %d\n", *ptr);

    return 0;
}

Structures

#include <stdio.h>

// Structure declaration
struct Point {
    int x;
    int y;
};

int main() {
    // Creating and initializing a structure variable
    struct Point p1 = {2, 4};

    // Accessing structure members
    printf("Coordinates: (%d, %d)\n", p1.x, p1.y);

    return 0;
}

File Handling

#include <stdio.h>

int main() {
    // File handling
    FILE *file = fopen("example.txt", "w");
    if (file != NULL) {
        fprintf(file, "Hello, C Programming!");
        fclose(file);
    } else {
        printf("Error opening the file\n");
    }

    return 0;
}

Refer my Source Code for better & clear understanding of these concepts

About

WORK IN PROGRESS...This My Coding Notes. I have made this to help others to grasp the basic concept of C as well as as revise VERY EASILY & systematically.If anyone wants to contribute they can

License:MIT License


Languages

Language:C 100.0%