You are on page 1of 8

THE BASIC C STRUCTURE

NOTES

Introduction
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.
A basic C program consists of two main parts: its preprocessor
directives and the main() function. Here is a sample program to illustrate
each part:
//This is the preprocessor directive
#include <stdio.h>

//This is the main function


int main(void){

return 0;
}

A preprocessor is a program which, as the name suggests, is executed


before the main code is processed. Mostly, this comprises of built-in
header files or libraries that contain tons of functions that can be used
when coding in C.
To call these, we use directives, which are keywords that start with a
hash/pound (#). When it comes to calling built-in header files, we use
#include and enclose the header file name in angle brackets, like this:
#include <filename.h>

The main() function, on the other hand, is the main part of 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.

PRINTF() SYNTAX
The printf() statement is a built-in output function in C that allows the
computer to print out on the screen a certain text or number. When
printing strings (or text), it follows the following syntax:

printf("dummy string");

where the text to be outputted is enclosed with a pair of double quotes ("),
and all enclosed by parentheses and ending with a semicolon (;).
However, we cannot use the printf () function easily without including the
header file that we need to use these standard input/output functions. To
do that, we have to make use of preprocessor directives.

So, when we want to greet the world using a program in C, we can now
do this:

#include <stdio.h>

int main(){
printf("Hello World!");

return 0;
}
Basically, this function can only print strings by default, but it could also
print numbers as well if we do the follow the same method of printing as
with strings – by enclosing it with double quotes, like this:
#include <stdio.h>
int main(){
printf("1234");

return 0;
}

Escape sequences are special characters characterized by a


backslash (\) and a letter or symbol beside it. It is used in printf ()
statements to show spaces, new lines, or show symbols that cannot be
outputted using the normal way of printing.
Types of Escape Sequences
Each escape sequence has its own function when used in printing. Here
are some basic ones that are made available for use in C:
• \t - Prints a horizontal tab space.
o printf("Hello\tWorld!")
Hello World!
• \n - Prints the proceeding text to a new line.
o printf("Hello\nWorld!")
Hello
World!
• \\ - Prints a backslash
• \" - Prints a double quote inside texts enclosed in double quotes
A placeholder is basically a formatting specifier that will be replaced by
a value corresponding to its data type. Basically, it's like a blank that needs
to be filled up with a value. When using placeholders in printf ()
statements, it follows the following syntax:
printf("placeholder", value);

Where the placeholder indicates the format specifier to be used, followed


by a comma (,) that connects it to the value that substitutes the
placeholder.
Tip: to print out the Pi symbol, we need to make use of its Unicode
equivalent. Just put 'π' inside the printf function e.g. printf("\u03C0");

Types of Placeholders
There are six (6) basic types of placeholders that you can use in C, and
these are the following:
Type Format Meaning
Int (Integer) %d & %i a whole number (3,7, +8, -9,1000000)

Float %f a number with decimal point (5.8, 9.0, -


5.6789, +0.0032)

Char %c a single letter, symbol, or number


enclosed within two single quotes
(‘B','r’, ‘*’, ‘3’)

Char %S a string in C is considered as a series of


characters (“Ms.”,” Hi!”, “143”, “I
love”)

double %f for bigger or larger numbers with a


decimal point (123456789.123)
VARIABLE DECLARATION
Variables are objects that are used to store values. As an object,
they act as containers or buckets where the data will be put into and stored.
In C, declaring a variable uses this kind of syntax:
data_type varName;

Variable declaration in C requires 3 parts: (1) the data type it will


possess, (2) the variable name of your choice, and (3) a semicolon at the
end. Unlike other languages that allow their variables to change data types
whenever the programmer wants to, C's variable data types are defined
upon declaration and will always hold that data type afterwards.
• Char - Character (a letter, symbol, or white space).
• Int - Integer (whole number ranging between ±32767).
• long int - Long integer (whole numbers ranging between
±2147483647).
• float - Floating point values (real numbers that can handle up to 7
decimals).
• double - Double floating-point values (real numbers that can handle
up to 15 decimals).
We can also declare a variable and directly assign it to a value using
the assignment operator, the equal sign (=), like this:
data_type varName = value;

If you wish to declare multiple variables of the same data type,


you can actually just use one line declaration and separate each variable
name with a comma, like this:
#include <stdio.h>
int main ()
{
int num1 = 4, num2 = 5;
printf ("Num1 = %d, Num2 = %d", num1, num2);
return 0;
}
Note that a variable's data type remains as it is once declared, so
when you try to assign a letter to an integer variable, the character value
will be converted into its integer equivalent. The same thing happens
when you assign a float to an integer variable, since it will only take the
whole number from the assigned value.
#include <stdio.h>

int main(){
int age = 'C';
//it will print out the integer equivalent of the uppercase C instead
//to start, since the integer equivalent of uppercase A is 65,
//then counting from there, uppercase C will be 67, like this:
printf("%d", age);

int gravity = 9.8;


//for floats, it will print out only the whole number portion instead
printf("%d", gravity);

return 0;
}

#include <stdio.h>

int main(){
int age;
age = 4;

//let us try to use the variable in a sentence using placeholders, like


this:
printf("I'm Cody and I am already %d years old\n", age);
printf("It has also been %d years since I have seen the world.", age);

return 0;
}
#include <stdio.h>

int main(){
int age = 4;
/* when you want to change the age you print,
you just need to change this code right here instead of typing in on tons of lines
like those below! */
printf("%d\n", age);
printf("%d\n", age);
printf("%d\n", age);
printf("%d\n", age);

return 0;
}

The texts after double slashes (//) and those in between a pair of
slash asterisks (/*) that you see in the code above will not be read by the
code, and these are what we call as comments.

Comments are very useful as they serve as guides to the code


especially when you want to look at your code again after a long time, or
if someone else tries to read the code you've written, it would easily be
understood by anyone who looks at it again.
CHANGING VARIABLE VALUES
Just like in other programming languages, we can change and
overwrite the value assigned in C variables as well! The only thing we
have to do is to follow the same syntax as when declaring a variable with
an assigned value using the assignment operator, but without typing in the
data type anymore, like this syntax:
varName = value;

For example, when we want to update the age that was initially
assigned to the variable, we can do it like this:
#include <stdio.h>

int main(){
int age = 3;
printf("Current Age: %d\n", age);

age = 4;
printf("Age a year after: %d", age);

return 0;
}

THE SYNTAX
The scanf() function works just like listening to another person in
human sense, such that, it receives what the other person says. Similarly,
in C, this function will allow the user to type in a specific data that is asked
by the program. We shall also remember that since this is a standard input
function, it will need the preprocessor directive, #include <stdio.h> for it
to work.
By convention, we must create a variable that will store the value to
be inputted using the scanf() function. After creating the variable that will
serve as the container, we can now use the scanf function by following
this syntax:
scanf("placeholder", &varName);

The ampersand symbol (&), also known as the address operator,


plays the most important role in this process as it allows the scanf()
function to access the address of the variable, locate it, and store the value
that will inputted by the user into the variable.

You might also like