The Basic C Structure
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>
return 0;
}
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;
}
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)
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);
return 0;
}
#include <stdio.h>
int main(){
int age;
age = 4;
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.
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);