0% found this document useful (0 votes)
1 views9 pages

C Programming Language

The document outlines the four fundamental stages in creating a C program: editing, compiling, linking, and executing. It explains the processes involved in each stage, including the use of integrated development environments (IDEs), the role of the compiler and linker, and the structure of a C program. Additionally, it covers basic data types, operators, and the significance of keywords and comments in C programming.

Uploaded by

Patson
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)
1 views9 pages

C Programming Language

The document outlines the four fundamental stages in creating a C program: editing, compiling, linking, and executing. It explains the processes involved in each stage, including the use of integrated development environments (IDEs), the role of the compiler and linker, and the structure of a C program. Additionally, it covers basic data types, operators, and the significance of keywords and comments in C programming.

Uploaded by

Patson
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/ 9

Chapter 1: Introduction

There are four fundamental stages, or processes, in the creation of any C program:
• Editing
• Compiling
• Linking
• Executing
You’ll soon get familiar with all these processes, since you’ll be carrying them out so often. First, we’ll
explain what each process is and how it contributes to the development of your C program.

1. Editing
Editing is the process of creating and modifying C source code – It is the name given to the program
instructions you write –.
Some C compilers come with a specific editor program that provides a lot of assistance in managing
your programs. In fact, an editor often provides a complete environment for writing, managing,
developing, and testing your programs. This is sometimes called an integrated development
environment (IDE). You can also use a general-purpose text editor -like block note on Windows OS-
to create your source files, but the editor must store the code as plain text without any extra formatting
data embedded in it; that is why word processor programs such as Microsoft Word aren’t suitable for
producing program code because of the extra formatting information they store along with the text. In
general, if you have a compiler system with an editor included, it will provide a lot of features that make
it easier to write and organize your source programs. There will usually be automatic facilities for
laying out the program text appropriately and color highlighting for important language elements, which
not only makes your code more readable but also provides a clear indicator when you make errors when
keying in such words.

2. Compiling
The compiler converts your source code into machine language and detects and reports errors in the
compilation process. The input to this stage is the file you produce during your editing, which is usually
referred to as a source file.
The compiler can detect a wide range of errors that are due to invalid or unrecognized program
code, as well as structural errors where, for example, part of a program can never be executed. The
output from the compiler is known as object code and it is stored in files called object files, which
usually have names with the extension .obj in the Microsoft Windows environment, or .o in the
Linux/UNIX environment.
The compiler can detect several different kinds of errors during the translation process, and most
of these will prevent the object file from being created. The result of a successful compilation is a file
with the same name as that used for the source file, but with the .o or .obj extension.
3. Linking
The linker combines the object modules generated by the compiler from source code files, adds required
code modules from the standard library supplied as part of C, and welds everything into an executable
whole. The linker also detects and reports errors; for example, if part of your program is missing or a
nonexistent library component is referenced.
In practice, a program of any significant size will consist of several source code files, from
which the compiler generates object files that need to be linked. A large program may be difficult to
write in one working session, and it may be impossible to work with as a single file. By breaking it up
into a number of smaller source files that each provide a coherent part of what the complete program
does, you can make the development of the program a lot easier. The source files can be compiled
separately, which makes eliminating simple typographical errors a bit easier. Furthermore, the whole
program can usually be developed incrementally. The set of source files that make up the program will
usually be integrated under a project name, which is used to refer to the whole program.
Program libraries support and extend the C language by providing routines to carry out
operations that aren’t part of the language. For example, libraries contain routines that support
operations such as performing input and output, calculating a square root, comparing two character
strings, or obtaining date and time information.
A failure during the linking phase means that once again you have to go back and edit your
source code. Success, on the other hand, will produce an executable file, but this does not necessarily
mean that your program works correctly. In a Microsoft Windows environment, the executable file
will have an .exe extension; in UNIX, there will be no such extension, but the file will be of an
executable type. Many IDEs have a build option, which will compile and link your program in a single
operation.

4. Executing
The execution stage is where you run your program, having completed all the previous processes
successfully. Unfortunately, this stage can also generate a wide variety of error conditions that can
include producing the wrong output, just sitting there and doing nothing, or perhaps crashing your
computer for good measure. In all cases, it’s back to the editing process to check your source code.
Now for the good news: this is also the stage where if your program works, you get to see your
computer doing exactly what you told it to do! In UNIX and Linux you can just enter the name of the
file that has been compiled and linked to execute the program. In most IDEs, you’ll find an appropriate
menu command that allows you to run or execute your compiled program. This Run or Execute option
may have a menu of its own, or you may find it under the Compile menu option. In Windows, you can
run the .exe file for your program as you would any other executable.
The following picture summarize the process of program editing a program in C programming
language.

Note: in this course we use the CodeBlock IDE that is free and open source. You are free to use any
other one like Visual Studio code if you are familiar with.
Figure 1: C program editing steps

