0% found this document useful (0 votes)
11 views7 pages

C Basic Programming Q1 - Week 3 to 4

The document provides an overview of the basic structure of a C program, highlighting the essential components such as the inclusion of header files, the main function, and the return statement. It explains the purpose of the main function as the entry point for program execution and introduces common header files used in C programming. Additionally, it includes a task for readers to identify and fix errors in a given code snippet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

C Basic Programming Q1 - Week 3 to 4

The document provides an overview of the basic structure of a C program, highlighting the essential components such as the inclusion of header files, the main function, and the return statement. It explains the purpose of the main function as the entry point for program execution and introduces common header files used in C programming. Additionally, it includes a task for readers to identify and fix errors in a given code snippet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

BASIC C STRUCTURE

Elective 10
Computer Programming
Quarter 1 - Module 3 to 4
COMPUTER PROGRAMMING – QUARTER 1 MODULE 3-4
Basic C Structure

Default C Program · 3 Topics

1
COMPUTER PROGRAMMING – QUARTER 1 MODULE 3-4
Basic C Structure

Default C Program · 3 Topics

2
COMPUTER PROGRAMMING – QUARTER 1 MODULE 3-4
Basic C Structure

Default C Program · 3 Topics


Topic 1

C Anatomy
Upon reaching your home, you cleaned up the place, took out the manual, and
opened it. Surprisingly, the book came with a preface before the actual lesson. From
there, it writes:

"Before you learn how to code in C, you must first learn its foundations in order to
fully understand how it works. For this reason, I, the author of this book, would like to
introduce to you C's parts and pieces that make it function as a whole: C's basic
program structure."

The Basic C Structure

Before we get on to the different functions and statements used to program in C, we


have to first understand the basic structure of the code that we need to make for it to
run.

This is what a basic C program looks like:


#include<stdio.h>

int main(void) {

return 0;
}
Now don't get overwhelmed in there, it's actually simple and easy to understand!
Let's breakdown the different lines of code:

3
COMPUTER PROGRAMMING – QUARTER 1 MODULE 3-4
Basic C Structure

Default C Program · 3 Topics


• #include<stdio.h> - This line of code "includes" (or uses) the <stdio.h> library. A
library is a set of built-in functions, created by the programmers of the C
programming language. We use these built-in functions in making our own C
programs (more on this later).

• int main(void) - For now, just ignore the int and the void. Just focus on
the main. The main part of code is the code that is called by the compiler to execute
the program. Hence, any code that is typed inside the main function's curly braces
({}) is where the program execution begins. If your whole program is a room, then
the main() function is the door - the entry point.

• return 0; - This is the exit status code of our program. On most operating
systems, returning 0 is a success status which means the program worked fine until
the end, without errors. So don't forget to put return 0 at the end of your code, okay?

Did you know?


You can actually write the main() function without putting the word void inside its
parentheses, and it will still work the same, like this syntax:
Code
main.c
keyboard_arrow_leftkeyboard_arrow_right
int main() {
return 0; You can practice in our codechum
} classroom

restoreplay_arrowExecute Code
Press the Execute Code button above and see that there are no errors in the output.
More specifically, the output will only say "Program Terminated" and that's because
we didn't have a code above that prints something to the screen. All we have is an
empty main() code. How do we print something to the screen? Stay tuned in the
upcoming lessons!
Header Files

C has a lot of built-in header files which can be useful when coding, but these can
only function if we include it using the #include directive.

In programming, a library is a set of functions that we can use to write our own
programs easily. Think of a library as a toolbox which contains a lot of tools inside

4
COMPUTER PROGRAMMING – QUARTER 1 MODULE 3-4
Basic C Structure

Default C Program · 3 Topics


that we can use to construct our own programs.

There are a lot of different libraries in C, each containing their own functions. Before
we are able to use any function in C, we have to identify first which library is needed
to use the function. Here are some of the most basic ones available:

<stdio.h>
Standard Input/Output Functions. For printing to the screen our taking user inputs.
<math.h>
Mathematical Functions. For mathematical functions.
<string.h>
String Functions. For string/word functions.
<ctype.h>
Character Handling Functions. For single character handling functions.

It is important to note that header files always end with a .h (from the "h" in the word
"header file") to indicate that it is indeed a header file. Moreover, built-in header files
are always enclosed in angle brackets (<>), so keep this in mind whenever you need
to use one, okay?

This lesson only gives you an overview of the common library/header files that are
used in C, but we will get to know more about each of these libraries in the future
lessons, like when and how to use them, so just continue learning and keep going!

1. A Missing Piece
by CodeChum Admin
Something's wrong with the program I’m making and I just can't seem to figure it out.
Will you please fix it for me?

Instructions:

1. There seems to be something missing from the basic C structure code in the code
editor. Your task is to find out what's missing from the code and fix it to prevent
making errors when running your code.
2. When submitting your code, you have to get all test cases correct in order to proceed
to the next lesson, so always check your code for any error before submitting, okay?

Output
N/A
N/A
Score: 0/10

5
COMPUTER PROGRAMMING – QUARTER 1 MODULE 3-4
Basic C Structure

Default C Program · 3 Topics


Note: MORE ON THIS IN YOUR CODECHUM APP. Answer the 4 activities in
your Codechum classroom. There are 4 test cases that you need to do.

The C Structure

Lesson Summary
Basic C Structure
SYNTAX
Basic main() program
#include<stdio.h>

int main(void) {
return 0;
}

TERMS
main() function
the main part of code that is called by the compiler to execute the program

return 0;
returns the exit status of the program. 0 usually means, success

library
contains built-in functions that we can use in making our own programs

You might also like