C Language
C Language
Overview of C
• C is developed by Dennis Ritchie
• C is a structured programming language
• C supports functions that enables easy
maintainability of code, by breaking large
file into smaller modules
• Comments in C provides easy readability
• C is a powerful language
Program structure
A sample C Program
#include<stdio.h>
int main()
{
--other statements
}
Header files
• The files that are specified in the include
section is called as header file
• These are precompiled files that has some
functions defined in them
• We can call those functions in our program
by supplying parameters
• Header file is given an extension .h
• C Source file is given an extension .c
Main function
• This is the entry point of a program
• When a file is executed, the start point is
the main function
• From main function the flow goes as per
the programmers choice.
• There may or may not be other functions
written by user in a program
• Main function is compulsory for any c
program
Writing the first program
#include<stdio.h>
int main()
{
printf(“Hello”);
return 0;
}
Eg:
i=10; Output:
do 10987654321
{
printf(“%d”,i);
i--;
}while(i!=0)
Conditional statements
if (condition)
{
stmt 1; //Executes if Condition is true
}
else
{
stmt 2; //Executes if condition is false
}
Conditional statement
switch(var)
{
case 1: //if var=1 this case executes
stmt;
break;
case 2: //if var=2 this case executes
stmt;
break;
default: //if var is something else this will execute
stmt;
}
Operators
• Arithmetic (+,-,*,/,%)
• Relational (<,>,<=,>=,==,!=)
• Logical (&&,||,!)
• Bitwise (&,|)
• Assignment (=)
• Compound assignment(+=,*=,-=,/=,
%=,&=,|=)
• Shift (right shift >>, left shift <<)
String functions
• strlen(str) – To find length of string str
• strrev(str) – Reverses the string str as rts
• strcat(str1,str2) – Appends str2 to str1 and
returns str1
• strcpy(st1,st2) – copies the content of st2 to st1
• strcmp(s1,s2) – Compares the two string s1 and
s2
• strcmpi(s1,s2) – Case insensitive comparison of
strings
Numeric functions
• pow(n,x) – evaluates n^x
• ceil(1.3) – Returns 2
• floor(1.3) – Returns 1
• abs(num) – Returns absolute value
• log(x) - Logarithmic value
• sin(x)
• cos(x)
• tan(x)
Procedures
• Procedure is a function whose return type
is void
ip : fetches 1000
*ip : fetches 10
* Is called as dereferencing operator
Call by Value
• Calling a function with parameters passed as
values