Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Module 6a_Introduction to C and Program Structure

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Module 6a_Introduction to C and Program Structure

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

www.covenantuniversity.

Raising a new Generation of Leaders

Applied Computer
Programming I
Alpha Semester
GEC 215
6.1 C Language Overview
6.1.1 History of C Programming Language
• C is a programming language which was
born at “AT & T’s Bell Laboratory” of USA in
1972.
• It was written by Dennis Ritchie, that is why
he is also called the father of C
programming language.
• C language was created for a specific
purpose i.e. designing the UNIX operating
system (which is currently the base of many
UNIX based OS)
2
• C was intended to be useful to
allow busy programmers get
things done.
• Its use quickly spread beyond
Bell Labs in the late 70’s
because of its long list of strong
features.

3
6.1.2 Unique Features of C Language
i) Low level language support.
ii) Powerful and feature rich.
iii) Program portability.
iv) High level features.
v) Bit manipulation.
vi) Efficient use of pointers.
vii) Modular programming.

4
6.1.3 Applications Of C Language
i. To create computer
applications.
ii. Used in writing embedded
softwares.
iii. To develop firmware for
various electronics, industrial
and communications products,
which use micro-controllers.
5
iv) It is used in developing
verification software, test code,
simulators etc. for various
applications and hardware products.
v) For creating compilers for other
languages.
vi) Used to develop different
Operating Systems.
vii) The core of different 2D & 3D
games are written in C language.
6
6.2 C Programs
• A C program can vary from 3
lines to millions of lines and it
should be written into one or
more text files with extension
".c"; for example,
hello.c.
• You can use "vi", "vim" or any
other text editor to write your C
program into a file.
7
• In order to set up your
environment for C programming
language, you need the
following two software tools on
your computer.
(a) Text Editor and
(b) The C Compiler.

8
6.2.1 Text Editor
• Text Editor is is used to type C
programs.
• The name and version of text
editors can vary on different
operating systems. For example,
Notepad will be used on Windows,
and vim or vi can be used on
windows as well as on Linux or
UNIX.
9
• The files you create with your
editor are called the source files
and they contain the program
source codes.
• The source files for C programs
are typically named with the
extension ".c".

10
6.2.2 The C Compiler
• The source code written in source file is
the human readable source for your
program.
• It needs to be "compiled", into machine
language so that your CPU can actually
execute the program or the instructions
given.
• The compiler is a program that translates
software written in source code into
executable instructions that a computer
can understand.
11
• The most frequently used and
freely available compiler is the
GNU C/C++ compiler,
• Otherwise you can have
compilers either from MS, HP or
Solaris if you have the respective
operating systems.

12
6.2.3 Installing the GNU C/C++
Compiler on Linux machine
• Check whether GCC is installed on your system
by entering the following command from the
command line
$ gcc –v.
• If you have GNU compiler installed on your
machine, then it should print a message
showing that it is installed.
• If GCC is not installed, then you will have to
install it yourself using the detailed instructions
available at http://gcc.gnu.org/install/
• Alternatively, you can install VS Code or Code
Block.
13
6.2.4 Program Structure
• A C program basically consists of
the following parts −
 Preprocessor Commands
 Functions
 Variables
 Statements & Expressions
 Comments
• Let us look at the example below to
further explain C program structure.
14
Exercise 6.1
1. #include <stdio.h>
2. int main() {
3. /* my first program in C */
4. printf("Hello, World! \n");
5. return 0;
6. }

15
• The first line of the program #include
<stdio.h> is a preprocessor command,
which tells a C compiler to include stdio.h
file before going to actual compilation.

• The next line int main() is the main


function where the program execution
begins.

• The next line /*...*/ will be ignored by the


compiler and it has been put to add
additional comments in the program. Such
lines are called comments in the program.16
• The next line printf(...) is another
function available in C which
causes the message "Hello,
World!" to be displayed on the
screen.
• Braces{ } use to group all
statements together.
• The next line return 0; terminates
the main() function and returns
the value 0.
17
6.2.5 C Program Execution

