0% found this document useful (0 votes)
3 views18 pages

201 - C Programming Note

The document provides an overview of computer systems, detailing the components of hardware and software, and categorizing software into system, programming, and application types. It explains computer languages, distinguishing between low-level and high-level languages, and introduces algorithms and flowcharts as essential programming concepts. Additionally, it covers the C programming language, its structure, tokens, variables, constants, and operators, along with examples of arithmetic operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

201 - C Programming Note

The document provides an overview of computer systems, detailing the components of hardware and software, and categorizing software into system, programming, and application types. It explains computer languages, distinguishing between low-level and high-level languages, and introduces algorithms and flowcharts as essential programming concepts. Additionally, it covers the C programming language, its structure, tokens, variables, constants, and operators, along with examples of arithmetic operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

C

PROGRAMMING

COS 201

SUMMARIZED LECTURE NOTE

1
COMPUTER SYSTEMS
A Computer is an electronic device that stores, manipulates and retrieves data. We can
also refer that computer computes the information supplied to it and generates data. A
System is a group of several objects with a process. For Example: Educational System
involves teacher, students (objects). Teacher teaches subject to students i.e., teaching
(process). Similarly a computer system can have objects and processes.
The following are the objects of the computer System
a) User (A person who uses the computer)
b) Hardware
c) Software

A computer system consists of hardware and software.


Computer hardware is the collection of physical elements that comprise a computer
system.

Computer software is a collection of computer programs and related data that provides
the instructions for a computer what to do and how to do it. Software refers to one or
more computer programs and data held in the storage of the computer for some purpose

2
Basically computer software is of three main types System Software:

System software is responsible for managing a variety of independent hardware


components, so that they can work together. Its purpose is to unburden the application
software programmer from the often complex details of the particular computer being
used, including such accessories as communications devices, printers, device readers,
displays and keyboards, and also to partition the computer's resources such as memory
and processor time in a safe and stable manner.
• Device drivers
• Operating systems
• Servers
• Utilities
• Window systems

Programming Software: Programming Software usually provides tools to assist a


programmer in writing computer programs, and software using different programming
languages in a more convenient way. The tools include:
• Compilers
• Debuggers
• Interpreters
• Linkers
• Text editors

Application Software: Application software is developed to aid in any task that benefits
from computation. It is a broad category, and encompasses Software of many kinds,
including the internet browser being used to display this page. This category includes:
• Business software
• Computer aided design
• Databases
• Decision making software
• Educational software
• Image editing

COMPUTER LANGUAGES
In order to communicate with the computer user also needs to have a language that
should be understood by the computer. For this purpose, different languages are

3
developed for performing different types of work on the computer. Basically, languages
are divided into two categories according to their interpretation.

1. Low Level Languages.


2. High Level Languages.
Low Level Languages Low level computer languages are machine codes or close to it.
Computer cannot understand instructions given in high level languages or in English. It
can only understand and execute instructions given in the form of machine language i.e.
language of 0 and 1. There are two types of low level languages:
 Machine Language.
 Assembly Language

Machine Language:
It is the lowest and most elementary level of Programming language and was the first
type of programming language to be Developed. Machine Language is basically the only
language which computers Can understand. In fact, a manufacturer designs a computer to
obey just one Language, its machine code, which is represented inside the computer by a
String of binary digits (bits) 0 and 1. The symbol 0 stands for the absence of an Electric
pulse and 1 for the presence of an electric pulse. Since a computer is capable of
recognizing electric signals, it understands machine Language.

Assembly Language
It was developed to overcome some of the many inconveniences of machine language.
This is another low-level but a very important language in which operation codes and
operands are given in the form of alphanumeric symbols instead of 0’s and 1’s. These
alphanumeric symbols will be known as mnemonic codes and can have a maximum up to
5 letter combinations e.g. ADD for addition, SUB for subtraction, START, LABEL etc.
Because of this feature, it is also known as „Symbolic Programming Language‟. This
language is also very difficult and needs a lot of practice to master it because very small

4
English support is given to this language. The language mainly helps in compiler
orientations. The instructions of the Assembly language will also be converted to
machine codes by language translator to be executed by the computer.

High-Level Languages
High-level computer languages give formats close to the English language and the
purpose of developing high-level languages is to enable people to write programs easily
and in their own native language environment (English). High-level languages are
basically symbolic languages that use English words and/or mathematical symbols rather
than mnemonic codes. Each instruction in the high-level language is translated into many
machine language instructions thus showing a one-to-many translation

Types of High-Level Languages


Many languages have been developed for achieving a variety of tasks; some are fairly
specialized, and others are quite general purpose. On a general note, the following are
examples of high-level programming languages:

BASIC (Beginners All Purpose Symbolic Instruction Code).


 FORTRAN (Formula Translation).
 PL/I (Programming Language, Version 1).
 ALGOL (Algorithmic Language).
LISP (List Processing).
 Prolog (Program in Logic).
