Basic C Program Examples for Beginners (2023)

Many interviews generally include questions about C programs. These programs can be used to handle files, arrays, strings, pointers, linked lists, and other fundamental data types.

Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is quite popular.
Since the UNIX operating system was created using C, the two are closely related.

  • Calculate the Percentage of 5 Subjects
    It is simple to calculate the percentage of five subjects, all you need to do is add all of the marks and multiply that total by 100. Then divide that by the total number of marks a student is permitted to receive. You will ultimately receive the results as a percentage of the 5 subjects.

    Input:

    50 20 30 40 80

    Output:

    Enter marks of five subjects:Total marks = 220.00Average marks = 44.00Percentage = 44.00
  • Convert infix to postfix
    Any operation may be written as an infix, prefix, or postfix, in this C language tutorial, we’ll see how to do this. Two approaches have been covered: the array-based stack approach and the struct-based stack approach.

    Input:

    ((p+(q*r))-s)

    Output:

    pqr*+s-
  • Odd Even Program
    A number is taken into consideration even though it may be split by two equally. The remainder that is not precisely divisible by 2 is known as an odd number. Simply expressed, odd numbers adopt the form n = 2k+1, whereas even numbers take the form n = 2k. Each integer will be made up of either even or odd integers.

    Input:

    10

    Output:

    Even

    Input:

    15

    Output:

    Odd
  • Find area of a circle
    Everyone knows the basics of mathematics,
    For finding the area of a circle, we can get this by performing some

    Area = Pie * Radius * Radius

    Where the value of pie = 3.14 and the radius will be given by the user.

    (Video) C Programming Tutorial for Beginners

    Input:

    10

    Output:

    The radius of the circle is 10The area of a given circle is 314.000000
  • Convert Celsius to Fahrenheit
    In the realm of programming, we may easily convert between celsius and fahrenheit by carrying out a few simple mathematical operations.
    The formula to change from celsius to fahrenheit is shown below.

    Formula

    Fahrenheit Temperature = (1.8 * celsius) + 32;

    Input:

    40

    Output:

    104
  • Add two numbers
    In the C programming language, the phrase "sum of two integers" refers to adding two integer values and printing the addition of two integer values and the printing of the result. To combine two numbers, all we need to do is utilize the "+" operator. In the link above, we have also explained several techniques.

    Input:

    10 20

    Output:

    30

    Input:

    20 20

    Output:

    40
  • Binary Search program in C
    In the C programming language, a sorted array may be searched using a technique called binary search by continually halving the search interval. In order to reduce the time complexity to O, binary search makes use of the fact that the array is sorted (LogN).
    This approach always searches the center of an array for the element.
    There are two ways to create a binary search program in C:

    • Iterative Method
    • Recursive Method

    Input:

    [11, 14, 25, 30, 40, 45]

    40
    Output:

    Element is present at position 4
  • Priority Scheduling program in C
    The algorithms prioritize the processes that must be carried out and schedule them accordingly. A higher-priority process will receive CPU priority first, and this will continue until all of the processes are finished. Due to the process’ greater priority, the Priority Scheduling Algorithm is in charge of transferring it from the ready queue to the work queue.

    (Video) First C Program | Easiest way to Understand C Programming

    Input:

    {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}}

    Output:

    Order in which processes get executed 1 3 2

    Processes Burst time Waiting time Turn around time
    1 10 0 10
    3 8 10 18
    2 5 18 23

    Average waiting time = 9.33333
    Average turn around time = 17

  • Find roots of quadratic equation
    A quadratic has the formula y = ax^2 + bx + c, where a, b, and c are all integers and a must be 0. An illustration of a quadratic equation is 3×2 + 3x + 1.
    Let’s talk about what a quadratic equation’s roots are and how to get them from the equation.
    The discriminant of a quadratic equation is the expression b2 – 4ac. It describes the roots’ character.

    Input:

    a = 2.4 b = 4 c = 506

    Output:

    root1 = -0.87+1.30i and root2 = -0.87-1.30i
  • Convert binary numbers to decimal number
    In order to convert a binary number to a decimal number, we must multiply each digit of the binary number starting at 0 by powers of 2 and then sum the results to obtain the decimal number.

    Input:
    111
    Output:

    7

    Input:

    0101

    Output:

    5
  • Ascending order program in C language
    Different kinds of reasoning can be used to sort things. For the sake of simplicity, we shall employ a tried-and-true technique. We choose a certain element from an array, compare it to other elements, and then place it appropriately to sort the array.

    Input:

    Arr[50, 20, 100, 25, 36]

    Output:

    20 25 36 50 100

    Input:

    (Video) C Programming Tutorial 1 - Intro to C

    Arr[5, 8, 2, 1, 6]

    Output:

    1 2 5 6 8
  • SJF Scheduling program in C
    One of the CPU scheduling strategies is to select the shortest jobs first. It is an algorithm whose burst time-dependent procedure. In other words, a process with a lower burst time executes first. The shortest job next (SJN) is another name for the shortest job first (SJF). For instance, we have four processes, P1, P2, P3, and P4, with burst times of 5, 7, 6, and 2. Process P4 will now be run first since it has a shorter burst period. Processes P1, P3, and P2 will then be carried out in that order.

    Output:

    enter the no of processes: 2the arrival time for process P1: 10the burst time for process P1: 5the arrival time for process P2: 6the burst time for process P2 : 3P[10] | -22765 | -32764

    average waiting time = -16382.000000
    average turnaround time = -11382.500000

  • Menu Driven program for all operations on the doubly linked list in C.
    In the menu-driven program in C, the user is given a variety of options, and some functionality is available based on these choices. These programs are used to give users the desired functions and a straightforward yet effective user interface.

    Output:

     1 To see list 2 For insertion at starting 3 For insertion at end 4 For insertion at any position 5 For deletion of first element 6 For deletion of last element 7 For deletion of element at any position 8 To exitEnter Choice :2Enter number to be inserted: 4 1 To see list 2 For insertion at starting 3 For insertion at end 4 For insertion at any position 5 For deletion of first element 6 For deletion of last element 7 For deletion of element at any position 8 To exitEnter Choice :3Enter number to be inserted: 11 1 To see list 2 For insertion at starting 3 For insertion at end 4 For insertion at any position 5 For deletion of first element 6 For deletion of last element 7 For deletion of element at any position 8 To exitEnter Choice :3Enter number to be inserted: 9 1 To see list 2 For insertion at starting 3 For insertion at end 4 For insertion at any position 5 For deletion of first element 6 For deletion of last element 7 For deletion of element at any position 8 To exitEnter Choice:1Data = 4Data = 11Data = 9
  • FCFS Scheduling program in C
    The CPU assigns work using a non-preemptive scheduling mechanism called First Come First Serve. According to the request that was made initially, as the name implies, the tasks are given the highest priority.

    Output:

    Processes Burst time Waiting time Turn around time 1 10 0 10 2 5 10 15 3 8 15 23

    Average waiting time = 8.33333
    Average turnaround time = 16

  • Swap two Numbers
    When two numbers are changed, two variables’ values are also changed. Think about the variables var1 and var2 you have. Var1 is valued at 20, whereas Var2 is valued at 40. As a result, after swapping, var1 and var2 will have values of 40 and 20, respectively.
    We have discussed different methods such as:

    1. Swapping Two Numbers Using a Third Variable
    2. Swapping Two Numbers Using Without Using Third Variable
    3. Swapping Function in C
    4. Swap two numbers using pointers in C
    5. Swap Two Numbers Using Bitwise XOR

    Input:

    a = 20 b = 40

    Output:

    a = 40 b = 20
  • Compound Interest Program
    To calculate the amount of interest given by:
    Amount of interest = P(1+R/100)t
    P = Principal amount
    R = Rate of interest
    T = The Time span
    Compound Interest = Amount – P

    Input:

    Principal (amount): 1200 Time: 2 Rate: 5.4

    Output:

    (Video) C Programming For Beginners | Learn C Programming | C Tutorial For Beginners | Edureka

    Compound Interest = 133.099243
  • Radix Sort Program
    Each digit is sorted in the Radix sort algorithm from least to greatest significance. In base 10, radix sort would first sort by the digits in the place of one, then by the digits in the place of ten, and so on. Radix sort uses counting sort as a subroutine to order the data in each digit position.

    Input:

    312 42 635 11 8 783 954 777 

    Output:

    8 11 42 312 635 777 783 954 

    Input:

    50 40 30 10 96 100

    Output:

    10 30 40 50 96 100
  • Bubble sort on Linked List
    The bubble sort’s fundamental principle is to compare each member of an array individually until they are sorted in ascending order, a process known as bubble busting. Instead of shifting the entire array when an element has to be moved, the impacted element is just moved.

    Input:

    Linked list before sorting8 -> 2 -> 4 -> 1 -> 5

    Output:

    Linked list after sorting1 -> 2 -> 4 -> 5 -> 8
  • Round-Robin Scheduling
    With round-robin scheduling, each job receives an equal amount of CPU time. In its most basic form, jobs are placed in a circular queue, where they are moved to the back of the queue when their allotted CPU time runs out and to the front when a new task is added.

    Output:

    Total number of processes in the system: 4Enter the Arrival and Burst time of the Process[1] Enter Arrival time: 0 Enter Burst time: 5 Enter the Arrival and Burst time of the Process[2] Enter Arrival time: 1 Enter Burst time: 4 Enter the Arrival and Burst time of the Process[3] Enter Arrival time: 2 Enter Burst time: 2 Enter the Arrival and Burst time of the Process[4] Enter Arrival time: 4 Enter Burst time: 1 Enter the Time Quantum for the process: 2
  • Reverse a Number
    Using the modulo division (%) function, find the final digit of the provided integer, and then save the result in the last digit variable, for example, last digit=number%10. Reversed is equal to reversed times 10 plus the final digit, therefore reversed is reversed times 10 plus the last digit. Like numbered/10, multiply the number by 10.

    Input:

    6789

    Output:

    9876
  • Conclusion
    This blog covers all the basic C Programs. You can just brush up on your basic concepts through this article. We hope this article will help you to enhance your knowledge.

    FAQs

    How to write C program for beginners? ›

    The printf() function is defined in stdio. h . int main() The main() function is the entry point of every program in c language.
    ...
    To write the first c program, open the C console and write the following code:
    1. #include <stdio. h>
    2. int main(){
    3. printf("Hello C Language");
    4. return 0;
    5. }

    Can I learn C in 10 days? ›

    Of course, You can easily learn C in 10 days. But you should put your hardwork in practising more and more problems. I will come to that again but first I will describe how to learn in 10 days. Take any online course because you want to learn in just 10 days and online MOOCs play an important role.

    How can I learn C fast? ›

    Ways You Can Learn Programming Faster
    1. Always Look for the Example Code. Figuring out how to program is about code. ...
    2. Just Don't Only Read the Example Code - Run It. ...
    3. Compose Your Own Code as Quickly as Time Permits. ...
    4. Figure Out How to Use a Debugger. ...
    5. Search Out More Sources.
    Dec 30, 2020

    How can I practice C programming at home? ›

    Practice sites
    1. Coderbyte. When you begin to develop your coding skills, you may be unsure what to practice first. ...
    2. HackerRank. ...
    3. Codewars. ...
    4. CodinGame. ...
    5. CodeChef. ...
    6. Project Euler. ...
    7. TopCoder. ...
    8. SPOJ.
    Aug 16, 2021

    Is C hard for beginners? ›

    C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

    Is C easy for beginners? ›

    C programming language uses blocks to separate pieces of code performing different tasks. This helps make programming easier and keeps the code clean. Thus, the code is easy to understand even for those who are starting out.

    What are the real life examples of C programming? ›

    The following are some of the systems that are used by millions and are programmed in the C language.
    • Microsoft Windows. Microsoft's Windows kernel is developed mostly in C, with some parts in assembly language. ...
    • Linux. ...
    • Mac. ...
    • Mobile. ...
    • Databases. ...
    • 3D Movies. ...
    • Embedded Systems. ...
    • Portability and Efficiency.

    Is C or C++ easy? ›

    C++ was designed to be easier to use and to allow programmers to make efficient use of computer resources. C++ also has some similarities with C, but there are some important differences. C++ is a good choice for experienced programmers who want to learn a new programming language.

    What is the first step in C program? ›

    Preprocessor:

    Preprocessing is the first stage of C Build process in which all the preprocessor directives are evaluated. The input file for this stage is *. c file.

    What are the 5 structures of C programming? ›

    Sections of the C Program
    • Documentation.
    • Preprocessor Section.
    • Definition.
    • Global Declaration.
    • Main() Function.
    • Sub Programs.
    Oct 9, 2022

    Why is C so hard to learn? ›

    C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

    Is C the hardest language to learn? ›

    Another reason why C is considered relatively easy, and why most beginners find C as a good starting point, is because it starts from the very basics and builds the foundation for learning all other, more complex programming languages. Further, C is also one of the fastest languages in terms of execution speed.

    Which is better C or Python? ›

    C is a faster language compared to Python as it is compiled. Python programs are usually slower than C programs as they are interpreted. In C, the type of the various variables must be declared when they are created, and only values of those particular types must be assigned to them.

    Is C harder than Python? ›

    The syntax of a C program is harder than Python. Syntax of Python programs is easy to learn, write and read. In C, the Programmer has to do memory management on their own. Python uses an automatic garbage collector for memory management.

    Is it possible to learn C in a week? ›

    The Basic Syntax: A Few Days – 1 Week

    I think it's safe to say you can learn this within the first few days to a week of picking up the language. The syntax for C is actually pretty simplistic. It's the easier part of picking up the language.

    How long does it take to understand C? ›

    How Long Does It Take to Learn C? It can take a few weeks to a few months to learn C. Each programmer has their own specific timeline in learning the programming language, especially if they are an absolute beginner. Therefore there is no one-size-fits-all for learning how to code using C.

    Which app is best for C programming beginners? ›

    What Are the Best Coding Apps for Beginners?
    • Enki.
    • CodeHub.
    • Programming Hub.
    • Grasshopper.
    • Encode.
    • Mimo.
    • Programming Hero.
    • Sololearn.
    Oct 11, 2022

    Can I learn C basics in one day? ›

    No, you cannot learn C in one day. (I assume you have not learned programming, and your exam is about programming in C; If you did have a lot of programming experience -e.g. in lower level languages semantically similar to C, like Ada, PL/1, Algol, Rust or Pascal- you might learn C in a few days).

    Can I learn C program in one day? ›

    You can't do it, at least, not in the sense that I think you mean it. To learn C programming “fully” without prior programming knowledge requires considerable practice and experience using C. This could take a year or more.

    What's the hardest coding language? ›

    Haskell. The language is named after a mathematician and is usually described to be one of the hardest programming languages to learn. It is a completely functional language built on lambda calculus.

    Can I learn C in 3 months? ›

    You can learn C in a three months , if you are totally dedicated to it . But only C does not raise you to greater heights ,you also have to learn DATA Structure and Algorithms .

    How do I start coding at zero level? ›

    How to Start Coding
    1. Figure out why you want to learn to code.
    2. Choose which coding language you want to learn first.
    3. Take online courses.
    4. Watch video tutorials.
    5. Read books and ebooks.
    6. Use tools that make learning to code easier.
    7. Check out how other people code.
    8. Complete coding projects.
    Jun 29, 2022

    Is C still being used? ›

    Yet despite its long history, C remains one of the top "most-used" programming languages in many "popular programming languages" surveys. For example, check out the TIOBE Index, which tracks the popularity of different programming languages.

    What are the key words in C? ›

    C reserved keywords
    autoelselong
    caseexternreturn
    charfloatshort
    constforsigned
    continuegotosizeof
    4 more rows

    Where is C mostly used? ›

    That's why the C language is widely used for developing system software, application software, and embedded systems. The C programming language has been highly influential, and many other languages have been derived from it. For example, C++ and Java are two popular modern dialects of C.

    Who uses C language today? ›

    There are many professionals who use C. Software developers, senior programmers, quality analysts, and programming architects all use it. It is widely used for developing desktop applications, embedded systems, and building system applications.

    What is the most fun programming language? ›

    Python is often thought to be one of the most fun programming languages to learn. There are many great reasons why this is the case. Python is written using a syntax that is extremely clear, readable, and accessible.

    What are the 4 types of coding? ›

    Common styles are imperative, functional, logical, and object-oriented languages. Programmers can choose from these coding language paradigms to best-serve their needs for a specific project.

    What are the 5 basic programming languages? ›

    Here are five basic programming languages to explore:
    • Python. This is a high-level and general-purpose language that focuses on code readability. ...
    • Java. ...
    • JavaScript. ...
    • C and C++ ...
    • SQL.
    Nov 24, 2021

    Do I need to learn C before C++? ›

    There is no need to learn C before learning C++. They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own. Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.

    What's better Python or C++? ›

    C++ is faster than Python because it is statically typed, which leads to a faster compilation of code. Python is slower than C++, it supports dynamic typing, and it also uses the interpreter, which makes the process of compilation slower.

    Why is C still used instead of C++? ›

    Picking C over C++ is a way for developers and those who maintain their code to embrace enforced minimalism and avoid tangling with the excesses of C++. Of course, C++ has a rich set of high-level features for good reason.

    How do you code Hello? ›

    Basic example: Creating and running “Hello World”
    1. Create the following C program and name the source file hello.c : #include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; }
    2. Compile the program: ...
    3. Run the program by entering the following command: ./hello.

    How do you explain C code? ›

    C is an imperative procedural language, supporting structured programming, lexical variable scope and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

    What are the 3 types of functions in C? ›

    Different Ways of Calling a Function
    • C Programming Functions Without Arguments and Return Value. ...
    • C programming Functions With No Arguments But has a Return Value. ...
    • C programming Functions With Arguments But No Return Value. ...
    • C programming Functions That Accept Arguments and Gives a Return Value.
    Oct 27, 2022

    How many codes are there in C? ›

    This tutorial provides a brief information on all 32 keywords in C programming.
    ...
    short, long, signed and unsigned.
    Data typesRange
    signed int-32768 to 32767
    unsigned int0 to 65535
    2 more rows

    What are the 3 major C language aspects? ›

    The main features of C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development.

    Is C language still relevant 2022? ›

    C might be old, but it is definitely relevant in 2022 and will likely remain so. The simplicity of C provides you with a perfect gateway into the programming world. It helps you understand the detailed implementation of any algorithm.

    What is the most popular C language? ›

    Python is the top programming language in TIOBE and PYPL Index. C closely follow Top-ranked Python in TIOBE.
    ...
    PYPL Index (US)
    Jun 2022Programming languageShare
    1Python29.53 %
    2Java17.06 %
    3JavaScript8.56 %
    4C/C++6.49 %
    24 more rows
    Jun 27, 2022

    What are the main topics in C? ›

    • Machine Learning Introduction.
    • Data PreProcessing.
    • Supervised Learning.
    • UnSupervised Learning.
    • Reinforcement Learning.
    • Dimensionality Reduction.
    • Natural Language Processing.
    • Neural Networks.
    Jan 16, 2023

    Is C easy to learn for beginners? ›

    While C is one of the more difficult languages to learn, it's still an excellent first language pick up because almost all programming languages are implemented in it. This means that once you learn C, it'll be simple to learn more languages like C++ and C#.

    How do you write Hello World in C? ›

    Basic example: Creating and running “Hello World”
    1. Create the following C program and name the source file hello.c : #include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; }
    2. Compile the program: ...
    3. Run the program by entering the following command: ./hello.

    Is C tough than Python? ›

    Ease of development – Python has fewer keywords and more free English language syntax whereas C is more difficult to write. Hence, if you want an easy development process go for Python. Performance – Python is slower than C as it takes significant CPU time for interpretation. So, speed-wise C is a better option.

    Should I learn C or Python? ›

    C is a great way to learn how computers actually work in terms of memory management, and is useful in high-performance computing. C++ is great for game development. Python is awesome for science and statistics. Java is important if you want to work at large tech companies.

    Should I learn C or C++ first? ›

    There is no need to learn C before learning C++. They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own. Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.

    How long should it take to learn C? ›

    If you are a beginner with no programming experience, you should expect it to take at least three months to learn the basics. If you have programmed before, it may only take you a month or two. To build mastery in C++, you should expect to spend at least two years working on improving your skills a little each day.

    Videos

    1. Writing your First C Program and Running it - C Programming Tutorial for Beginners
    (Studytonight)
    2. C Language Tutorial for Beginners (with Notes & Practice Questions)
    (Apna College)
    3. #15 C Functions | C Programming for Beginners
    (Programiz)
    4. C Programming Full Course 🕹️ (FREE)
    (Bro Code)
    5. Learn C language in 30 Minutes & Start Coding For Beginners in Hindi
    (Technology Gyan)
    6. Sample C Program - 11 C programming in telugu
    (Lab Mug)
    Top Articles
    Latest Posts
    Article information

    Author: Laurine Ryan

    Last Updated: 12/03/2022

    Views: 6655

    Rating: 4.7 / 5 (77 voted)

    Reviews: 84% of readers found this page helpful

    Author information

    Name: Laurine Ryan

    Birthday: 1994-12-23

    Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

    Phone: +2366831109631

    Job: Sales Producer

    Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

    Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.