Introduction to C program
Structure of C program
Preprocessor directives
Global variables
Main(void)
{
Local variables
Statements
…..………
…………….
Return 0;
}
Function()
{
Local variables
Statements
…………..
}
#include<stdio.h>
#define num 10
Static Int I; //static or extern
Main()
{
Int I, num; /* static, global, auto or registers
declaration*/
Int a=5; // initialization
Scanf(“%d”,&i);
Printf(“i=%d,num=%d,a=%d”,I,num,a);
}
Compilation and execution of c program
Pre-processor
Compiler
Assembler
Linker
Source code
Expanded code
Assembly code
Object code
Executable code
.c C code
.i intermediate code
.s assembly code
.o object code
.exe / a.out
Errors in c program
Compile time error Run time error
Pre processor
error
Translator error
Linker error Segmentation error
Bus error
…….
……..
...…….64 types of errors
Data types
 Character data type (char)
 Signed - 1 byte (-128 to 127)
 Unsigned - 1 byte (0 to 255)
 Integer data type (int)
 Short int : signed - 1 byte (-128 to 127)
Unsigned - 1 byte (0 to 255)
 int : signed - 2 byte (-2G to 2G)
Unsigned - 2 byte (0 to 4G)
 long int : signed - 4 byte (-4G to 4G)
Unsigned - 4 byte (0 to 8G)
 Real data type (float and double)
 Float - 4bytes (3.4e-38 to 3.4e+38)
 Double - 8 bytes (1.7e-308 to 1.7e+308)
 Long double - 10 bytes (3.4e-4932 to 3.4e+4932)
 String data type (string)
‘a’ , ’1’
123 , 0X1223
Float (0.25f) and double (0.25)
“1234”, “nishma”
Operators
• Arithmetic operator ( + , - , * , / , % ) – binary – doesn’t change the value – result (value)
• Relational operator ( > , >= , < , <= , == , != ) – binary – doesn’t change the value – result (0 or 1)
• Logical or Boolean operator (&& , || , ! ) – binary – doesn’t change the value – result (0 or 1)
• Conditional operator ( [? :] ) – ternary – doesn’t change the value – result (true stat or false stat)
• Bitwise operator ( & , | , ~ , << , >> ,^ ) – binary – changes the value
• Increment and decrement operator ( ++ , -- ) – unary – changes the value
• Size of operator ( sizeof() ) –unary –doesn’t change the value
Control statements
Iterative Non - iterative
Conditional Unconditional
 For
 While
 Do while
 If
 Else if
 Switch - case
 Goto
 Break
 Return
 Continue