C
C++
 Java
 Visual Basic
 Visual Java
 Visual C

ALGORITHM
An algorithm is a finite sequence of instructions, each of which has a clear meaning and
can be performed with a finite amount of effort in a finite length of time. No matter what
the input values may be, an algorithm terminates after executing a finite number of
instructions. We represent an algorithm using a pseudo-language that is a combination of
the constructs of a programming language together with informal English statements. The

5
ordered set of instructions required to solve a problem is known as an algorithm. The
characteristics of a good algorithm are:
i. Definiteness
ii. Effectiveness
iii. Finiteness – the algorithm stops after a finite number of instructions are executed.
The program must be terminated.
1 Input – the algorithm receives input.
2 Output – the algorithm must produce output.

Example.
Write an algorithm to find out number is odd or even?
Ans.
step 1 : start
step 2 : input number
step 3 : rem=number mod
2 step 4 : if rem=0then print "number even" else print "number odd" endif
step 5 :stop

FLOWCHART
A flowchart is a diagrammatic representation of an algorithm. A flowchart is very helpful
in writing programs and explaining programs to others.

Symbols Used In Flowchart


Different symbols are used for different states in flowchart, For example: Input/Output
and decision making has different symbols. The table below describes all the symbols
that are used in making flowchart.

6
Examples of flowcharts in programming
Draw a flowchart to add two numbers entered by user.

7
C PROGRAMMING
What is C?
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972.
It was designed and written by Dennis Ritche. Dennis Ritchie is known as the founder of
c language. It was developed to overcome the problems of previous languages such as B,
BCPL etc. Initially, C language was developed to be used in UNIX operating system.

Facts about C
•C was invented to write an operating system called UNIX (UNiplexed Information
Computing System, a multitasking, multi-user OS that can run on many different types of
computer).
• C is a successor of B language which was introduced around1970
• The language was formalized in 1988 by the American National Standard Institute
(ANSI).
• By 1973 UNIX OS almost totally written in C.
• Today C is the most widely used System programming language.
• Most of the state of the art software have been implemented using C

Why to use C?
C was initially used for system development work, in particular the programs that make
up the operating system. C was adopted as a system development language because it
produces code that runs nearly as fast as code written in assembly language. Some
examples of the use of C might be:

• OperatingSystems
• LanguageCompilers
• Assemblers
• Text Editors
• PrintSpoolers
• NetworkDrivers
• ModernPrograms
• DataBases
• LanguageInterpreters
• Utilities

8
C ProgramFile
All the C programs are writen into text files with extension ".c" for example hello.c. You
can use "vi" editor to write your C program into a file.

BASIC STRUCTURE OF C PROGRAMMING

1. Documentation section: The documentation section consists of a set of comment lines


giving the name of the program, the author and other details, which the programmer
would like to use later.
2. Link section: The link section provides instructions to the compiler to link functions
from the system library such as using the #includedirective.
3. Definition section: The definition section defines all symbolic constants such as using
the #definedirective.
4. Global declaration section: There are some variables that are used in more than one
function. Such variables are called global variables and are declared in the global
declaration section that is outside of all the functions. This section also declares all the
user-defined functions.
5. main () function section: Every C program must have one main function section. This
section contains two parts; the declaration part and the executable part

9
 Declaration part: The declaration part declares all the variables used in the
executable part.
 Executable part: There is at least one statement in the executable part.
These two parts must appear between the opening and closing braces. The
program execution begins at the opening brace and ends at the closing
brace. The closing brace of the main function is the logical end of the
program. All statements in the declaration and executable part end with a
semicolon.
6. Subprogram section: If the program is a multi-function program then the subprogram
section contains all the user-defined functions that are called in the main () function. User
defined functions are generally placed immediately after the main () function, although
they may appear in any order.

First C Program
Before starting the abcd of C language, you need to learn how to write, compile and run
the first c program.
To write the first c program, open the C console and write the following code:

1. #include <stdio.h>

2. void main(){

3. printf("Hello C Language");

4. return 0;

5. }

#include <stdio.h> includes the standard input output library functions. The printf()
function is defined in stdio.h .
void main() The main() function is the entry point of every program in c language. The
void keyword specifies that it returns no value.
printf() The printf() function is used to print data on the console.
getch() The getch() function asks for a single character. Until you press any key, it
blocks the screen.

C TOKENS

10
The smallest individual units are known as tokens. C has six types of tokens.
1: Identifiers
2: Keywords
3: Constants
4: Strings
5: Special Symbols
6: Operators

Identifiers:
Identifiers refer to the names of variables, constants, functions and arrays. These are user-
defined names is called Identifiers. These identifier are defined against a set of rules.
Rules for an Identifier
1. An Identifier can only have alphanumeric characters (a-z, A-Z, 0-9) and underscore
(_).
2. The first character of an identifier can only contain alphabet (a-z, A-Z) or underscore
(_).
3. Identifiers are also case sensitive in C. For example name and Name are two different
identifier in C.
4. Keywords are not allowed to be used as Identifiers.
5. No special characters, such as semicolon, period, whitespaces, slash or comma are
permitted to be used in or as Identifier.
6. C’ compiler recognizes only the first 31 characters of an identifiers.

