C Basic Commands

By | February 14, 2023

C is a high-level programming language that was developed in the 1970s and is still widely used today. It is used to develop a wide range of applications, including system software, games, and desktop and mobile applications. Here are some of the most commonly used C commands:

  1. printf: This function is used to display output on the screen.
    Example –

    #include <stdio.h>
    
    int main() {
       printf("Hello, World!");
       return 0;
    }
    
    

     

  2. scanf: This function is used to read input from the keyboard.
    Example –

    #include <stdio.h>
    
    int main() {
       int a;
       printf("Enter a value: ");
       scanf("%d", &a);
       printf("You entered: %d", a);
       return 0;
    }
    
    
  3. main: This is the starting point of a C program. It returns an integer value to indicate the status of the program’s execution.
    Example –

    #include <stdio.h>
    
    int main() {
       printf("This is the starting point of the program.");
       return 0;
    }
    
    
  4. if: This statement is used to execute a block of code conditionally.
    Example –

    #include <stdio.h>
    
    int main() {
       int a = 5;
       if (a > 0) {
          printf("a is positive.");
       }
       return 0;
    }
    
    
  5. for: This loop is used to repeat a block of code a specified number of times.
    Example –

    #include <stdio.h>
    
    int main() {
       int i;
       for (i = 1; i <= 10; i++) {
          printf("%d ", i);
       }
       return 0;
    }
    
    
  6. while: This loop is used to repeat a block of code as long as a condition is true.
    Example –

    #include <stdio.h>
    
    int main() {
       int i = 1;
       while (i <= 10) {
          printf("%d ", i);
          i++;
       }
       return 0;
    }
    
    
  7. do-while: This loop is similar to a while loop, but the block of code is executed at least once.
    Example –

    #include <stdio.h>
    
    int main() {
       int i = 1;
       do {
          printf("%d ", i);
          i++;
       } while (i <= 10);
       return 0;
    }
    
    
  8. break: This statement is used to exit a loop early.
    Example –

    #include <stdio.h>
    
    int main() {
       int i;
       for (i = 1; i <= 10; i++) {
          if (i == 5) {
             break;
          }
          printf("%d ", i);
       }
       return 0;
    }
    
    
  9. continue: This statement is used to skip the current iteration of a loop and move on to the next one.
    Example –

    #include <stdio.h>
    
    int main() {
       int i;
       for (i = 1; i <= 10; i++) {
          if (i % 2 == 0) {
             continue;
          }
          printf("%d ", i);
       }
       return 0;
    }
    
    
  10. switch: This statement is used to choose between multiple blocks of code to execute based on a value.
    Example –

    #include <stdio.h>
    
    int main() {
       int a = 2;
       switch (a) {
          case 1:
             printf("a is 1.");
             break;
          case 2:
             printf("a is 2.");
             break;
          default:
             printf("a is neither 1 nor 2.");
       }
       return 0;
    }
    
    

These are just a few of the many commands available in the C programming language. If you are new to C, it’s a good idea to start by learning these basics before diving into more advanced topics.

 

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.