C Programming in 2021
by Ray Dall
################################################################################
Section 2
################################################################################
Before we go any further, lets lay down a framework of common terms that you'll need to know and understand:
Compiler: The program that converts your high level C computer
program into a low level assembly language program.
Directive: "Commands" given specifically to the compiler, telling
it how to deal with your code.
Keyword: A predefined reserved word in the C language.
They are statements, functions,or types, and have
special meaning to the compiler. You can not
re-define a keyword.
Macro Syntax: #define
This macro defines constant value and can be any of the basic data types.
Header file inclusion Syntax: #include
The source code of the file “file_name” is included in the main program at the specified place.
Conditional compilation Syntax: #ifdef, #endif, #if, #else, #ifndef
Set of commands are included or excluded in source program before compilation with respect to the condition.
Other directives Syntax: #undef, #pragma
#undef is used to undefine a defined macro variable. #Pragma is used to call a function before and after main function in a C program.
Operator: A symbol that tells the compiler to perform specific mathematical
or logical functions (+ - / * etc.)
Operand A value (fixed or variable) acted upon by an operator
c = a + b;
a and b are operands
+ is the operator.
Expression: A sequence of operators and operands often yielding a single value.
Statement: An expression followed by a semicolon.
Function: A program or subroutine made up of at least one, often several statements.
Argument: A value that is passed to a function
Note: There are a TOTAL of 32 keywords in C.
A couple of them are statements or functions.
The majority of them are descriptions of types.
You do not need to memorize these yet.
We will cover each keyword one at a time.
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
The C programming language is made up of commands called Statements or Functions.
A Statement is a directive to the computer to "go do something".
A Function is a group of statements that perform a particular task.
Function - change the tire....
Statement - put the wrench on the first lug nut
A function may contain a single statement, or several statements.
A function can be a stand alone program all by itself, or there may be multiple functions within a program.
All functions have the same basic parts, and if you skip one of them, the
compiler will barf at you.
The parts of every function are:
the type,
the identifier,
the arguments,
and the ending punctuation.
Some statements may exclude some of the above.
The TYPE indicates what type of RETURN the function will give (more on this shortly).
The identifier, of course, is the name of the function.
ALL C programs must have at least one function, and that function must be called main().
The program may have other functions in it as well, but it must always include the main().
// type identifier arguments endpunc
int main () {}
|
The arguments are data, either text or numerical, which is fed to the function for the function to act upon.
That data can be inserted directly within your code, by the user at time of execution, or by another program.
We'll cover this in more detail later.
Finally, the endpunc.... ending punctuation. Think of this as the period at the end of a sentence. Note that in English (your language) you don't have to have a period. You might end a sentence with a question mark (?) or an exclamation point (!).
There are two, and only two legal types of endpunc in the C language. The curley braces {} and the semicolon ;
...and each has its own purpose.
If the function contains other functions within it, you use the curley brace. If it is a stand-alone function, it will have a semicolon. Think of the semicolon as the period at the end of the sentence. If you forget it, your grammar teacher doesn't just take points off your paper, they completely fail you. If you forget one endpunc, you fail the whole paper. I told you that the compiler was the meanest grammar teacher in the world!
So let's go back into our program and make some changes.... let's make it actually DO something!
nano first.c
// NAME: first.c
// DESCRIPTION: A first C program to teach the parts of a function.
int main()
{
// type identifier arguments endpunc
return (12) ;
}
|
Notice that this time, we added return(12); to the program.
The curley braces are not on the same line anymore, but that doesn't matter, so long as
you still have an open and close curley brace.
The function main() still has a type, a name, arguments (which are blank), and endpunc (the curley braces).
We have added a "return" statement, which also has an identifier (return), an argument (12), and endpunc (the semicolon).
Note that we don't have a "type" for return. Statements do not necessarily follow the same rules as a function, but often they can.
So what exactly does the return(12); do? I thought you'd never ask!
If you compile and run the program, it "returns" a code (specifically an integer) to the shell.
That code is interpreted as a success or failure code. Typically, if it returns a zero (0), it indicates a successful completion of the program. To see whether the program successfully completed or not, type echo $? into the terminal.
In this particular case, instead of it returning what would be the nominal success code (0), we have told it to return a 12. Lets see what happens when we compile and run it...
$> cc first.c
$> ./a.out
$> echo $?
12
$>
|
Because you TOLD it to return 12, it returned a 12 to the shell. When you ask the shell what the response from the last running program was (echo $?) it gives you the return.
If you want to test the theory, go back into the program and change the 12 to some other number, and compile and run it again. When you ask for the echo $? response, you will get that number. (hint - if it didn't give you the same number, you probably forgot to compile it with cc ).
// NAME: first.c
// DESCRIPTION: A first C program to teach the parts of a function.
int main()
{
return(5);
}
|
Compile and run...
$> cc first.c
$> ./a.out
$> echo $?
5
$>
|
Let us take this a step further. If all a C program could do is return whatever
number we told it to, it would be kind of useless. We need for it to be able to
do other stuff, like print words out, create files, do math, etc. So let's add some
simple math stuff, just to prove it can do it, then we'll start to get fancy...
Let's write another program:
nano second.c
// NAME: second.c
// DESCRIPTION: My SECOND program can do math!
int main()
{
int x;
x=5;
int y;
y=10;
int z;
z=x+y;
return(z);
}
|
Compile and run...
$> cc second.c
$> ./a.out
$> echo $?
15
$>
|
So you see now, that we can not only send data to the shell, but that we can do maths and send the result to the shell.
While this is handy, this is NOT normally done. Under normal conditions, return() in a C program should typically return
ZERO unless there is some kind of a problem. The return of zero, in most cases, indicates a successful completion of a program.
Return of some other number usually is the result of a program failure.
int main()
{
return(0);
}
|
################################################################################
1
2
3
4
5
################################################################################