Write a Hello World application with C

This section outlines the steps to write a C program in Geany.

  1. Launch Geany. From the desktop, select Applications > Development > Geany.

  2. Select Project > New. Name the project hello_world_c.

    The fields represent the following:

    • Name — The name of the project.
    • Filename — The filepath of the project, where the project settings are stored. This path generally ends in .geany
    • Base path — The base path of all files making up the project.


  3. Click OK when prompted to create the directory.

  4. Write a Hello World sample application. This example uses:

    #include <stdlib.h>
    #include <stdio.h>
    
    int main (int argc, char **argv)
    {
        printf("Hello World!);
    
        char *buf = NULL;
    
        printf("%d", *buf);
    
        exit(0);
    }
  5. Select File > Save As, and save it as hello_world.c.

  6. Select Build > Compile to compile the source.

  7. Select Build > Build to create an executable for the code.

  8. Select Build > Execute to execute the code in a terminal window:



  9. Try the linting function by adding the following code after your print statement:

    char *buf = NULL;
    
    printf("%d", *buf);
  10. Select Build > Lint. You should see a warning for dereferencing a NULL pointer:



  11. Next, try debugging the Hello World sample. Update the compile commands to include the -g flag by selecting Project > Properties.

  12. Select the Build tab and update the Compile and Build commands to include -g:



  13. Select Debug > Setup Program.

  14. Enter the full path to the hello_world executable, then click OK:



  15. Set a breakpoint on the printf() statement. Click the line in the IDE, then select Debug > Toggle Breakpoint. A blue dot appears next to the respective line number:



  16. Select Debug > Run/Continue to run the debugger.

  17. Once the breakpoint has been hit, select Debug > Run/Continue or Debug > Step Over.

Next steps

You can override the default commands for C files in /home/qnxuser/.config/geany/filedefs.

For more information on how to modify the default commands, refer to the "Adjusting build commands" section in the "Customizing Geany" page.

For more information on how to customize the lint commands, refer to the Clang-Tidy documentation at https://clang.llvm.org/extra/clang-tidy/.

Page updated: