05-Basics of C Programming
05-Basics of C Programming
05-Basics of C Programming
Programming Fundamentals
§ 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
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
History of C
UNIX developed c. 1969 -- DEC PDP-7 Assembly
Language
§ Source
§ Text form using the C statements
§ Compiler
§ Convert the source code to machine language
C Types
Floating-
Void Integral Derived
point
data_type variable_name;
Constants
§ Constants are data values that cannot be changed during
the execution of a program.
§ 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
Operators
Unary Binary
Assignment Operator
§ The assignment operator which is the equal sign ( = ). If you
write c = y in a C program, the value of y is assigned to c.
§ In assigning, the right side can be any expression and the left
side must be a variable name.
Mathematical Operators
§ C mathematical operators can be classified into two: unary
and binary
§ 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.
type_specifier function_name(parameter_declaration_list);
§ 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;
}
Scope
§ Scope determines the region of the program in which a
defined object is visible, that is, the part of the program in
which you can use the its name.
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();
}