Step1:Edit
• First Write C Program using Text
Editor , such as vim
• Save Program by using [.C]
Extension.
• File Saved with [.C] extension is
called “Source Program

18
Step 2 : Compiling
• C Source code with [.c] extension is
given as input to compiler and
compiler convert it into Equivalent
Machine Instruction.
• In Linux C program can be compiled
by entering gcc filename at the
command line.
• Compiler Checks for errors . If source
code is error free then Code is
converted into Object File [.obj].
19
Step 3 : Checking Errors
• During Compilation Compiler will
check for error, If compiler finds
any error then it will report it.
• User have to re-edit the program.
• After re-editing program , Compiler
again check for any error.
• If program is error-free then
program is linked with appropriate
libraries.
20
Step 4 : Linking Libraries
• Program is linked with included
header files.
• Program is linked with other
libraries.
• This process is executed by
Linker

21
Step 5 : Error Checking
• If run time error occurs then
“Run-time” errors are reported
to user.
• Again programmer have to
review code and check for the
solution

22
6.3 Basic Syntax
6.3.1 Tokens in C
• C tokens are the basic buildings blocks in C
language which are constructed together to
write a C program.
• Each and every smallest individual units in a C
program are known as C tokens.
• The tokens can be a keyword, an identifier, a
constant, a string literal, special symbol or an
operator.
• For example, the following C statement
consists of five tokens −
printf("Hello, World! \n");
23
The individual tokens are −
printf
(
"Hello, World! \n"
)
;
6.3.1.1 Semicolons
• In a C program, the semicolon is a
statement terminator.
• That is, each individual statement must be
ended with a semicolon. It indicates the end
of one logical entity
24
Given below are two different
statements −
printf("Hello, World! \n");
return 0;

25
6.3.1.2 Comments
• Comments are like helping text in
your C program and they are
ignored by the compiler.
• They start with /* and terminate with
the characters */ as shown below −
/* my first program in C */
• You cannot have comments within
comments and they do not occur
within a string or character literals.
26
Example 6.2:
1. int main()
2. {
3. int x, y, total;
4. x = 10, y = 20;
5. total = x + y;
6. printf ("Total = %d \n", total);
How many tokens?

27
6.3.1.3 Identifiers
• Each program elements in a C
program are given a name
called identifiers.
• Names given to identify
Variables, functions and arrays
are examples for identifiers.
• x, y, total and main are
identifiers from Example 6.2
28
• These identifier are defined against a set
of rules.
These rules are:
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 ( _ ).
29
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.

30
Some other valid identifiers are-
total
sum
average
_x
y_
mark_1

31
6.3.1.4 Keywords
• Keywords are preserved words that have
special meaning in C language.
• Keywords are referred names for
compiler, they can’t be used as variable
name.
• The meaning has already been described.
• These meaning cannot be changed.
• There are total 32 keywords in C
language.

32
Table 6.1 List of Keywords
auto double int struct

break else long switch

case enum register typedef

const extern return union

char float short unsigned

continue for signed volatile

default goto sizeof void

do if static while

33
Table 6.2: Differences between
Identifier and Keyword
Keyword Identifier

Predefined word. User defined word

Must be written in Lowercase Can be written in lowercase


or uppercase

Has fixed meaning Must be meaningful in the


program

34
Exercise 6.1:
1. #include <stdio.h>
2. // Variable declaration:
3. extern int a, b;
4. extern int c;
5. extern float f;
6. int main () {
7. /* variable definition: */
8. int a, b;
9. int c;

35
10. float f;
11. /* actual initialization */
12. a = 10;
13. b = 20;
14. c = a + b;
15. printf("value of c : %d \n", c);
16. f = 70.0/3.0; printf("value of f : %f \n", f);
17. return 0;
18. }

a) What are the keywords in the above source code?


b) Type the code in your editor, run and comment
on the outputs.

36

You might also like