C Programming
Ganesan C
Lead Engineer – HCL Technology
Test
Use
include the
use one header file preprocessor
How to restrict the header files to be included only once header file
1 Use only onec to include all other derivatives
in the program ? using double
header files. conditioning to
quotes
include
which function(s) requires return key conformation getch() and
2 All of them getche() No of them
getc(), getch(), getche() ? getc()
Limited
What is kind of memory damage is happening due to
3 Memory leak Truncation Fragmentation memory
malloc()
allocation
2
History of C Programming
Invented and Implemented by Dennis Ritchie in 1972
at AT&T Bell Laboratory on DEC PDP-11 under UNIX
platform from BCPL.
ANSI – 1989 to 1990 [C89]
Dennis M Ritchie
ANSI/ISO
Amendment 1 in 1995 to C, becomes base document
for Standard C++ and defines the C subset of C++
A Middle Level Language - MLL (since it combines the
control of LLL language and flexibility of HLL)
A portable Language DEC PDP-11
Brief of C
It supports basic data types [integer, character, floating point]
It contains build-in data types [double, enumerator, structure, union]
It is not strongly typed language. It allows almost all type casting.
No run time check. It is up to the programmer.
It allows direct manipulations of bits, bytes and word and memory
(pointer) and it makes it to do system-level Programming.
It contains very less number of Keywords [32+5]
Case sensitive language
4
Advantage of C
It is structured Language not block-structured
It allows several looping methods and almost avoids goto statement.
Modular programming ability – Sub-routines
Code-Block – placing code inside a block.
Complier , not an Interpreter
5
C Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
_Bool _Imaginary restrict
_Complex inline
6
General form of C programming
Pre
processor
derivative
Structure &
Union
Declarations
Global Variable
declarations
Sub-Function Declarations
Main function •Local variable declarations
Sub-Function definitions
7
Pre-processor Derivatives
Starts with ‘#’ symbol
Not required ‘;’ at the end of the statement
One line statement can be extended by ‘\’
Used to execute certain codes before compiling.
Can be used to decide the environment of complier
8
Some derivatives
#include -> includes any file to the source code
#ifdef & #ifndef
#if - #else -#endif
#define
#pragma
auto_inline, comment, once, etc
for more: http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
9
I/O Statements
Formatted an un-formatted Statements
Un-Formatted:
• getch(), getche(), getchar()
• putch(), putchar()
Formatted:
• scanf(), printf(), sscanf(), sprintf()
10
Variables
What is variable?
• Representation of data in memory
Types of Variables
• Standard, Build-in
Standard: integer, character, floating point
Build-in: double, pointers, structure, union
Declaration
<storage class> <type modifier> <data type> [variable name];
11
Storage class and scope
Defines the life time of the variable:
1. auto
2. static
3. register
4. extern
Files are Global scope
12
Type modifier
Modifies the basic character of the data type
Sign modifiers
• signed
• unsigned
length modifiers
• long
• short
13
Arithmetic and logic
Constants, Expressions
Arithmetic, Relational & Logic Operators
+, -, *, /, %
<, >, <=, >=, !, !=
|, &, !, ||, &&, ^, ~, <<, >>
Increment & decrement
++, --
Post and Pre increment & decrement
14
Conditional Statements
If
If-else
If-else if
Nested if
Switch-case
What is mean by if(1) and if (0)?
15
Looping Statements
For
While
Do-while
Different between while and do-while
Infinite loop
Nested usage
Common mistakes
16
Functions
What is function and sub-function?
Main() function
Prototype declaration, implementation
Function signature(return type, arguments)
What is void functions?
17
Functions continued
Call by value
Reduces the damage to the program, but utilize the large
memory
int a=10;
funtion(a);
Call by Reference
Reduces the memory, but also reduces the security
int a=10;
function(&a);
18
Recursive function
The self calling mechanism.
The solution to the problem is in, some how, the problem itself.
#include <stdio.h>
int sumupto(int x);
int main()
{
int in, result;
printf("Enter a Number:");
scanf("%d", &in);
result=sumupto(in);
printf("\nresult is: %d", result);
}
int sumupto(int a)
{
if(a==0)
return a;
else
retrun a+sumupto(a-1);
}
19
Pointers and Arrays
What is Pointer?
What is Array?
How to declare a pointer?
String
Array declarations
• Single and Multi dimension
Pointer arithmetic
Array of Pointers
20
Pointer – Do and Don’t
Use less stage of pointer
Always make sure you free it
Initialize before using
Assign to NULL after deleting it for a safer side.
Make sure you are assigning a address not the variable
Do not assign direct address unless you know the code
If you use static make sure your code does not crashes.
Instead of assigning address try typecasting the address.
21
Function Pointer
Also known as an "indirect" call
Function pointers can be used to simplify code by providing a
simple way to select a function to execute based on run-time values.
#include <stdio.h> int main()
#include <conio.h> {
#include <math.h> double(*funpt)(double);
double rValue;
double funcptr(double(*fp) (double), int n, double x) funpt = sin;
{
double result = 0.0; rValue = funcptr(funpt, 20.2, 5);
printf("\n\n\tResult for function: %0.4lf", rValue);
for (int count = 0; count < n; count++) while (!_kbhit());
{ return 0;
double tResult = 0.0; }
tResult = (*fp)(x);
result += (tResult*tResult);
}
return result;
}
22
Dynamic Memory Allocation
What is memory allocation?
What is dynamic and static memory allocation?
Malloc() -- stdlib.h
Realloc()
Free()
23
Structure & Union
Declaration and Usage
Difference between structure & union
Array of structures
Pointer to the structures
Typedef
Bit-fields
Enum
24
Advanced I/O Statements
Sscanf()
Sprintf()
Inportb()
Bioskey()
25
File operations
fopen, fclose, fscanf, fprintf, fgetc, fputc
open, create, close, unlink, read, write
Text and Binary file operations.
26
Math and String functions
sin, cos sinh, cosh, log, log10, exp, pow, sqrt,
Strcpy, strcmp, strlen, strncpy, ect
27
Questions
1 Which follows overlapped memory allocation Union Structure Both None
The prototype
2 What is the function sigunature The function defination the return value None
declaration
Wha is the macimum size of memory allocated by
3 1MB 64KB Unlimited 2GB
malloc()
28
www.hcl.com