TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    C Tutorials

    C Program Structure

    Introduction

    In this tutorial, we will understand the C Program Structure. The main parts of a C program are as follows:

      • Comments 
      • Preprocessor Directives
      • Global Variables Declaration
      • Function Prototypes
      •  main() function
        • Local Variables Declaration
        • Program Code
      • Function definitions

    Comments

    Comments provide useful explanations and documentation for the readers. Comments are not executable statements in the C program. They are ignored by the compiler.

    Preprocessor Directives

    The preprocessor directive is a directive to the C preprocessor. It is processed before compiling the C program.

    Global declaration

    The global declaration section consists of the declaration of global variables. Global variables have the lifetime of the entire C program and are visible to the entire C program.

    Function Prototypes

    A C program can contain more than one function. If any functions are used other than the main function, we can declare the prototypes of the functions in this section.

    main Function

    The main function is the entry point of the C program. The C program execution starts with the main() function. The main function consists of local variable declarations and the programming statements section. The statements perform the actual program logic and can also contain function calls.

    int main()
    {
    /*
    Local declarations go here
    */
    …

    // Programming Statements – Program Logic
    …
    return 0;
    } // end main

    User-defined functions

    The function definitions section contains the user-defined function code. This section contains the code for the functions declared in the function prototype section.

    C Program Structure

    Example C program to show the structure.

    /**
    **********************************
    * Program Description:
    * C Program Structure Demo
    * Filename : program.c
    * Author : TestingDocs
    * Date : 02/01/2017
    * C Tutorials - www.TestingDocs.com
    *************************************
    */
    // Preprocessor directives
    #include<stdio.h>
    
    // Global declarations
    #define PI 3.1415
    
    // Function prototypes
    double computeArea(double r);
    
    // main function
    int main()
    {
     /*
     Local declarations go here
     */
     double radius = 0.0;
     double circleArea = 0.0;
    
     // programming statements - logic
     printf("Enter radius of circle ( in meters) =");
     scanf("%lf",&radius);
     circleArea = computeArea(radius);
     printf("Area of circle = %lf m^2",circleArea);
     return 0;
    } // end main
    
    /* User defined function definition */
    double computeArea(double r)
    {
     return PI*r*r;
    }

     

    C Program Structure

    —

    C Tutorials

    C Tutorials on this website can be found at:

    https://www.testingdocs.com/c-language-tutorial/

    The IDE used in the tutorial is Code:: Blocks. To download and install Code Blocks follow the link:

    https://www.testingdocs.com/download-and-install-codeblocks/

    For more information on Code Blocks IDE, visit the official website of Code blocks IDE:

    https://www.codeblocks.org/

    Related Posts

    C Language Characteristics

    C Tutorials /

    C Language Characteristics

    C Source Code .c File

    C Tutorials /

    C Program Files & Formats

    GNU Compiler Collection GCC

    C Tutorials /

    C Compilers

    C Pointers

    C Tutorials /

    C Pointers

    C Tutorials /

    C Two-Dimensional Arrays

    ‹ Getting Started with the First C Program› C Arithmetic Operators

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com