10BC0 GitHub - SOMNATH0904/C-Programming: This Repository contains the codes of C Programming. · GitHub
[go: up one dir, main page]

Skip to content

SOMNATH0904/C-Programming

Repository files navigation

C Programming

Welcome to the "C Programming Concepts and Basic Codes" repository! This repository is designed to help beginners and intermediate learners understand fundamental concepts of C programming through concise explanations and practical code examples.

Table of Contents

  1. Introduction
  2. Setup and Compilation
  3. Basic C Programming Concepts
  4. Basic Codes
  5. Contributing
  6. License
  7. Contact

Introduction

This repository contains a collection of C programming concepts and basic code examples to help you grasp the foundational aspects of C. Each concept is accompanied by a brief explanation and sample code to illustrate its usage.

Setup and Compilation

  1. Clone the repository:

    git clone https://github.com/your-username/C-Programming.git
    cd C-Programming
  2. Compile and run a C program:

    Use a C compiler such as gcc to compile the .c files:

    gcc filename.c -o output
    ./output

Basic C Programming Concepts

Variables and Data Types

  • Variables: Used to store data values.

  • Data Types: Common data types include int, float, char, and double.

    #include <stdio.h>
    
    int main() {
        int age = 25;
        float height = 5.9;
        char grade = 'A';
        printf("Age: %d, Height: %.1f, Grade: %c\n", age, height, grade);
        return 0;
    }

Control Structures

  • Conditional Statements: if, else if, and else.

  • Loops: for, while, and do-while.

    #include <stdio.h>
    
    int main() {
        int num = 10;
        if (num > 0) {
            printf("Positive\n");
        } else {
            printf("Negative\n");
        }
    
        for (int i = 0; i < 5; i++) {
            printf("Loop: %d\n", i);
        }
    
        return 0;
    }

Functions

  • Defining Functions: Using return_type function_name(arguments).

  • Arguments and Return Values:

    #include <stdio.h>
    
    int add(int a, int b) {
        return a + b;
    }
    
    int main() {
        int sum = add(5, 3);
        printf("Sum: %d\n", sum);
        return 0;
    }

Pointers

  • Pointer Variables: Pointers store memory addresses.

  • Dereferencing: Access the value at the memory address.

    #include <stdio.h>
    
    int main() {
        int x = 10;
        int *ptr = &x;
    
        printf("Value of x: %d\n", x);
        printf("Address of x: %p\n", ptr);
        printf("Value at address: %d\n", *ptr);
    
        return 0;
    }

Arrays and Strings

  • Arrays: Contain multiple elements of the same data type.

  • Strings: Arrays of characters.

    #include <stdio.h>
    
    int main() {
        int arr[5] = {1, 2, 3, 4, 5};
        for (int i = 0; i < 5; i++) {
            printf("%d ", arr[i]);
        }
    
        char str[] = "Hello, World!";
        printf("\n%s\n", str);
    
        return 0;
    }

Structures

  • Defining Structures: Group variables of different data types.

    #include <stdio.h>
    
    struct Student {
        char name[50];
        int age;
        float gpa;
    };
    
    int main() {
        struct Student s1 = {"Alice", 20, 3.8};
        printf("Name: %s, Age: %d, GPA: %.2f\n", s1.name, s1.age, s1.gpa);
        return 0;
    }

File Handling

  • Reading and Writing Files: Use functions like fopen(), fclose(), fread(), fwrite().

    #include <stdio.h>
    
    int main() {
        FILE *file = fopen("example.txt", "w");
        if (file != NULL) {
            fprintf(file, "Hello, File Handling in C!\n");
            fclose(file);
        } else {
            printf("File could not be opened.\n");
        }
        return 0;
    }

Dynamic Memory Allocation

  • Allocating Memory: Use malloc(), calloc() 73BD , realloc(), and free().

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        int *ptr;
        ptr = (int *)malloc(5 * sizeof(int));
    
        for (int i = 0; i < 5; i++) {
            ptr[i] = i + 1;
            printf("%d ", ptr[i]);
        }
    
        free(ptr);
        return 0;
    }

Basic Codes

In this section, you will find various basic C code examples categorized by their functionality and concept. These examples are meant to reinforce the concepts covered and provide hands-on practice.

Contributing

Contributions are welcome! If you would like to contribute to this repository, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature-branch).
  5. Create a new Pull Request.

License

This repository is licensed under the MIT License. See the LICENSE file for more information.

Contact

For any questions, suggestions, or feedback, please feel free to contact me:


Happy Coding!

About

This Repository contains the codes of C Programming.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

0