C Programming 2
C Programming 2
Type declaration
C provides
id a wide
id range off types. Th
The most common are
• Example
• int a,b;
scanf(“%d”,&a);
f(“%d” & )
b=(a>=10?5:15);
Getting Started with C
C Instructions
• Loop control structures
• Repeating a segment of the program either a specified
number of times or until a given condition is met.
• for
• while
• do ‐ while
Getting Started with C
C Instructions: Loop control structures
• for Statement
• Allows the programmer to execute a block of code for a
specified number of times
• The for loop works well where the number of iterations of
the loop is known
• The head of the loop consists of three parts separated by
semicolons
• Syntax
S t
• for (initial‐statement; condition; iteration‐statement)
body‐statement;
Getting Started with C
C Instructions: Loop control structures
• for Statement
• The head of the loop consists of three parts separated by
semicolons
• Syntax
• for (initial‐statement; condition; iteration‐statement)
body statement;
body‐statement;
• The first is run before the loop is entered. This is usually the initialisation of
the loop variable. In most cases setting loop counter to initial value
• The second is a test, the loop is exited when this returns false.
• The third is a statement to be run every time the loop body is completed.
This is usually an increment/decrement of the loop counter.
Getting Started with C
C Instructions: Loop control structures Shorthand operators
• for Statement Increment operator: ++
Decrement operator: ‐ ‐
• Example:
xample: Post‐increment: i++;;
Pre‐increment ++i;
• for(i=1; i<=10; i=i+1)
printf(“\n%d”
printf( \n%d , i); Oper: Short: Expr:
+= X+=2; X=X+2;
• for(i=10; i>=1; i=i‐1) ‐= X‐=2; X=X‐2;
printf(“\n%d”
printf( \n%d , i); *
*= X* 2
X*=2; X X*2
X=X*2;
/= X/=2; X=X/2;
• Nested loops %= X%=2 X=X%2;
Example
do {
printf("Enter 0 to terminate :");
scanf("%d", &input_value);
} while (input_value != 0)
Getting Started with C
C Instructions: Loop control structures
The switch Statement
• Another form of the multi way decision.
decision
• Can only be used in certain cases where;
• Only one variable is tested, all branches must depend on the value of
that variable.
• The variable must be an integral type. (int, long, short or char).
• Each possible value of the variable can control a single branch
A[0] A[1] [ ]
A[8]
Getting Started with C
C Instructions:
Array:
• Declaration:
• <datatype> arrayName[size];
• int marks[9];
• marks[0]=50;
k [0] 50
• marks[1]=65;
0 1 2 3 4 5 6 7 8
marks
50 65
marks[0] marks[8]
marks[1]
Getting Started with C
C Instructions:
Array: Character arrays (strings)
• Strings are arrays of characters
• The special character ‘\0’ (NULL) is used to indicate the end of
the string
• Example
char
h name[40];
[40] name
0 1 2 3
name[0]='S'; S a m \0
name[1]='a';
name[2]='m';
name[3]='\0';
Getting Started with C
C Instructions:
Array: Character arrays (strings)
String manipulation using string.h
Integer type
t pe Con ersion
Conversion Si e in Bytes
Size B tes
Signed short %h 2
Signed
g int %d 2 or 4
Signed long int %ld 4
Unsigned short int %H 2
Unsigned int %u 2 or 4
unsigned long int %lu 4
Getting Started with C
C Instructions:
Reading and Printing Numbers
Float type Conversion Size in Bytes
float %f 4
double %lf 8
Long double %L 8
Hexa %0x
Octal %o
V i bl Scopes
Variable S and
d Functions
F ti
Scope and Class
Scope: The area of the program where the variable is valid
Global variables: A variable which is valid everywhere in the
program. Its scope is the whole program
Local variables: The scope of a local variable is limited to the
block where it is declared. A block is a section of code
enclosed in { }
• possible to declare local variables with same names as
global (That hides the global variable to that block)
Variable Scopes and Functions
Scope and Class
Class: It tells the compiler where to store the data, such as on the
stack or on the heap
Class of a variable may either permanent or temporary
Global variables are permanent , and are created and initialized
before the program starts. (in heap)
Temporary (automatic) variables are allocated from Stack at the
beginning of the block. All local variables are temporary unless
th are static
they t ti
Example: count number of visits to a function
Variable Scopes and Functions
Functions
A group ( unit) of commonly used code that can be used
repeatedly ‐‐ Reuse is one of the main reasons for having a
function. E.g. printf()