“A typical vice of American politics is the avoidance of saying anything real on real issues” Theodore Roosevelt
In this section we will give you an overview of a simple C code and explaining each part of it.
Note: the introduction in this section will be shallow just to give you a general idea about syntax and structure of the language but in later sections we will dive deep in each part of the language and learn more about them.
Let’s take a look at a simple example:
#include <stdio.h>
int main() {
printf("Hello World!" );
return 0;
}
If you compile and run this example, you’ll get the “Hello World!” message on the monitor.
In a C program typically you’ll have these sections:
-
Preprocessor directives: In the example above the
`#include`is an example of such directives. You can
think of this particular directive as a way of referencing to another
source code somewhere in your computer.
Note: preprocessor is a program that runs before compiler starts to compile a program. Its job is to run directives like the one here #include.
Why we need them? Well, using preprocessor directive like #include is like saying:
“Hey preprocessor, I used one or more functions and probably a few variables that you will not find any definition of them in this program unless you check the file that I’m referencing via #include directive. So go ahead and read the file first so that compiler knows what to do when it faced the functions and variables that are not known in this program”.
If you take a look at the example, we’ve used a function named printf( ). Basically this function will take a string (text) and send it to the output stream (if you see the message on the screen then by default the destination of the output stream is computer’s screen).
But how does printf( ) function operate? How the compiler knew that this function is a standard C function and where are the instructions of this function? This is where the preprocessors directives like #include come in.
By using #include <stdio.h>we told the preprocessor before running the instructions of the program, first go and INCLUDE THE CONTENT of the file named “stdio.h” and replace the #include <stdio.h>line with the content of this file. Basically it is a copy& paste operation from the content of stdio.h file into our program.
Note: For now just remember that stdio.h is a header file (the extension of this file is “.h”) you’ll learn about headers in header section.
The content in this header file help the compiler to figure out what is the structure of the printf( ) function and how should it run.
Note: You’ll learn more about preprocessors in preprocessor section.
-
Functions: If you take a look at a restaurant you can see
that each person who works in there has a specific job. For example
restaurant server will take order and bring food to the customers but
he/ she does not cook him/herself! This is the job of the chef.
Each person who works in the restaurant knows the set of instructions and functions that he/ she need to follow.
This is a way of organizing tasks so that the whole work of restaurant becomes manageable.
In C Language we have pretty much the same concept!
Function is a way of dividing tasks into separate blocks and we call each function whenever they are needed.
For example if you have a program that should take text-input from a user and then process this text like it does some mathematical operation on it and then send the result to the output stream; we can simply divide this program into multiple functions like:
-
The first function only takes input texts from users and returns
those texts to any other functions that called this one.
Note: Like in a restaurant that a customer can call a waiter/ waitress and give orders (input) to her and expect food in return, functions in C language can also call each other and expect result from each other.
-
The second function takes text-input and it does some
mathematical operation on it and it returns the result to any function
that calls it. -
The last function will send any value to the output.
As you can see using function we can divide the purpose of an application into sub-tasks and focus on those tasks separately which will increase the speed and quality of developing an application at the end.
Now that we know what function is and why we need them, let’s see the structure of a function in the C language:
type function-name() {
//body...
}
-
Type: In a restaurant you expect a chief to return food right?
Food is the type of result that you can expect from a chief.
In the same manner, in C program, we can expect a function to return a value (like number or text etc.) but you can’t expect a function to return everything! Compilers need to know whether or not a function returns a result and if yes, what type that result is?
You can learn more about Data-Type in its section but for now some of the values that can be used here are: long, int, double, float, char etc.
-
Function-name: In our restaurant if the waiter/ waitress want to
give order to a chief he/ she need to call that person’s name! She can’t
just shout HEEYYYYYYYYYY and expect the chief to respond! The right way
is to call the chief’s name and give the order to her/ him when she got
her/ him attention.
In the same manner, functions in C program should have name so that when one function wants to call another one, there will be a name to use!
-
(): open and close parentheses that comes after the name of a
function has two use cases:
-
The function will declare what type of input it takes. For
example in the restaurant a chief only takes a piece of paper with type
of food written on it. He/ she can’t accept money and based on that
figure out what type of food she should return!
Note: if there’s nothing declared inside parentheses, it means the function will not accept input.
-
The caller of the function can put its inputs (also called
arguments) in this parentheses when it is calling the target
function.
For example:
// This is a prototype and you'll learn about it in prototype section.
int sum(int firstValue , int secondValue);
int main() {
int result = sum(2 , 4);
return 0;
}
int sum(int firstValue , int secondValue){
return firstValue + secondValue;
}
In this example we have two functions: main and sum`.
-
`{}`: curly brackets (also known as braces) declare the body or
the boundary of a function. So the body of a function starts from
opening brace and ends with the closing brace. -
Main function: When you want to read a book, typically
you’ll start from the page 1 and then read one page at a time until you
reach to the end of the book and is finished right? You won’t start from
the last page and read to the first page! (Hopefully!)
Normally the entry point to a book is its first page labeled with number 1 or something like that!
This is true for programs as well. Each program has an entry point where the first line of instruction will run and from there the next lines of instructions will follow.
The main()function is the entry point of any C-program. Basically this is where your program starts to execute instructions.
Note: the name of this function is main and not Main or MAIN. (The C-language is a case-sensitive language and so main is different than Main or MAIN).
For example in the code you’ve seen above, the first line of instruction in main ( ) function is to call another function named printf ( ) and so the moment this program ran, the main function will be executed and the instructions in the function will start to run one by one from top to down.
-
Comments: comments are a way of giving information to
developers (those who work on the program to build it) about the whole
program or simply part of it. Basically comments are designed for
developers not compilers! (Compilers simply ignore any comment in the
program).
Example:
// This is a comment….
int sum(int firstValue , int secondValue);
The // This is a comment…. part that starts with double forward-slashes is comment.
Note: we can write anything in comment (even in another language).
You can learn more about it in comments section.
-
`return` keyword: if a function returns a value when it’s
called, we use the `return` keyword to declare what information should
this function return to its caller.
For example:
int sum(int firstValue , int secondValue){
return firstValue + secondValue;
}
This function takes 2 arguments (firstValue and secondValue) then it will add these two values together and returns the result via the return keyword.
Note: the return keyword also indicates the end of a function. Which means any instruction below this keyword will not run.
You can learn more about it in return-section.
You should know that C-language is not line oriented. In this language we semicolon ; marks the end of each statement.
For example:
int ivar = 20;
This statement declared a variable named iVar and assigned the value 20.
As you can see at the end of this statement we used semicolon ;.
Because the C-language is not line oriented, we can change the statement above like this:
int
iVar
=
20
;
This statement has the same effect as int iVar = 20;. But for the sake of clarity we use one line to declare and assign value to a variable.
Note: elements like `data-type`, `variable-name` etc. are called tokens and such elements are indivisible which means we can't put space in them.
Example:
in t //WRONG…