1 C Basic
1 C Basic
Definition: Programming
Programming is the translation of user ideas into a representation or
form that can be understood by the computer. The tools of writing
programs are called programming languages.
Definition: Programming language
A programming language is a set of rules used to write computer
programs. Like human languages, computer languages have a
syntax which defines the language grammar.
2. Design program
This stage is about specifying how the program will meet the requirements
identified i.e. how it will be implemented. Program areas considered are the
user interface, program flow (algorithm) , data types and names.
often referred to as the source file. The custom is that the text of a C program
is stored in a file with the extension .c for C language
4. Compiling
This is the translation of the source file must be translated into machine
understandable form - binary numbers. An intermediate file called the object
code - with the extension .obj is produced.
5. Linking
This is the integration of library file information with the intermediate
code of your program to produce the final program file that you
execute or run. The final file is called the executable file - .exe file.
Library files are used by a programmer to enable his/her program
perform some special task for which a function has been written by a
manufacturer.
You can then run .exe files as you run applications, simply by typing
their names at the DOS prompt or run using Windows menu.
What is C?
• C is a compiled language. This means that once you write your C
program, you must run it through a C compiler to turn your program
into an executable that the computer can run (execute).
• C is a general-purpose language which has been closely associated
with the Unix operating system for which it was developed - since the
system and most of the programs that run it are written in C.
• C supports structured programming
• C is a middle level language. This refers to the fact that C can be
used to write low level programs as well as high level programs.
2
3 BASIC @ FRED 2023
In addition C has (and still is) been used to solve problems in areas such
as physics and engineering.
A simple C program.
This program will print out the message: This is a C program.
#include<stdio.h>
main()
{
printf("This is a C program \n");
return 0;
}
3
4 BASIC @ FRED 2023
• Expressions
• Input and output statements
The following program demonstrates the C program components.
#include<stdio.h>
main()
{
int num;/ define a variable called num */
num = 1; / assignment /
printf(“This is a simple program ”);
printf(“to display a message. \n”);
printf(“My favorite number is %d because ”, num);
printf(“ it is first.\n ”);
return 0;
}
Keywords
These are reserved words
that have special meaning
in a language. The
compiler recognizes a
keyword as part of the language’s built – in syntax and therefore it cannot be
used for any other purpose such as a variable or a function name.
Functions
A function is a set of related statements that perform a specific task in a
program. A statement specifies an action to be performed by the program.
Note that all C statements must end with a semicolon.
The main( ) function is the point at which execution of your program begins.
When your program begins running, it starts executing the statements inside
the main( ) function, beginning with the first statement after the opening curly
brace - {. Execution of your program terminates when the closing brace- }, is
reached.
Library functions are also part of C programs. These are ready-to- run
programs written by the C language producer (such as Borland). The
functions perform tasks such as program data Input/output, string
manipulations, mathematics, and much more.
4
5 BASIC @ FRED 2023
One of the most common library functions is called printf( ). This is C’s
general purpose output function. Its simplest form is
printf(“string – to – output”);
The printf( ) outputs the characters that are contained between the beginning
and ending double quotes.
For example, printf(“ This is a C program “);
• In the above program, line 6 causes the message enclosed in speech
marks “ ” to be printed on the screen. Line 7 does the same thing.
• The \n in line 7 tells the computer to insert a new line after printing the
message. \n is an example of an escape sequence.
• Line 8 prints the value of the variable num (1) embedded in the
phrase. The %d instructs the computer where and in what form to
print the value. %d is used to specify the output format for integer
numbers.
• Line 9 has the same effect as line 7.
• Line11 indicates the value to be returned by the function main( ) when
it is executed. By default any function used in a C program returns an
integer value (when it is called to execute). Therefore, line 2 could
also be written int main( ). If the int keyword is omitted, still an integer
is returned.
Then, why return (0); ? Since all functions are subordinate to main( ), the
function does not return any value.
Tip
• Since the main function does not return any value, line 3 can
alternatively be written as : void main( ) – void means valueless.
In this case, the statement return 0; is not necessary. We shall
adopt the former hence forth.
Preprocessor Directives
A preprocessor directive is a statement that performs various manipulations on your source
file before it is actually compiled.
The preprocessor directive #include is an instruction to read in the contents of another file
and include it within your program. This is generally used to read in header files for library
functions
Header files contain details of functions used within the library. They must be included
before the program can make use of the library functions. Library header file names are
enclosed in angle brackets, < >.
Comments
Comments are non – executable program statements meant to enhance
program readability and allow easier program maintenance, i.e. they
document the program.
5
6 BASIC @ FRED 2023
Declaration Statements
In C, all variables must be declared before they are used (discussed later).
Line 4 is a declaration for an integer variable called num.
Escape Sequences
Escape sequences (also called back slash codes) are character combinations that begin
with a backslash symbol (\) used to format output and represent difficult-to-type characters.
One of the most important escape sequences is \n, which is often referred to as the new line
character. The program below displays the following output on the screen.
#include<stdio.h>
void main()
{
printf(“This is line one \n”);
printf(“This is line two \n”);
printf(“This is line three”);
}
Syntax Errors
They result from the incorrect use of the rules of programming. The compiler
detects such errors as soon as you start compiling. A program that has
syntax errors can produce no results. You should look for the error in the line
suggested by the compiler.
Syntax errors include;
• Missing semi colon at the end of a statement
• Use of an undeclared variable in an expression
• Illegal declaration e.g. int x, int y, int z;
• Use of a keyword in uppercase e.g. FLOAT, WHILE
• Misspelling keywords e.g. init instead of int
6
7 BASIC @ FRED 2023
Logic Errors
These occur from the incorrect use of control structures, incorrect calculation,
or omission of a procedure. Examples include: An infinite loop in a program,
generation of negative values instead of positive values. The compiler will
not detect such errors since it has no way of knowing your intentions. The
programmer must dry run the program so that he/she can compare the
program’s results with already known results.
Semantic Errors
They are caused by illegal expressions that the computer cannot make
meaning of. Usually no results will come out of them and the programmer will
find it difficult to debug such errors. Examples include a data overflow caused
by an attempt to assign a value to a field or memory space smaller than the
value requires, division by zero, etc.
That is:
datatype variable; or
For example,
7
8 BASIC @ FRED 2023
Variable Names
Every variable has a name and a value. The name identifies the variable and
the value stores data. There is a limitation on what these names can be.
Every variable name in C must start with a letter; the rest of the name can
consist of letters, numbers and underscore characters.
int count;
Integer variables may hold signed whole numbers (numbers with no fractional
part.
The ‘char’ specifier
A variable of type char is 1 byte long and is mostly used to hold a single
character.
8
9 BASIC @ FRED 2023
Normal characters are simply displayed as is on the screen in the order in which they are
encountered in the string (reading left to right). A format specifier, on the other hand informs
printf( ) that a different type item is being displayed. In this case, the %d, means that an
integer is to be output in decimal format. The value to be displayed is to be found in the
second argument. This value is then output at the position at which the format specifier is
found on the string.
The main output code used with print() and the format they represent are listed below.
Code Format
%c Character
%d or %i Integer
%f Floating point numbers (float, double)
%s String of characters
To use scanf( ) to read an integer value from the keyboard, use the general form:
scanf(“%d”, &int_var-name);
Where int-var-name is the name of the integer variable you wish to receive the value.
The first argument to scanf( ) is a string that determines how the second argument will be
treated. In this case the %d specifies that the second argument will be receiving an integer
value entered in decimal format.
The fragment below, for example, reads an integer entered from the keyboard.
int num;
scanf(“%d”, &num);
The & preceding the variable name means ‘address of’. The values you enter are put into
variables using the variables’ location in memory.
9
10 BASIC @ FRED 2023
When you enter a number at the keyboard, you are simply typing a string of digits. The
scanf( ) function waits until you have pressed <ENTER> before it converts the string into
the internal format used by the computer.
The table below codes and formats used with scanf() for basic types.
Code Format
%c Read a single character
%d or %i Read an integer
%lf Read a double
%s Read a string
Example
This program computes the area of a rectangle, given its dimensions. It first
prompts the user for the length and width of the rectangle and then
displays the area.
#include<stdio.h>
void main()
{
int len, width;
printf(“\n Enter length: “);
scanf (“%d “, &len);
printf(“\n Enter width : ” );
scanf( “ %d “, &width);
printf(“\n The area is %d “, len * width);
}
Enter, compile and run the program
Variable Types
There are two places where variables are declared: inside a function or
outside all functions.
Global variables
Variables declared outside all functions are called global variables and they
may be accessed by any function in your program. Global variables exist the
entire time your program is executing.
Local variables
Variables declared inside a function are called local variables. A local variable
is known to and may be accessed by only the function in which it is declared.
Constants
A constant is a value that does not change during program execution. In other words,
constants are fixed values that may not be altered by the program.
Floating - point constants require the use of the decimal point followed by the
number’s fractional component. For example, 11.123 is a floating point constant.
Character constants are usually just the character enclosed in single quotes; 'a', 'b',
'c'. For example:
10
11 BASIC @ FRED 2023
Types of constants
Constants can be used in C expressions as:
• Direct constants
• Symbolic constants
Direct constants
Here the constant value is inserted in the expression, as it should typically be
For example:
The value 3.14 is used directly to represent the value of PI which never
requires changes in the computation of the area of a circle
Symbolic constants
This involves the use of another C preprocessor, #define.
For example, #define PI 3.14
For example, all occurrences of the symbolic constant PI are replaced with
the replacement text 3.14.
#include<stdio.h>
#define PI 3.14
void main()
{
float radius, area;
printf(“Enter the radius of the circle \n”);
scanf(“%f”, &radius);
area = PI * radius * radius;
printf(“Area is %.2f cm squared “,area);
}
At the time of the substitution, the text such as 3.14 is simply a string of
characters composed of 3, ., 1 and 4. The preprocessor does not convert a
number into any sort of internal format. This is left to the compiler.
The macro name can be any valid C identifier. Although macro names can
appear in either uppercase or lowercase letters, most programmers have
adopted the convention of using uppercase for macro names to distinguish
them from variable names. This makes it easy for anyone reading your
program to know when a macro name is being used.
11