Académique Documents
Professionnel Documents
Culture Documents
UNIT 2
elf © 2015
Unit 2: Data, Modules, Expressions and
Statements
• Features of good programming design
• Basics of C language
• Data Types
• Literals, Variables, and constants
• Arithmetic operations (Precedence and
Associativity)
• Modularizing a Program
Features of Good Programming
Design
UNIT 2: DATA, MODULES, EXPRESSIONS AND STATEMENTS
Programming style
• Programming style is a term used to describe the
effort a programmer should take to make his or her
code easy to read and easy to understand.
• Good organization of the code and meaningful
variable names help readability, and liberal use of
comments can help the reader understand what the
program does and why.
• Style Principle
“Structure and document your program the way you
wish other programmers would.”
Good Programming Practices
Indentation
Internal Documentation
External Documentation
Miscellaneous Comments
Basics of C Language
UNIT 2: DATA, MODULES, EXPRESSIONS AND STATEMENTS
History of C
UNIX developed c. 1969 -- DEC PDP-7 Assembly
Language
• Small size
• Extensive use of function calls
• Loose typing -- unlike PASCAL
• Structured language
• Low level (BitWise) programming readily
available
• Pointer implementation - extensive use of
pointers for memory, array, structures and
functions.
Uses of C Language
• Source
• Text form using the C statements
• Compiler
• Convert the source code to machine language
• Preprocessor Commands
• Type definitions
• Function prototypes -- declare function types
and variables passed to function.
• Variables
• Functions
C Program Structures
• Function
• Building blocks of C
• Collection instruction that perform one or more task
called statements.
• Arguments
• Information passed from calling function to the
called function
• Braces { }
• Block of codes, marks the beginning & end of the
block of statements
• Semi-colon ;
• Terminator for each statements
C Program Structures
• Header Files
• Head of the program
• main()
• Main module of the program
• First function called when program execution begins
• C is a case sensitive language
Data Types
UNIT 2: DATA, MODULES, EXPRESSIONS AND STATEMENTS
Types
C Types
Floating-
Void Integral Derived
point
• Syntax
data_type variable_name;
Constants
• Constant Representation
• Boolean constants
• Character constants
• Integer constants
• Real constants
• Complex constants
• String constants
Coding Constants
• Literal Constants
• Unnamed constants used a specific data
• Example: a = b +5;
• Defined Constants
• Use preprocessor command define; placed at the
beginning of the program
• Example:
#define SALES_TAX_RATE .0825
Coding Constants
• Memory Constants
• Indicate that the data cannot be changed ; give a
type and size to a named object in memory
Unary Binary
Assignment Operator
• Unary operators
• C has two unary operators: the increment and the
decrement
• Prefix or infix : ++x for increment and - - x for
decrement
• Postfix : x++ for increment and x - - for decrement
Mathematical Operators
• Binary operators
• Mathematical operators perform math operations
such as:
• addition ( + ), subtraction ( - ), multiplication ( * ),
division ( / ) and modulus ( % ).
Operator Precedence
• An expression contains more than one operator.
The order in which operations are performed
pose a significance. This order is called operator
precedence. Operations with higher precedence
are performed first when an expression is
evaluated.
Operators Relative Precedence
++ - - 1
* / % 2
+ - 3
• If an expression contains more than one operator
with the same precedence level, the operators are
performed in left to right order as they appear in
the expression.
• Building blocks of C
• Function Prototype
type_specifier function_name(parameter_declaration_list);
• type_specifier
• specifies the type of the value that function will
return using the return statement
• default return an integer result
• parameter_declaration_list
• comma separated list of variable types & name that
will receive the values of the arguments when
function is called
• example: func (int x, int y, float z)
Function Arguments
NO RETURN VALUE, WITH PARAMETER
• Example 1
#include<stdio.h>
void sqr (int x)
void sqr (int x);
main() {
{ prinft(“%d squared is %d”,x, x*x);
int num = 100;
getch();
sqr (num);
}
}
Function Arguments
NO RETURN VALUE, WITH PARAMETER
• Example 2
#include<stdio.h>
void mul (int a, int b); void mul (int a, int b)
main() {
{ printf(“%d”, a*b);
mul (10, 11); getch();
} }
Function Returning Values
WITH RETURN VALUE, WITH PARAMETER
• Example 1
#include<stdio.h>
int mul (int a, int b);
main()
{
int answer;
answer = mul (10, 11);
printf(“The answer is % d”, answer);
getch();
}
int mul (int a, int b)
{
return a*b;
}
Example
main
func1()
{
func2();
printf(“count is %d”,
count);
}
Calling Functions
• Call by Value
• copies value of an argument into the formal
parameter of the subroutine
• Call by Reference
• the address of an argument is copied into the
parameter
Calling Functions
• Pointer Operator
• Ampersand (&)
• unary operator that returns the memory address of its
operand (“the address of ”)
• Asterisk (*)
• returns the value of variable located at the address that
follows (“value at address”)
Call by Value
#include<stdio.h>
main()
{
int t=10;
printf(“%d %d”, sqr(t), t);
getch();
}