Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: c programming basics
Description: Define programming Explain what a programming language is Describe the structured programming approach Identify the steps of developing a program Explain the characteristics of C language. Discuss the components of a C program Identify various types of program errors Create and compile a program
Description: Define programming Explain what a programming language is Describe the structured programming approach Identify the steps of developing a program Explain the characteristics of C language. Discuss the components of a C program Identify various types of program errors Create and compile a program
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
1 BASIC @ mayunga winton 2017
Chapter One:
Introduction to Programming
Expected Learning Outcomes
Define programming
Explain what a programming language is
Describe the structured programming approach
Identify the steps of developing a program
Explain the characteristics of C language
...
The tools of writing
programs are called programming languages
...
Like human languages, computer languages have a
syntax which defines the language grammar
...
Steps in program development
1
...
The requirements of the all
the program’s phases (Input, Processing, Output) need to be well spelt out
...
Design program
This stage is about specifying how the program will meet the requirements
identified i
...
how it will be implemented
...
3
...
You type the program directly into a
1
2 BASIC @ mayunga winton 2017
window on
the screen and save the resulting text as a separate file
...
The custom is that the text of a C program is stored in a
file with the extension
...
Compiling
This is the translation of the source file must be translated into machine
understandable form - binary numbers
...
obj is produced
...
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
...
exe file
...
You can then run
...
6
...
Programs may have bugs (errors)
...
7
...
This is easily made
possible if you document the program clearly and follow good program design
practices
...
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 supports structured programming
C is a middle level language
...
Factors that make C popular
2
3 BASIC @ mayunga winton 2017
Support for structured programming- It allows programmers to
break down their programs into functions as well as use of control
structures
...
The final code tends to be more compact and
runs quickly
...
Adaptability/ and flexibility - C has been used to develop to
develop complex software such as operating systems (such as Unix
and Windows), Language Compilers, Assemblers ,Text Editors, Print
Spoolers, Network Drivers, Application packages (such as
WordPerfect and Dbase), Language Interpreters, Utilities, et al
...
A simple C program
...
#include
h> allows the program to interact with the
screen, keyboard and file system of your computer
...
main() declares the start of the function, while the two curly
brackets show the start and finish of the function
...
Such a grouping is known as
a compound statement or a block
...
The text to be printed is enclosed in double quotes
...
C is case sensitive, that is, it recognises a lower case letter
and it's upper case equivalent as being different
...
#include
\n”);
printf(“My favorite number is %d because ”, num);
printf(“ it is first
...
Keywords
These are reserved words
that have special meaning
in a language
...
C keywords must be used in lowercase otherwise they will not be recognized
...
A statement specifies an action to be performed by the program
...
A C program consists of one or more functions, and one of them must be
called main, in order for the program to execute
...
When your program begins running, it starts executing the statements inside
the main( ) function, beginning with the first statement after the opening curly
brace - {
...
Library functions are also part of C programs
...
The
functions perform tasks such as program data Input/output, string
manipulations, mathematics, and much more
...
This is C’s
general purpose output function
...
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
...
The \n in line 7 tells the computer to insert a new line after printing the
message
...
Line 8 prints the value of the variable num (1) embedded in the
phrase
...
%d is used to specify the output format for integer
numbers
...
Line11 indicates the value to be returned by the function main( ) when
it is executed
...
Therefore, line 2 could
also be written int main( )
...
Then, why return (0); ? Since all functions are subordinate to main( ), the
function does not return any value
...
In this case, the statement return 0; is not necessary
...
Preprocessor Directives
A preprocessor directive is a statement that performs various manipulations on your source
file before it is actually compiled
...
This is generally used to read in header files for library
functions
Header files contain details of functions used within the library
...
Library header file names are
enclosed in angle brackets, < >
...
e
...
5
6 BASIC @ mayunga winton 2017
Declaration Statements
In C, all variables must be declared before they are used (discussed later)
...
Assignment and expression statements
The statement num =1; (Line 5) is an assignment statement
...
One of the most important escape sequences is \n, which is often referred to as the new line
character
...
6
7 BASIC @ mayunga winton 2017
7
8 BASIC @ mayunga winton 2017
#include
Syntax Errors
They result from the incorrect use of the rules of programming
...
A program that has
syntax errors can produce no results
...
Syntax errors include;
Missing semi colon at the end of a statement
Use of an undeclared variable in an expression
Illegal declaration e
...
int x, int y, int z;
Use of a keyword in uppercase e
...
FLOAT, WHILE
Misspelling keywords e
...
init instead of int
Logic Errors
These occur from the incorrect use of control structures, incorrect calculation,
or omission of a procedure
...
The compiler will
not detect such errors since it has no way of knowing your intentions
...
Semantic Errors
They are caused by illegal expressions that the computer cannot make
meaning of
...
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
...
Variables
A variable is a memory location whose value can change during program
execution
...
Variables
can be declared at the start of any block of code
...
That is:
datatype variable; or
datatype variable1,variable2,…
...
That is: Variables can also be initialised when they are declared
...
int high = 250; /* Maximum Temperature */
int low = -40;
/* MinimumTemperature */
Variable Names
Every variable has a name and a value
...
There is a limitation on what these names can be
...
C recognizes upper and lower case characters as being different (C is casesensitive)
...
Basic Data Types
C supports five basic data types
...
Do not be confused by void
...
9
10 BASIC @ mayunga winton 2017
Type
Character
Integer
Float
Double
Void
Meaning
Character data
Signed whole
number
floating-point
numbers
double precision
floating-point
numbers
Valueless
Keyword
char
int
float
double
void
The ‘int’ specifier
It is a type specifier used to declare integer variables
...
The ‘char’ specifier
A variable of type char is 1 byte long and is mostly used to hold a single
character
...
These are
numbers that have a whole number part and a fractional or decimal
part for example 234
...
The ‘double’ specifier
It is a type specifier used to declare double-precision floating point
variables
...
Using printf( ) to output values
You can use printf( ) to display values of characters, integers and floating - point values
...
For example:
printf(“This prints the number %d ”, 99);
displays This prints the number 99 on the screen
...
The first one is the quoted string and the other is
the constant 99
...
In general, when there is more than one argument to a function, the arguments are separated
from each other by commas
...
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)
...
In this case, the %d, means that an
integer is to be output in decimal format
...
This value
is then output at the position at which the format specifier is found on the string
...
Code
Format
%c
Character
%d or %i
Integer
%f
Floating point numbers (float, double)
%s
String of characters
Inputting values from the keyboard using scanf( )
There are several ways to input values through the keyboard
...
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
...
In this case the %d specifies that the second argument will be receiving an integer
value entered in decimal format
...
int num;
scanf(“%d”, &num);
The & preceding the variable name means ‘address of’
...
When you enter a number at the keyboard, you are simply typing a string of digits
...
The table below codes and formats used with scanf() for basic types
...
It first
prompts the user for the length and width of the rectangle and then
displays the area
...
h>
void main()
{
int len, width;
printf(“\n Enter length: “);
scanf (“%d “, &len);
printf(“\n Enter width : ” );
11
12 BASIC @ mayunga winton 2017
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 exist the
entire time your program is executing
...
A local variable
is known to and may be accessed by only the function in which it is declared
...
In other words,
constants are fixed values that may not be altered by the program
...
For
example –10, 1000 are integer constants
...
For example, 11
...
Character constants are usually just the character enclosed in single quotes; 'a', 'b',
'c'
...
14 * Radius * Radius;
The value 3
...
For example, #define PI 3
...
g
...
g
...
14) before the program is compiled
...
14
...
#include
14
void main()
{
float radius, area;
printf(“Enter the radius of the circle \n”);
scanf(“%f”, &radius);
area = PI * radius * radius;
printf(“Area is %
...
14 is simply a string of
characters composed of 3,
...
The preprocessor does not convert a
number into any sort of internal format
...
The macro name can be any valid C identifier
...
This makes it easy for anyone reading your
program to know when a macro name is being used
Title: c programming basics
Description: Define programming Explain what a programming language is Describe the structured programming approach Identify the steps of developing a program Explain the characteristics of C language. Discuss the components of a C program Identify various types of program errors Create and compile a program
Description: Define programming Explain what a programming language is Describe the structured programming approach Identify the steps of developing a program Explain the characteristics of C language. Discuss the components of a C program Identify various types of program errors Create and compile a program