Chapter 2: The structure of a program in C programming


language.
The general structure of a C program is given below:
// Preprocessing Directives

} Preprocessing Directives
// instruction to the compiler.
int main()

}
{
// instructions // signature of the main function
return value; // Beginning of the main function
}
// Instructions of the program
the main function
// returning an integer value to the
// end of the main function
1. The preprocesing directives
a preprocesing directives is not strictly part of the executable program; they give instruction to the
compiler to perform some action. They use the symbol # to indicate it is a preprocessing directive,
which is an instruction to your compiler to do something before compiling the source code. The
compiler handles these directives during an initial preprocessing phase before the compilation process
starts. There are quite a few preprocessing directives, and there are usually some at the beginning of the
program source file, but they can be anywhere.
Example:
#include <stdio.h>
#include <stdlib.h>

2. The main function


A function is just a named block of code between braces that carries out some specific set of operations.
Every C program consists of one or more functions, and every C program must contain a function
called main()—the reason being that a program always starts execution from the beginning of this
function. So imagine that you’ve created, compiled, and linked a file called progname.exe. When you
execute this program, the operating system executes the function main() for the program.
The main() function can call other functions, which in turn may call further functions, and so
on. For every function that’s called, you have the opportunity to pass some information to it within the
parentheses that follow its name. A function will stop execution when a return statement in the body of
the function is reached, and control will then transfer to the calling function (or the operating
system in the case of the function main()). In general, you define a function so that either it does
return a value or it does not. When a function does return a value, the value is always of a specific type.
In the case of main(), the value that is returned is of type int, which is an integer.
Example:
the following program is a C program that prints the message “Hello world!” to the user
#include <stdio.h>
#include <stdlib.h>
int void main()
{
printf("Hello world!\n");
return 0;
}

3. Keywords
In C as in most common programming languages , a keyword is a word with special significance, so
you must not use keywords for any other purpose in your program -for example you cannot use any as
the name of a variable-. For this reason, keywords are also referred to as reserved words. In the
preceding example, int is a keyword and void and return are also keywords. C has several keywords,
and you’ll become familiar with more of them as you learn more of the language.

Chapter 3: Basic Types and Operators