If statement
If (condition)
{
Statement1;
Statement2;
Statement3;
}
If (condition)
Statement1;
Statement2;
Statement3;
If (condition);
{
Statement1;
Statement2;
Statement3;
}
true
Example
#include<stdio.h>
Main()
{
Int a=10,b=20;
If (a<b)
{
a=b;
Printf(“greater=%d”,a);
}
}
#include<stdio.h>
Main()
{
Int a=10,b=20;
If (a<b)
a=b;
Printf(“greater=%d”,a);
}
If esle statement
If (condition)
Statement 1;
Else
Statement 2;
If (condition)
{
Statement 1;
}
Else
{
Statement 2;
}
If (condition)
{
Statement 1;
}
Else
Statement 2;
If (condition)
Statement 1;
Else
{
Statement 2;
}
Condition
Statement 1 Statement 2
Next statement
True False
Example
#include<stdio.h>
Main()
{
Int a=10,b=20;
If (a<b)
{
a=b;
Printf(“greater=%d”,a);
}
else
Printf(“greater=%d”,a);
}
#include<stdio.h>
Main()
{
Int a=10,b=20;
If (a<b)
a=b;
Printf(“greater=%d”,a);
else
Printf(“greater=%d”,a);
}
Else if ladder
If (expression 1)
Statement 1;
Else if (expression 2)
Statement 2;
Else if (expression 3)
Statement 3;
Else
Statement 4;
Nested if else ladder
If (exp 1)
{
If (exp 2)
{
if (exp 3)
{
S1;
}
else
S2;
}
else
S3;
}
Else
S4;
Switch case statement
Switch (expression)
{
Case label 1 : S1;
S2;
break;
Case label 2 : S1;
S2;
break;
Default : S1;
Result should be in
Char, short int, int.
Strings and float are not allowed
Not mandatory
Example
#include<stdio.h>
Main()
{
Int a;
Scanf(“%d”,&a);
Switch (a)
{
Case 1 : printf(“%d”,a++);
break;
Case 2 : printf(“%d”,++a);
break;
Default:printf(“%d”,--a);
For statement
For (exp 1 ; exp 2 ; exp 3)
{
Statement 1;
Statement 2;
}
Initialization
Condition checking
Control variable
operators
Body of the
loop
For (exp 1 ; exp 2 ; exp 3);
{
Statement 1;
Statement 2;
}
Example
For (i=0 ; i<=10 ; i++)
{
Printf(“%d”,i);
}
For (i=0 ; i<=10 ; i++);
{
Printf(“%d”,i);
}
o/p:
0 1 2 3 4 5 6 7 8 9 10
o/p:
i=11
Nesting For statement
For (exp 1 ; exp 2 ; exp 3)
{
Statement 1;
Statement 2;
for (exp 4 ; exp 5 ; exp 6)
{
statement 3;
statement 4;
}
}
Outer loop control
variables and inner loop
control variables will not
be the same
1 2
3
8
4 5
6
7
For (i=0 ; i<4 ; i++)
{
n=i;
for (j=0 ; j<n ; j++)
{
printf(“ %d ”,j);
}
printf(“n”);
}
o/p:
i=0 , i<4 , n=0 ; j=0 , j<0;
i=1 , i<4 , n=1 ; j=0 , j<1; j=1 , j<1;
i=2 , i<4 , n=2 ; j=0 , j<2; j=1 , j<2;j=2 , j<2
i=3 , i<4 , n=1 ; j=0 , j<3; j=1 , j<3;j=2 , j<3; j=3 , j<3
i=4 , i<4
0
0 1
0 1 2
While statement
While (expression)
{
Statement 1;
Statement 2;
}
Expression
Body of the loop
True
False
Next statements
While (exp1 ; exp2 ; exp3)
{
Statement 1;
Statement 2;
}
Last exp (exp3)
decides whether
to enter the loop
or not
While (expression)
Statement 1;
Example
Int i=0;
While (i==0)
{
Printf(“i=%dn”, i++);
Printf(“i=%dn”,++i);
}
o/p:
i=0
i=2
Int i=0;
While (i==0);
{
Printf(“i=%dn”, i++);
Printf(“i=%dn”,++i);
}
o/p:
Infinite loop
Doesn’t print any value
Do While statement
Do
{
Statement 1;
Statement 2;
}
While (expression);
Expression
Body of the loop
True
False
Next statements
Do
Statement 1;
While (expression);
Example
Int i=0;
Do
{
Printf(“i=%dn”, i++);
Printf(“i=%dn”,++i);
}
While(i<10); o/p:
i=0 , i=2
i<10 , i=2 , i=4
…………
i<10 , i=8 , i=10
i<10 next statement
Goto statement
Forward jump
Goto label;
Statements;
…………
……………
Label:
Statements;
…………
……………
Backward jump
Label:
Statements;
…………
……………
Goto label;
Statements;
…………
……………
Example
Int a;
Start:
Printf(“enter the valuen”);
Scanf(“%d”,&a);
Printf(“%d”,++a);
Goto start;
Break statement
Break;
Arrays
Data_type array_name [index];
No of elements
in the array
To scan
For (i=0 ; a[i] ; i++)
{
Scanf(“%d”,&a[i]);
}
To print
For (i=0 ; a[i] ; i++)
{
printf(“%d”,a[i]);
}
Array representation
A[i]=i[a]=*(a+i)
Example:
Int a[6];
Int a[6] = {10,20,30,40,50,60};
1D array
2D array
To scan
For (i=0 ; i<n ; i++)
{
for(j=0 ; j<m ; j++)
Scanf(“%d”,&a[i]);
}
To print
For (i=0 ; a[i] ; i++)
{
for(j=0 ; j<m ; j++)
printf(“%d”,a[i]);
}
Strings
Data_type string_name [index];
Example:
char s[6];
char s[6] = “hello”;
No of elements
in the string
To scan
For (i=0 ; s[i] ; i++)
{
Scanf(“%c”,&s[i]);
}
Or
Scanf(“%s”,s);
To print
For (i=0 ; s[i] ; i++)
{
printf(“%c”,s[i]);
}
Or
Printf(“%s”,s);
It is a collection of characters terminated by a null symbol (‘o’)
Functions
Library function
User defined function
Example:
Sqrt();
Function declaration
Function call
Function definition
Return_type fun_name (type1 par1, type2 par2,…..);
Fun_name (arg1, arg2, arg3);
Return_type fun_name (parameter declarations)
Return (exp);
Storage classes
Auto Static Extern Register
Default Garbage 0 0 Garbage
Memory Stack frame Data segment Data segment Stack frame
Scope locally Accessed directly
within a fun or
block
Accessed within
a fun
Accessed within
a fun
Accessed directly
within a fun or
block
globally Cannot be
declared globally
can access
throughout the
file.
can access
throughout the
file.
Cannot be
declared globally
Life Start When fun stack
frame created
When program
is loaded into
RAM
Same as static When fun
execution starts
End When it gets
deallocated
When execution
is completed
Same as static When execution
ends

1 introduction to c program

  • 1.
  • 2.
    Structure of Cprogram Preprocessor directives Global variables Main(void) { Local variables Statements …..……… ……………. Return 0; } Function() { Local variables Statements ………….. } #include<stdio.h> #define num 10 Static Int I; //static or extern Main() { Int I, num; /* static, global, auto or registers declaration*/ Int a=5; // initialization Scanf(“%d”,&i); Printf(“i=%d,num=%d,a=%d”,I,num,a); }
  • 3.
    Compilation and executionof c program Pre-processor Compiler Assembler Linker Source code Expanded code Assembly code Object code Executable code .c C code .i intermediate code .s assembly code .o object code .exe / a.out
  • 4.
    Errors in cprogram Compile time error Run time error Pre processor error Translator error Linker error Segmentation error Bus error ……. …….. ...…….64 types of errors
  • 5.
    Data types  Characterdata type (char)  Signed - 1 byte (-128 to 127)  Unsigned - 1 byte (0 to 255)  Integer data type (int)  Short int : signed - 1 byte (-128 to 127) Unsigned - 1 byte (0 to 255)  int : signed - 2 byte (-2G to 2G) Unsigned - 2 byte (0 to 4G)  long int : signed - 4 byte (-4G to 4G) Unsigned - 4 byte (0 to 8G)  Real data type (float and double)  Float - 4bytes (3.4e-38 to 3.4e+38)  Double - 8 bytes (1.7e-308 to 1.7e+308)  Long double - 10 bytes (3.4e-4932 to 3.4e+4932)  String data type (string) ‘a’ , ’1’ 123 , 0X1223 Float (0.25f) and double (0.25) “1234”, “nishma”
  • 6.
    Operators • Arithmetic operator( + , - , * , / , % ) – binary – doesn’t change the value – result (value) • Relational operator ( > , >= , < , <= , == , != ) – binary – doesn’t change the value – result (0 or 1) • Logical or Boolean operator (&& , || , ! ) – binary – doesn’t change the value – result (0 or 1) • Conditional operator ( [? :] ) – ternary – doesn’t change the value – result (true stat or false stat) • Bitwise operator ( & , | , ~ , << , >> ,^ ) – binary – changes the value • Increment and decrement operator ( ++ , -- ) – unary – changes the value • Size of operator ( sizeof() ) –unary –doesn’t change the value
  • 7.
    Control statements Iterative Non- iterative Conditional Unconditional  For  While  Do while  If  Else if  Switch - case  Goto  Break  Return  Continue
  • 8.
    If statement If (condition) { Statement1; Statement2; Statement3; } If(condition) Statement1; Statement2; Statement3; If (condition); { Statement1; Statement2; Statement3; } true
  • 9.
  • 10.
    If esle statement If(condition) Statement 1; Else Statement 2; If (condition) { Statement 1; } Else { Statement 2; } If (condition) { Statement 1; } Else Statement 2; If (condition) Statement 1; Else { Statement 2; } Condition Statement 1 Statement 2 Next statement True False
  • 11.
  • 12.
    Else if ladder If(expression 1) Statement 1; Else if (expression 2) Statement 2; Else if (expression 3) Statement 3; Else Statement 4; Nested if else ladder If (exp 1) { If (exp 2) { if (exp 3) { S1; } else S2; } else S3; } Else S4;
  • 13.
    Switch case statement Switch(expression) { Case label 1 : S1; S2; break; Case label 2 : S1; S2; break; Default : S1; Result should be in Char, short int, int. Strings and float are not allowed Not mandatory
  • 14.
    Example #include<stdio.h> Main() { Int a; Scanf(“%d”,&a); Switch (a) { Case1 : printf(“%d”,a++); break; Case 2 : printf(“%d”,++a); break; Default:printf(“%d”,--a);
  • 15.
    For statement For (exp1 ; exp 2 ; exp 3) { Statement 1; Statement 2; } Initialization Condition checking Control variable operators Body of the loop For (exp 1 ; exp 2 ; exp 3); { Statement 1; Statement 2; }
  • 16.
    Example For (i=0 ;i<=10 ; i++) { Printf(“%d”,i); } For (i=0 ; i<=10 ; i++); { Printf(“%d”,i); } o/p: 0 1 2 3 4 5 6 7 8 9 10 o/p: i=11
  • 17.
    Nesting For statement For(exp 1 ; exp 2 ; exp 3) { Statement 1; Statement 2; for (exp 4 ; exp 5 ; exp 6) { statement 3; statement 4; } } Outer loop control variables and inner loop control variables will not be the same 1 2 3 8 4 5 6 7 For (i=0 ; i<4 ; i++) { n=i; for (j=0 ; j<n ; j++) { printf(“ %d ”,j); } printf(“n”); } o/p: i=0 , i<4 , n=0 ; j=0 , j<0; i=1 , i<4 , n=1 ; j=0 , j<1; j=1 , j<1; i=2 , i<4 , n=2 ; j=0 , j<2; j=1 , j<2;j=2 , j<2 i=3 , i<4 , n=1 ; j=0 , j<3; j=1 , j<3;j=2 , j<3; j=3 , j<3 i=4 , i<4 0 0 1 0 1 2
  • 18.
    While statement While (expression) { Statement1; Statement 2; } Expression Body of the loop True False Next statements While (exp1 ; exp2 ; exp3) { Statement 1; Statement 2; } Last exp (exp3) decides whether to enter the loop or not While (expression) Statement 1;
  • 19.
    Example Int i=0; While (i==0) { Printf(“i=%dn”,i++); Printf(“i=%dn”,++i); } o/p: i=0 i=2 Int i=0; While (i==0); { Printf(“i=%dn”, i++); Printf(“i=%dn”,++i); } o/p: Infinite loop Doesn’t print any value
  • 20.
    Do While statement Do { Statement1; Statement 2; } While (expression); Expression Body of the loop True False Next statements Do Statement 1; While (expression);
  • 21.
    Example Int i=0; Do { Printf(“i=%dn”, i++); Printf(“i=%dn”,++i); } While(i<10);o/p: i=0 , i=2 i<10 , i=2 , i=4 ………… i<10 , i=8 , i=10 i<10 next statement
  • 22.
    Goto statement Forward jump Gotolabel; Statements; ………… …………… Label: Statements; ………… …………… Backward jump Label: Statements; ………… …………… Goto label; Statements; ………… …………… Example Int a; Start: Printf(“enter the valuen”); Scanf(“%d”,&a); Printf(“%d”,++a); Goto start;
  • 23.
  • 24.
    Arrays Data_type array_name [index]; Noof elements in the array To scan For (i=0 ; a[i] ; i++) { Scanf(“%d”,&a[i]); } To print For (i=0 ; a[i] ; i++) { printf(“%d”,a[i]); } Array representation A[i]=i[a]=*(a+i) Example: Int a[6]; Int a[6] = {10,20,30,40,50,60}; 1D array
  • 25.
    2D array To scan For(i=0 ; i<n ; i++) { for(j=0 ; j<m ; j++) Scanf(“%d”,&a[i]); } To print For (i=0 ; a[i] ; i++) { for(j=0 ; j<m ; j++) printf(“%d”,a[i]); }
  • 26.
    Strings Data_type string_name [index]; Example: chars[6]; char s[6] = “hello”; No of elements in the string To scan For (i=0 ; s[i] ; i++) { Scanf(“%c”,&s[i]); } Or Scanf(“%s”,s); To print For (i=0 ; s[i] ; i++) { printf(“%c”,s[i]); } Or Printf(“%s”,s); It is a collection of characters terminated by a null symbol (‘o’)
  • 27.
    Functions Library function User definedfunction Example: Sqrt(); Function declaration Function call Function definition Return_type fun_name (type1 par1, type2 par2,…..); Fun_name (arg1, arg2, arg3); Return_type fun_name (parameter declarations) Return (exp);
  • 28.
    Storage classes Auto StaticExtern Register Default Garbage 0 0 Garbage Memory Stack frame Data segment Data segment Stack frame Scope locally Accessed directly within a fun or block Accessed within a fun Accessed within a fun Accessed directly within a fun or block globally Cannot be declared globally can access throughout the file. can access throughout the file. Cannot be declared globally Life Start When fun stack frame created When program is loaded into RAM Same as static When fun execution starts End When it gets deallocated When execution is completed Same as static When execution ends