Skip to main content

Fundamental Topic in C Language

Sure! Below is a list of some fundamental C language topics along with detailed examples for each topic:

1. Hello World Program:
   Example:
   ```c
   #include <stdio.h>
   
   int main() {
       printf("Hello, World!\n");
       return 0;
   }
   ```
   This is a basic C program that prints "Hello, World!" to the console.

2. Variables and Data Types:
   Example:
   ```c
   #include <stdio.h>
   
   int main() {
       int age = 25;
       float height = 5.9;
       char grade = 'A';
   
       printf("Age: %d\n", age);
       printf("Height: %.1f\n", height);
       printf("Grade: %c\n", grade);
   
       return 0;
   }
   ```
   This program declares variables of different data types (int, float, char) and prints their values.

3. Input and Output:
   Example:
   ```c
   #include <stdio.h>
   
   int main() {
       char name[50];
       printf("Enter your name: ");
       scanf("%s", name);
   
       printf("Hello, %s!\n", name);
       return 0;
   }
   ```
   This program takes user input (name) and prints a personalized greeting.

4. Conditional Statements (if-else):
   Example:
   ```c
   #include <stdio.h>
   
   int main() {
       int num;
       printf("Enter a number: ");
       scanf("%d", &num);
   
       if (num > 0) {
           printf("Positive number\n");
       } else if (num < 0) {
           printf("Negative number\n");
       } else {
           printf("Zero\n");
       }
   
       return 0;
   }
   ```
   This program checks if a number is positive, negative, or zero using if-else statements.

5. Loops (for, while, do-while):
   Example (for loop):
   ```c
   #include <stdio.h>
   
   int main() {
       int i;
       for (i = 1; i <= 5; i++) {
           printf("%d ", i);
       }
       printf("\n");
   
       return 0;
   }
   ```
   This program uses a for loop to print numbers from 1 to 5.

6. Arrays:
   Example:
   ```c
   #include <stdio.h>
   
   int main() {
       int numbers[5] = {1, 2, 3, 4, 5};
       int i;
   
       printf("Array elements: ");
       for (i = 0; i < 5; i++) {
           printf("%d ", numbers[i]);
       }
       printf("\n");
   
       return 0;
   }
   ```
   This program declares and prints elements of an integer array.

7. Functions:
   Example:
   ```c
   #include <stdio.h>
   
   int add(int a, int b) {
       return a + b;
   }
   
   int main() {
       int num1 = 5, num2 = 3, result;
       result = add(num1, num2);
       printf("Sum: %d\n", result);
   
       return 0;
   }
   ```
   This program defines a function to add two numbers and uses it in the main function.

8. Pointers:
   Example:
   ```c
   #include <stdio.h>
   
   int main() {
       int num = 10;
       int *ptr;
   
       ptr = &num;
   
       printf("Value of num: %d\n", num);
       printf("Address of num: %p\n", &num);
       printf("Value of ptr: %p\n", ptr);
       printf("Value pointed by ptr: %d\n", *ptr);
   
       return 0;
   }
   ```
   This program demonstrates pointers by declaring a variable, a pointer to that variable, and printing their values and addresses.

9. Structures:
   Example:
   ```c
   #include <stdio.h>
   
   struct Student {
       char name[50];
       int age;
       float marks;
   };
   
   int main() {
       struct Student s;
       printf("Enter name: ");
       scanf("%s", s.name);
       printf("Enter age: ");
       scanf("%d", &s.age);
       printf("Enter marks: ");
       scanf("%f", &s.marks);
   
       printf("Student Details:\n");
       printf("Name: %s\n", s.name);
       printf("Age: %d\n", s.age);
       printf("Marks: %.2f\n", s.marks);
   
       return 0;
   }
   ```
   This program defines a structure for a student and stores and prints their details.

10. File Handling (Reading and Writing):
    Example (writing to a file):
    ```c
    #include <stdio.h>
    
    int main() {
        FILE *file = fopen("example.txt", "w");
        if (file != NULL) {
            fprintf(file, "This is a file handling example.\n");
            fclose(file);
            printf("Data written to the file successfully.\n");
        } else {
            printf("Failed to open the file.\n");
        }
    
        return 0;
    }
    ```
    This program demonstrates writing data to a file.

These examples cover various fundamental concepts of the C programming language. As you progress in your C programming journey, you will encounter more advanced topics and techniques to enhance your skills further.

Comments

Popular posts from this blog

Tokens, Identifiers, Data Types, Sequence Control, Subprogram Control, Arrays, Structures, Union, String, Pointers, Functions, File Handling, Command Line Argumaents, Preprocessors in C with example

 Let's discuss each concept briefly and provide examples for better understanding: 1. Tokens: Tokens are the smallest building blocks in C programming. They include keywords, identifiers, constants, strings, operators, and punctuators. Example: ```c #include <stdio.h> int main() {     int num = 42;  // 'int', 'main', 'return', '42', '=', ';' are tokens     printf("Hello, World!");  // 'printf', '(', ')', 'Hello, World!', ';', are tokens     return 0;  // 'return', '0', ';' are tokens } ``` 2. Identifiers: Identifiers are names used to identify variables, functions, or other user-defined entities. Example: ```c int age = 30;  // 'age' is an identifier (variable name) void displayMessage() {  // 'displayMessage' is an identifier (function name)     // function body } ``` 3. Data Types: Data types define the type of data that can be stored in ...

Web Programming: HTML, DHTML, XML, Scripting, Java, Servlets, Applets

 Web programming encompasses various technologies and concepts used to develop web applications. Let's explore each of them in detail: 1. HTML (Hypertext Markup Language): HTML is the standard markup language used to create the structure and content of web pages. It uses tags to define elements like headings, paragraphs, images, links, forms, etc. Example: ```html <!DOCTYPE html> <html> <head>     <title>My Web Page</title> </head> <body>     <h1>Hello, World!</h1>     <p>This is a paragraph.</p>     <img src="image.jpg" alt="Image">     <a href="https://www.example.com">Visit Example</a> </body> </html> ``` 2. DHTML (Dynamic HTML): DHTML is a combination of HTML, CSS, and JavaScript that allows web pages to become more dynamic and interactive. Example (DHTML with JavaScript): ```html <!DOCTYPE html> <htm...

Place holder and control character in c language

 In the C programming language, placeholders and control characters are used to format and control the output of text in console-based programs. They are special characters or sequences of characters that have specific meanings. Here are the placeholders and control characters commonly used in C: 1. Placeholders:    - %d: Used to display signed integers.      Example: printf("The value is %d", 10);    - %u: Used to display unsigned integers.      Example: printf("The value is %u", 10);    - %f: Used to display floating-point numbers.      Example: printf("The value is %f", 3.14);    - %c: Used to display characters.      Example: printf("The character is %c", 'A');    - %s: Used to display strings (sequence of characters).      Example: printf("The string is %s", "Hello");    - %p: Used to display memory addresses (pointers)...