C provides a standard, minimal set of basic data types. Sometimes these are called "primitive" types.
More complex data structures can be built up from these basic types.
1. Basic types
1.1 Integer Types
The "integral" types in C form a family of integer types. They all behave like integers and can be mixed
together and used in similar ways. The differences are due to the different number of bits ("widths")
used to implement each type -- the wider types can store a greater ranges of values.
1.1.1 char: ASCII character
they are at least 8 bits. As a practical matter char is basically always a byte which is 8 bits
which is enough to store a single ASCII character. 8 bits provides a signed range of -
128..127 or an unsigned range is 0..255. char is also required to be the "smallest addressable
unit" for the machine -- each byte in memory has its own address.
A char constant is written with single quotes (') like 'A' or 'z'. The char constant 'A' is really just a
synonym for the ordinary integer value 65 which is the ASCII value for 4 uppercase 'A'. There are
special case char constants, such as '\t' for tab, for characters which are not convenient to type on a
keyboard.
1.1.2 short: Small integer
they are at least 16 bits which provides a signed range of -32768..32767. Typical size is 16
bits. Not used so much.
1.1.3 int: Default integer -- at least 16 bits, with 32 bits being typical. Defined to be the
"most comfortable" size for the computer. If you do not really care about the range for an
integer variable, declare it as int since that is likely to be an appropriate size (16 or 32
bit) which works well for that machine.
1.1.4 long: Large integer -- at least 32 bits. Typical size is 32 bits which gives a signed
range of about -2 billion ..+2 billion.
The integer types can be preceded by the qualifier unsigned which disallows representing negative
numbers, but doubles the largest positive number representable. For example, a 16 bit implementation
of short can store numbers in the range -32768..32767, while unsigned short can store 0..65535.
1.2 Floating point Types
float Single precision floating point number typical size: 32 bits
double Double precision floating point number typical size: 64 bits
long double Possibly even bigger floating point number (somewhat obscure)
1.3 Literals
In C, Literals are the constant values that are assigned to the variables. Literals represent fixed
values that cannot be modified. Literals contain memory but they do not have references as
variables. Generally, both terms, constants, and literals are used interchangeably.
1.3.1 char Constants
A char constant is written with single quotes (') like 'A' or 'z'. The char constant 'A' is really
just a synonym for the ordinary integer value 65 which is the ASCII value for 4 uppercase
▪ 'A' uppercase 'A' character
▪ '\n' newline character
▪ '\t' tab character
▪ '\0' the "null" character -- integer value 0 (different from the char digit '0')
▪ '\012' the character with value 12 in octal, which is decimal 10
1.3.2 int Constants
Numbers in the source code such as 234 default to type int. They may be followed by an 'L' (upper or
lower case) to designate that the constant should be a long such as 42L. An integer constant can be
written with a leading 0x to indicate that it is expressed in hexadecimal -- 0x10 is way of expressing the
number 16. Similarly, a constant may be written in octal by preceding it with "0" -- 012 is a way of
expressing the number 10.
1.3.3 flotting point Constants
Constants in the source code such as 3.14 default to type double unless the are suffixed with an 'f' (float)
or 'l' (long double). Single precision equates to about 6 digits of precision and double is about 15 digits
of precision. Most C programs use double for their computations. The main reason to use float is to save
memory if many numbers need to be stored. The main thing to remember about floating point numbers
is that they are inexact. For example, what is the value of the following double expression? (1.0/3.0 +
1.0/3.0 + 1.0/3.0) // is this equal to 1.0 exactly? The sum may or may not be 1.0 exactly, and it may vary
from one type of machine to another. For this reason, you should never compare floating numbers to
eachother for equality (==) -- use inequality (<) comparisons instead.
Realize that a correct C program run on different computers may produce slightly different outputs in
the rightmost digits of its floating point computations.
2 Basic operators
2.1 Comments
Comments in C are enclosed by slash/star pairs: /* .. comments .. */ which may cross
multiple lines. C++ introduced a form of comment started by two slashes and extending to
the end of the line: // comment until the line end The // comment form is so handy that many
C compilers now also support it, although it is not technically part of the C language. Along
with well-chosen function names, comments are an important part of well written code.
Comments should not just repeat what the code says. Comments should describe what the
code accomplishes which is much more interesting than a translation of what each statement
does. Comments should also narrate what is tricky or non-obvious about a section of code.
2.2 Variables
As in most languages, a variable declaration reserves and names an area in memory at run
time to hold a value of particular type. Syntactically, C puts the type first followed by the
name of the variable. The following declares an int variable named "num" and the 2nd line
stores the value 42 into num.
int num;
num = 42;

A variable corresponds to an area of memory which can store a value of the given type. Making
a drawing is an excellent way to think about the variables in a program. Draw each variable as
box with the current value inside the box. Variables, such as num, do not have their memory
cleared or set in any way when they are allocated at run time. Variables start with random
values, and it is up to the program to set them to something sensible before depending on their
values. Names in C are case sensitive so "x" and "X" refer to different variables. Names can
contain digits and underscores (_), but may not begin with a digit. Multiple variables can be
declared after the type by separating them with commas.
Example:

2.3 Assignment Operator =


The assignment operator is the single equals sign (=). i = 6; i = i + 1; The assignment
operator copies the value from its right hand side to the variable on its left hand side. The
assignment also acts as an expression which returns the newly assigned value. Some
programmers will use that feature to write things like the following
y = (x = 2 * x); // double x, and also put x's new value in y
2.4 Boolean
C does not have a distinct boolean type, int is used instead. The language treats integer 0 as false and all
non-zero values as true. So the statement... i = 0;
while (i - 10) { ...

will execute until the variable i takes on the value 10 at which time the expression (i - 10) will become
false (i.e. 0). (we'll see the while() statement a bit later)

2.5 Mathematical Operators

C includes the usual binary and unary arithmetic operators. The operators are sensitive to
the type of the operands. So division (/) with two integer arguments will do integer division.
If either argument is a float, it does floating point division. So (6/4) evaluates to 1 while
(6/4.0) evaluates to 1.5 -- the 6 is promoted to 6.0 before the division.
+ Addition
- Subtraction
/ Division
* Multiplication
% Remainder (mod)
2.6 Unary Increment Operators: ++ --
The unary ++ and -- operators increment or decrement the value in a variable. There are
"pre" and "post" variants for both operators which do slightly different things (explained
below)
var++ increment "post" variant
++var increment "pre" variant
var-- decrement "post" variant
--var decrement "pre" variant
Example
int i = 42;
i++; // increment on i
// i is now 43
i--; // decrement on i
// i is now 42
2.7 Relational Operators
These operate on integer or floating point values and return a 0 or 1 boolean value.
!= Not Equal
> Greater Than
< Less Than
>= Greater or Equal
<= Less or Equal
To see if x equals three, write something like:
if (x == 3) ...
2.8 Logical Operators
The value 0 is false, anything else is true. The operators evaluate left to right and stop as soon as the
truth or falsity of the expression can be deduced. (Such operators are called "short circuiting") In ANSI
C, these are furthermore guaranteed to use 1 to represent true, and not just some random non-zero bit
pattern. However, there are many C programs out there which use values other than 1 for true (non-zero
pointers for example), so when programming, do not assume that a true boolean is necessarily 1 exactly.
! Boolean not (unary)
&& Boolean and
|| Boolean or

You might also like