[go: up one dir, main page]

0% found this document useful (0 votes)
27 views20 pages

01 - Structure of A C Program

Uploaded by

Jagadish B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views20 pages

01 - Structure of A C Program

Uploaded by

Jagadish B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Structure of a C Program

Course Leader:
Jishmi Jos Choondal

1
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Objectives
• At the end of this lecture, student will be able to
– Explain the structure of a C program
– Explain the building blocks of C programs

2
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Contents
• Introduction to C programming
• Tokens, keywords, identifiers and constants
• Expressions, Blocks and Statements

3
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Program Development Cycle

Analyze the problem

Design the solution algorithm

Design the user interface

Write the code

Test and debug the program

Complete the documentation

4
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
C Language
• Developed by Dennis Ritchie at Bell Laboratory
• Procedure oriented language
• Evolved from B, which evolved from BCPL

5
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Phases of C Programs

6
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Structure of a C Program
Documentation section /*This is a program to calculate perimeter
of circle */
link section #include<stdio.h>
definition section #define PI 3.14
Global declaration section float area(float);

main() function int main(int argc, char *argv[]){


Braces printf(“Perimeter of circle with radius
statements %f is %f\n”,2.0f,area(2.0));
….. }
user defined functions float area(float radius){
return 2*PI*radius;
}
7
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
main() Function
• main() is a part of every C program
• C programs contain one or more functions, one of which
must be main
• Every program in C begins executing at the main()

• Block – the pair of braces {} and the portion of program


between the braces

• Statement terminator – every statement must end with a ;

8
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Question
• We all know one or more human languages. What is a
human language made up of?
– Alphabets
– Words
– Sentences
– Paragraphs
• A computer program
– Character set
– Tokens
– Statements
– Functions

9
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Character Set
• Alphabets
 Lower case letters – a to z
 Upper case letters – A to Z

• Digits
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

• Special characters
~ % | @ + < _ - > ^ # = & $ / ( * \ ) ′ : [ “ ; ] ! , { ?
. }

10
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
C Tokens
• C tokens : the smallest individual units
 Keyword – float, while, for, int,….
 Identifier – main( ) , amount, sum, …
 Constants – -13.5, 500, …
 Strings – “ABC”, “MCA”, …
 Operators – + - * % …
 Special Symbols – [ ] { }…

11
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Keywords used in C
• Fixed meaning, cannot be changed
• Reserved words
• Cannot be used as a variable or function name
• Basic building blocks
• All in Lowercase

12
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Keywords used in C contd.

13
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Identifiers
• Name given to a function or variable memory location
• An identifier is a series of characters consisting of letters,
digits and underscores “_”
• Should start with a letter or underscore
• Can be any length
– only the first 31 characters are required to be recognized by
ANSI C compilers
– Keep identifiers 31 characters or less for portability and fewer
problems

14
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Identifiers - Examples
• CAN
– contain a number elsewhere h2o
– be of mixed cases Xsquared
– contain or begin with an underscore _height_

• CANNOT
– start with a number 2i
– contain any arithmetic operators r*s+t
– contain any other punctuation marks #@x%£!!a
– be a C keyword struct
– contain a space my var

15
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Constants
• Fixed values
• Does not changes during execution of program
• Numeric constant – Integer (decimal, octal, hexadecimal)
and Real
• Character constant :
– Single character constant
– String constant
– Backslash character constant

16
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Escape Sequence
• Escape character – backslash \
• When encountering a backslash in a string, the compiler
looks ahead at the next character and combines it with \
to form escape sequence

17
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Common Escape Sequences

18
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary
• All C programs follow a predefined structure
• Words of a computer programming language are known
as Tokens
– They can be Reserved Keywords, Identifiers, Operators and
Constants
• Expressions are a collection of operands and operators
• Statements are always terminated
• Statements can be composed of declarations, expressions,
control structures or a function call

19
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Further Reading
Allain, A. (2005) Introduction to C, available at
http://www.cprogramming.com/tutorial/c/lesson1.html (accessed
22 July 2014).
Kernighan, B. W. and Richie, D. (1992) The C Programming Language.
2nd ed., New Delhi:PHI.

20
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences

You might also like