Keywords:
A keyword is a reserved word. All keywords have fixed meaning that means we cannot
change. Keywords serve as basic building blocks for program statements. All keywords
must be written in lowercase. A list of 32 keywords in c language is given below:

11
Note: Keywords cannot be used as a variable name, constant name etc.

Data Types/Types:
To store data the program must reserve space which is done using datatype. A datatype is
a keyword/predefined instruction used for allocating memory for data. A data type
specifies the type of data that a variable can store such as integer, floating, character etc.
It used for declaring/defining variables or functions of different types before to use in a
program.

There are 4 types of data types in C language.

12
Variables
A variable is a name of memory location. It is used to store data. Variables are
changeable, we can change value of a variable during execution of a program. . It can be
reused many times. Note: Variable are nothing but identifiers.
Rules to write variable names:
1. A variable name contains maximum of 30 characters/ Variable name must be up to 8
characters.
2. A variable name includes alphabets and numbers, but it must start with an alphabet.
3. It cannot accept any special characters, blank spaces except under score (_).
4. It should not be a reserved word.

Declaration of Variables:
A variable can be used to store a value of any data type. The declaration of variables must
be done before they are used in the program. The general format for declaring a variable.
Syntax: data_type variable-1,variable-2,------, variable-n; Variables are separated by
commas and declaration statement ends with a semicolon.
Examples: int x,y,z;
float a,b;
char m,n;
we can also assign a value to a variable at the time of the variable is declared.
The general format of declaring and assigning value to a variable is:

Syntax: data_type variable = constant;

Example; int x=100;


float a=12.25;
char m=‟f‟;

13
Types of Variables in C There are many types of variables in c:
1. local variable
2. global variable
3. static variable

Constants
Constants refer to fixed values that do not change during the execution of a program.
Note: constants are also called literals.

OPERATORS AND EXPRESSIONS:


Operators: An operator is a Symbol that performs an operation. An operators acts some
variables are called operands to get the desired result.
Example : a+b;
Where a,b are operands and + is the operator. Types of Operator:
1) Arithmetic Operators.
2) Relational Operators.
3) Logical Operators.
4) Assignment Operators.
5). Unary Operators.
6) Conditional Operators.
7) Special Operators.
8) Bitwise Operators.
9) Shift Operators.

Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction
and multiplication on numerical values (constants and variables). C Program to
demonstrate the working of arithmetic operators

#include <stdio.h>
void main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);

14
c = a*b;
printf("a*b = %d \n",c);
c=a/b;
printf("a/b = %d \n",c);
c=a%b;
printf("Remainder when a divided by b = %d \n",c);
}

Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

Relational Operators.
A relational operator checks the relationship between two operands. If the relation is true,
it returns 1; if the relation is false, it returns value 0. Operands may be variables,
constants or expressions. Relational operators are used in decision making and loops.

Logical Operators.
These operators are used to combine the results of two or more conditions. An expression
containing logical operator returns either 0 or 1 depending upon whether expression
results true or false. Logical operators are commonly used in decision making in C
programming.

15
Assignment Operators.
Assignment operators are used to assign a value (or) an expression (or) a value of a
variable to another variable.
Syntax: variable name=expression (or) value (or) variable
Example : x=10;
y=a+b;
z=p;

Read about Unary Operators, Conditional Operators, Special Operators and Bitwise
Operators.

16
Expressions Expressions:
An expression is a combination of operators and operands which reduces to a single
value. An operator indicates an operation to be performed on data that yields a value. An
operand is a data item on which an operation is performed.
A simple expression contains only one operator.
Example: 3+5 is a simple expression which yields a value 8, -a is also a single
expression.
A complex expression contain more than one operator.

Example: complex expression is 6+8*7.

Operator Precedence:
Arithmetic Operators are evaluated left to right using the precedence of operator when the
expression is written without the parenthesis. There are two levels of arithmetic operators
in C.
1: High Priority * / %
2: Low Priority + -.
Arithmetic Expression evaluation is carried out using the two phases from left to right.
1: First phase: The highest priority operator are evaluated in the 1st phase.
2: Second Phase: The lowest priority operator are evaluated in the 2nd phase.

17
Whenever parentheses are used, the expressions within parentheses have highest priority.
If two or more sets of parenthesis appear one after another. The expression contained in
the left-most set is evaluated first and the right-most in the last.

1:7

Rules for Evaluation of Expression:


1: Evaluate the sub-expression from left to right. If parenthesized.
2: Evaluate the arithmetic Expression from left to right using the rules of precedence.
3: The highest precedence is given to the expression with in parenthesis.
4: When parentheses are used, the expressions within parentheses assume highest
priority.
5: Apply the associative rule, if more operators of the same precedence occurs.

18

You might also like