SlideShare a Scribd company logo
C Language 
Wednesday, November 19, 2014 H1arshit
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 
2
Program structure 
A sample C Program 
#include<stdio.h> 
int main() 
{ 
--other statements 
// Comments after double slash 
} 
3
C Character set 
• C uses 
• Uppercase letters A to Z 
• Lowercase letters a to z 
• Digits: 0 to 9 
• Special Characters: 
• + - ! # % ^ & * ? <>;.’|?/” 
• Special combination 
• b- backspace 
• n- New Line 
• t- horizontal tab 
• a- print with alert 
Wednesday, November 19, 24014
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 
` 5
Identifiers 
• Identifiers are the name given to various elements 
such as variables, functions and array. 
• Both upper and lower case letters are permitted 
• May begin with _ (underscore) 
• Diff b/w - and _ 
Wednesday, November 19, 26014
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 
7
Writing the first program 
#include<stdio.h> 
int main() 
{ 
printf(“Hello”); 
return 0; 
} 
• This program prints Hello on the screen when we 
execute it 
8
Running a C Program 
• Type a program 
• Save it 
• Compile the program – This will generate an exe file 
(executable) 
• Run the program (Actually the exe created out of 
compilation will run and not the .c file) 
• In different compiler we have different option for 
compiling and running. We give only the concepts. 
9
Comments in C 
• Single line comment 
• // (double slash) 
• Termination of comment is by pressing enter key 
• Multi line comment 
/*…. 
…….*/ 
This can span over to multiple lines 
` 10
Data types in C 
• Primitive data types 
• int, float, double, char 
• Aggregate data types 
• Arrays come under this category 
• Arrays can contain collection of int or float or char or 
double data 
• User defined data types 
• Structures and enum fall under this category. 
11
Variables 
• Variables are data that will keep on changing 
• Declaration 
<<Data type>> <<variable name>>; 
int a; 
• Definition 
<<varname>>=<<value>>; 
a=10; 
• Usage 
<<varname>> 
a=a+1; //increments the value of a by 1 
12
Variable names- Rules 
• Should not be a reserved word like int etc.. 
• Should start with a letter or an underscore(_) 
• Can contain letters, numbers or underscore. 
• No other special characters are allowed including 
space 
• Variable names are case sensitive 
• A and a are different. 
13
Keywords 
• In C there are certain reserved word that we cannot 
declare as variable. 
• These word is known as keyword 
• Keywords are reserved words that have a special 
meaning in a programming language. 
Wednesday, November 19,1 24014
Keywords 
• auto 
• double 
•• break 
int 
•struct 
• else 
•long 
•switch 
• case 
• enum 
•register 
•typedef 
• char 
• extern 
•return 
•union 
• const 
• float 
•short 
•unsigned 
• continue 
• for 
•signed 
•void 
• • default 
goto 
•sizeof 
•volatile 
• if 
•static 
•• do while 
15
Input and Output 
• Input 
• scanf(“%d”,&a); 
• Gets an integer value from the user and stores it under the 
name “a” 
• Output 
• printf(“%d”,a); 
• Prints the value present in variable a on the screen 
16
For loops 
• The syntax of for loop is 
for(initialisation;condition checking;increment) 
{ 
set of statements 
} 
Eg: Program to print Hello 10 times 
for(I=0;I<10;I++) 
{ 
printf(“Hello”); 
} 
17
While loop 
• The syntax for while loop 
while(condn) 
{ 
statements; 
} 
Eg: 
a=10; 
while(a != 0) Output: 10987654321 
{ 
printf(“%d”,a); 
a--; 
} 
18
Do While loop 
• The syntax of do while loop 
do 
{ 
set of statements 
}while(condn); 
Eg: 
i=10; Output: 
do 10987654321 
{ 
printf(“%d”,i); 
i--; 
}while(i!=0) 
19
Conditional statements 
if (condition) 
{ 
stmt 1; //Executes if Condition is true 
} 
else 
{ 
stmt 2; //Executes if condition is false 
} 
20
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; 
} 
21
Operators 
• Arithmetic (+,-,*,/,%) 
• Relational (<,>,<=,>=,==,!=) 
• Logical (&&,||,!) 
• Bitwise (&,|) 
• Assignment (=) 
• Compound assignment(+=,*=,-=,/=,%=,&=,|=) 
• Shift (right shift >>, left shift <<) 
22
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 
23
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) 
24
Procedures 
• Procedure is a function whose return type is void 
• Functions will have return types int, char, double, 
float or even structs and arrays 
• Return type is the data type of the value that is 
returned to the calling point after the called function 
execution completes 
25
Functions and Parameters 
• Syntax of function 
Declaration section 
<<Returntype>> funname(parameter list); 
Definition section 
<<Returntype>> funname(parameter list) 
{ 
body of the function 
} 
Function Call 
Funname(parameter); Wednesday, November 192, 62014
Example function 
#include<stdio.h> 
void fun(int a); //declaration 
int main() 
{ 
fun(10); //Call 
} 
void fun(int x) //definition 
{ 
printf(“%d”,x); 
} 
27
Actual and Formal parameters 
• Actual parameters are those that are used during a 
function call 
• Formal parameters are those that are used in function 
definition and function declaration 
28
Arrays 
• Arrays fall under aggregate data type 
• Aggregate – More than 1 
• Arrays are collection of data that belong to same data 
type 
• Arrays are collection of homogeneous data 
• Array elements can be accessed by its position in the 
array called as index 
29
Arrays 
• Array index starts with zero 
• The last index in an array is num – 1 where num is the 
no of elements in a array 
• int a[5] is an array that stores 5 integers 
• a[0] is the first element where as a[4] is the fifth element 
• We can also have arrays with more than one dimension 
• float a[5][5] is a two dimensional array. It can store 5x5 
= 25 floating point numbers 
• The bounds are a[0][0] to a[4][4] 30
Structures 
• Structures are user defined data types 
• It is a collection of heterogeneous data 
• It can have integer, float, double or character data in 
it 
• We can also have array of structures 
struct <<structname>> 
{ 
members; 
}element; 
We can access element.members; 
31
Structures 
struct Person 
{ 
int id; 
char name[5]; 
}P1; 
P1.id = 1; 
P1.name = “vasu”; 
32
Type def 
• The typedef operator is used for creating alias of a 
data type 
• For example I have this statement 
typedef int integer; 
Now I can use integer in place of int 
i.e instead of declaring int a;, I can use 
integer a; 
This is applied for structures too. 
33
Pointers 
• Pointer is a special variable that stores address of 
another variable 
• Addresses are integers. Hence pointer stores integer 
data 
• Size of pointer = size of int 
• Pointer that stores address of integer variable is 
called as integer pointer and is declared as int *ip; 
34
Pointers 
• Pointers that store address of a double, char and 
float are called as double pointer, character pointer 
and float pointer respectively. 
• char *cp 
• float *fp 
• double *dp; 
• Assigning value to a pointer 
int *ip = &a; //a is an int already declared 
35
Examples 
int a; 
a=10; //a stores 10 
int *ip; 
ip = &a;//ip stores address of a (say 1000) 
ip : fetches 1000 
*ip : fetches 10 
* Is called as dereferencing operator 
36
Call by Value 
• Calling a function with parameters passed as 
values 
int a=10; void fun(int a) 
fun(a); { 
defn; 
} 
Here fun(a) is a call by value. 
Any modification done with in the function is local 
to it and will not be effected outside the function 
Wednesday, November 193, 72014
Call by reference 
• Calling a function by passing pointers as parameters 
(address of variables is passed instead of variables) 
int a=1; void fun(int *x) 
fun(&a); { 
defn; 
} 
Any modification done to variable a will effect outside 
the function also 
38
Example program – Call by value 
#include<stdio.h> 
void main() 
{ 
int a=10; 
printf(“%d”,a); a=10 
fun(a); 
printf(“%d”,a); a=10 
} 
void fun(int x) 
{ 
printf(“%d”,x) x=10 
x++; 
printf(“%d”,x); x=11 
} 
39
Explanation 
Wednesday, November 194, 02014
Example Program – Call by 
reference 
#include<stdio.h> 
void main() 
{ 
int a=10; 
printf(“%d”,a); a=10 
fun(a); 
printf(“%d”,a); a=11 
} 
void fun(int x) 
{ 
printf(“%d”,x) x=10 
x++; 
printf(“%d”,x); x=11 
} 
Wednesday, November 194, 12014
Explanation 
a and x are referring to same location. So 
value will be over written. 
Wednesday, November 194, 22014
Conclusion 
• Call by value => copying value of variable in another 
variable. So any change made in the copy will not 
affect the original location. 
• Call by reference => Creating link for the parameter 
to the original location. Since the address is same, 
changes to the parameter will refer to original 
location and the value will be over written. 
Wednesday, November 194, 32014

More Related Content

What's hot (20)

PDF
Cse115 lecture03problemsolving
Md. Ashikur Rahman
 
PPT
Basics of c++ Programming Language
Ahmad Idrees
 
PPTX
C++ Programming Language Training in Ambala ! Batra Computer Centre
jatin batra
 
PDF
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
 
PPT
Basics of c++
Madhavendra Dutt
 
PPT
C++ programming program design including data structures
Ahmad Idrees
 
PDF
Cse115 lecture02overviewofprogramming
Md. Ashikur Rahman
 
ODP
(2) cpp imperative programming
Nico Ludwig
 
PPTX
Learn c++ Programming Language
Steve Johnson
 
PPTX
Managing input and output operation in c
yazad dumasia
 
PDF
Programming for Problem Solving Unit 2
Dhiviya Rose
 
PDF
C++ Tokens
Amrit Kaur
 
PDF
C++
Shyam Khant
 
PPTX
Learning C++ - Introduction to c++ programming 1
Ali Aminian
 
PPTX
Library functions in c++
Neeru Mittal
 
PPTX
POLITEKNIK MALAYSIA
Aiman Hud
 
PDF
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
PPTX
C++ PROGRAMMING BASICS
Aami Kakakhel
 
PPTX
Introduction to C++
Sikder Tahsin Al-Amin
 
PDF
Tutorial on c language programming
Sudheer Kiran
 
Cse115 lecture03problemsolving
Md. Ashikur Rahman
 
Basics of c++ Programming Language
Ahmad Idrees
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
jatin batra
 
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
 
Basics of c++
Madhavendra Dutt
 
C++ programming program design including data structures
Ahmad Idrees
 
Cse115 lecture02overviewofprogramming
Md. Ashikur Rahman
 
(2) cpp imperative programming
Nico Ludwig
 
Learn c++ Programming Language
Steve Johnson
 
Managing input and output operation in c
yazad dumasia
 
Programming for Problem Solving Unit 2
Dhiviya Rose
 
C++ Tokens
Amrit Kaur
 
Learning C++ - Introduction to c++ programming 1
Ali Aminian
 
Library functions in c++
Neeru Mittal
 
POLITEKNIK MALAYSIA
Aiman Hud
 
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
C++ PROGRAMMING BASICS
Aami Kakakhel
 
Introduction to C++
Sikder Tahsin Al-Amin
 
Tutorial on c language programming
Sudheer Kiran
 

Viewers also liked (20)

PPS
2
Baska789
 
PDF
JOBS Act Rulemaking Comments on SEC File Number S7-06-13 Part 3
Jason Coombs
 
PDF
Comments on SEC File Number 4-692 (Accredited Investor) and S7-06-13 (Regulat...
Jason Coombs
 
PPTX
יישום חוק חינוך
susanake
 
DOCX
Resume
Anuj Kumar
 
PPT
Mike Miozza for Mayor Action Plan - City of Fall River
JoAnne Breault
 
PDF
Customer Decision Journey
cmaionaise
 
PDF
Lounge up
cmaionaise
 
PDF
JOBS Act Rulemaking Comments on SEC File Number S7-06-13 Dated September 13, ...
Jason Coombs
 
PDF
История одного стартапа
RISSPA_SPb
 
PPT
Presentazione luigi pugliese
Luigi Pugliese
 
PDF
Трудный путь к соответствию требованиям PCI DSS (путевые заметки)
RISSPA_SPb
 
PDF
JOBS Act Rule 506(c) Federal Subpoena to Jason Coombs from Securities and Exc...
Jason Coombs
 
PDF
JOBS Act Rulemaking Comments on SEC File Number S7-11-13 Dated March 26, 2014
Jason Coombs
 
PDF
JOBS Act Rulemaking Comments on SEC File Number S7-06-13 Dated September 13, ...
Jason Coombs
 
PDF
JOBS Act Rulemaking Comments on SEC File Number S7-09-13 Dated February 3, 2014
Jason Coombs
 
PPSX
MRC Power Point.2012
JoAnne Breault
 
PDF
June 18 2012 letter to We Cluster and Public Startup Company co-founders, fri...
Jason Coombs
 
PPTX
Come creare un alveare in tre mosse
Marta Murari
 
PDF
JOBS Act Rulemaking Comments on SEC File Number S7-09-13 Dated February 11, 2014
Jason Coombs
 
JOBS Act Rulemaking Comments on SEC File Number S7-06-13 Part 3
Jason Coombs
 
Comments on SEC File Number 4-692 (Accredited Investor) and S7-06-13 (Regulat...
Jason Coombs
 
יישום חוק חינוך
susanake
 
Resume
Anuj Kumar
 
Mike Miozza for Mayor Action Plan - City of Fall River
JoAnne Breault
 
Customer Decision Journey
cmaionaise
 
Lounge up
cmaionaise
 
JOBS Act Rulemaking Comments on SEC File Number S7-06-13 Dated September 13, ...
Jason Coombs
 
История одного стартапа
RISSPA_SPb
 
Presentazione luigi pugliese
Luigi Pugliese
 
Трудный путь к соответствию требованиям PCI DSS (путевые заметки)
RISSPA_SPb
 
JOBS Act Rule 506(c) Federal Subpoena to Jason Coombs from Securities and Exc...
Jason Coombs
 
JOBS Act Rulemaking Comments on SEC File Number S7-11-13 Dated March 26, 2014
Jason Coombs
 
JOBS Act Rulemaking Comments on SEC File Number S7-06-13 Dated September 13, ...
Jason Coombs
 
JOBS Act Rulemaking Comments on SEC File Number S7-09-13 Dated February 3, 2014
Jason Coombs
 
MRC Power Point.2012
JoAnne Breault
 
June 18 2012 letter to We Cluster and Public Startup Company co-founders, fri...
Jason Coombs
 
Come creare un alveare in tre mosse
Marta Murari
 
JOBS Act Rulemaking Comments on SEC File Number S7-09-13 Dated February 11, 2014
Jason Coombs
 
Ad

Similar to C programming (20)

PPTX
Programming Fundamentals
umar78600
 
PPTX
C language
Robo India
 
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
Rasan Samarasinghe
 
PPTX
Fundamental programming Nota Topic 2.pptx
UmmuNazieha
 
PPTX
Introduction to c
Ajeet Kumar
 
PPTX
C programming
Nazmus Shakib Shadin
 
PPTX
C introduction by thooyavan
Thooyavan Venkatachalam
 
PPT
C intro
Kamran
 
PPTX
Persentation on c language
SudhanshuVijay3
 
PPT
Basics of C programming
avikdhupar
 
PPTX
Introduction%20C.pptx
20EUEE018DEEPAKM
 
PPTX
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
PPTX
C programming language tutorial
javaTpoint s
 
PDF
C programming day#1
Mohamed Fawzy
 
PPTX
C Programming Unit-1
Vikram Nandini
 
PPTX
C_Progragramming_language_Tutorial_ppt_f.pptx
maaithilisaravanan
 
PPTX
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
 
PPTX
Introduction to c
Sayed Ahmed
 
PPTX
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
MUHUMUZAONAN1
 
PDF
C programming day#2.
Mohamed Fawzy
 
Programming Fundamentals
umar78600
 
C language
Robo India
 
Esoft Metro Campus - Certificate in c / c++ programming
Rasan Samarasinghe
 
Fundamental programming Nota Topic 2.pptx
UmmuNazieha
 
Introduction to c
Ajeet Kumar
 
C programming
Nazmus Shakib Shadin
 
C introduction by thooyavan
Thooyavan Venkatachalam
 
C intro
Kamran
 
Persentation on c language
SudhanshuVijay3
 
Basics of C programming
avikdhupar
 
Introduction%20C.pptx
20EUEE018DEEPAKM
 
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
C programming language tutorial
javaTpoint s
 
C programming day#1
Mohamed Fawzy
 
C Programming Unit-1
Vikram Nandini
 
C_Progragramming_language_Tutorial_ppt_f.pptx
maaithilisaravanan
 
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
 
Introduction to c
Sayed Ahmed
 
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
MUHUMUZAONAN1
 
C programming day#2.
Mohamed Fawzy
 
Ad

Recently uploaded (20)

PDF
monopile foundation seminar topic for civil engineering students
Ahina5
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
PPTX
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
monopile foundation seminar topic for civil engineering students
Ahina5
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Design Thinking basics for Engineers.pdf
CMR University
 

C programming

  • 1. C Language Wednesday, November 19, 2014 H1arshit
  • 2. 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 2
  • 3. Program structure A sample C Program #include<stdio.h> int main() { --other statements // Comments after double slash } 3
  • 4. C Character set • C uses • Uppercase letters A to Z • Lowercase letters a to z • Digits: 0 to 9 • Special Characters: • + - ! # % ^ & * ? <>;.’|?/” • Special combination • b- backspace • n- New Line • t- horizontal tab • a- print with alert Wednesday, November 19, 24014
  • 5. 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 ` 5
  • 6. Identifiers • Identifiers are the name given to various elements such as variables, functions and array. • Both upper and lower case letters are permitted • May begin with _ (underscore) • Diff b/w - and _ Wednesday, November 19, 26014
  • 7. 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 7
  • 8. Writing the first program #include<stdio.h> int main() { printf(“Hello”); return 0; } • This program prints Hello on the screen when we execute it 8
  • 9. Running a C Program • Type a program • Save it • Compile the program – This will generate an exe file (executable) • Run the program (Actually the exe created out of compilation will run and not the .c file) • In different compiler we have different option for compiling and running. We give only the concepts. 9
  • 10. Comments in C • Single line comment • // (double slash) • Termination of comment is by pressing enter key • Multi line comment /*…. …….*/ This can span over to multiple lines ` 10
  • 11. Data types in C • Primitive data types • int, float, double, char • Aggregate data types • Arrays come under this category • Arrays can contain collection of int or float or char or double data • User defined data types • Structures and enum fall under this category. 11
  • 12. Variables • Variables are data that will keep on changing • Declaration <<Data type>> <<variable name>>; int a; • Definition <<varname>>=<<value>>; a=10; • Usage <<varname>> a=a+1; //increments the value of a by 1 12
  • 13. Variable names- Rules • Should not be a reserved word like int etc.. • Should start with a letter or an underscore(_) • Can contain letters, numbers or underscore. • No other special characters are allowed including space • Variable names are case sensitive • A and a are different. 13
  • 14. Keywords • In C there are certain reserved word that we cannot declare as variable. • These word is known as keyword • Keywords are reserved words that have a special meaning in a programming language. Wednesday, November 19,1 24014
  • 15. Keywords • auto • double •• break int •struct • else •long •switch • case • enum •register •typedef • char • extern •return •union • const • float •short •unsigned • continue • for •signed •void • • default goto •sizeof •volatile • if •static •• do while 15
  • 16. Input and Output • Input • scanf(“%d”,&a); • Gets an integer value from the user and stores it under the name “a” • Output • printf(“%d”,a); • Prints the value present in variable a on the screen 16
  • 17. For loops • The syntax of for loop is for(initialisation;condition checking;increment) { set of statements } Eg: Program to print Hello 10 times for(I=0;I<10;I++) { printf(“Hello”); } 17
  • 18. While loop • The syntax for while loop while(condn) { statements; } Eg: a=10; while(a != 0) Output: 10987654321 { printf(“%d”,a); a--; } 18
  • 19. Do While loop • The syntax of do while loop do { set of statements }while(condn); Eg: i=10; Output: do 10987654321 { printf(“%d”,i); i--; }while(i!=0) 19
  • 20. Conditional statements if (condition) { stmt 1; //Executes if Condition is true } else { stmt 2; //Executes if condition is false } 20
  • 21. 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; } 21
  • 22. Operators • Arithmetic (+,-,*,/,%) • Relational (<,>,<=,>=,==,!=) • Logical (&&,||,!) • Bitwise (&,|) • Assignment (=) • Compound assignment(+=,*=,-=,/=,%=,&=,|=) • Shift (right shift >>, left shift <<) 22
  • 23. 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 23
  • 24. 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) 24
  • 25. Procedures • Procedure is a function whose return type is void • Functions will have return types int, char, double, float or even structs and arrays • Return type is the data type of the value that is returned to the calling point after the called function execution completes 25
  • 26. Functions and Parameters • Syntax of function Declaration section <<Returntype>> funname(parameter list); Definition section <<Returntype>> funname(parameter list) { body of the function } Function Call Funname(parameter); Wednesday, November 192, 62014
  • 27. Example function #include<stdio.h> void fun(int a); //declaration int main() { fun(10); //Call } void fun(int x) //definition { printf(“%d”,x); } 27
  • 28. Actual and Formal parameters • Actual parameters are those that are used during a function call • Formal parameters are those that are used in function definition and function declaration 28
  • 29. Arrays • Arrays fall under aggregate data type • Aggregate – More than 1 • Arrays are collection of data that belong to same data type • Arrays are collection of homogeneous data • Array elements can be accessed by its position in the array called as index 29
  • 30. Arrays • Array index starts with zero • The last index in an array is num – 1 where num is the no of elements in a array • int a[5] is an array that stores 5 integers • a[0] is the first element where as a[4] is the fifth element • We can also have arrays with more than one dimension • float a[5][5] is a two dimensional array. It can store 5x5 = 25 floating point numbers • The bounds are a[0][0] to a[4][4] 30
  • 31. Structures • Structures are user defined data types • It is a collection of heterogeneous data • It can have integer, float, double or character data in it • We can also have array of structures struct <<structname>> { members; }element; We can access element.members; 31
  • 32. Structures struct Person { int id; char name[5]; }P1; P1.id = 1; P1.name = “vasu”; 32
  • 33. Type def • The typedef operator is used for creating alias of a data type • For example I have this statement typedef int integer; Now I can use integer in place of int i.e instead of declaring int a;, I can use integer a; This is applied for structures too. 33
  • 34. Pointers • Pointer is a special variable that stores address of another variable • Addresses are integers. Hence pointer stores integer data • Size of pointer = size of int • Pointer that stores address of integer variable is called as integer pointer and is declared as int *ip; 34
  • 35. Pointers • Pointers that store address of a double, char and float are called as double pointer, character pointer and float pointer respectively. • char *cp • float *fp • double *dp; • Assigning value to a pointer int *ip = &a; //a is an int already declared 35
  • 36. Examples int a; a=10; //a stores 10 int *ip; ip = &a;//ip stores address of a (say 1000) ip : fetches 1000 *ip : fetches 10 * Is called as dereferencing operator 36
  • 37. Call by Value • Calling a function with parameters passed as values int a=10; void fun(int a) fun(a); { defn; } Here fun(a) is a call by value. Any modification done with in the function is local to it and will not be effected outside the function Wednesday, November 193, 72014
  • 38. Call by reference • Calling a function by passing pointers as parameters (address of variables is passed instead of variables) int a=1; void fun(int *x) fun(&a); { defn; } Any modification done to variable a will effect outside the function also 38
  • 39. Example program – Call by value #include<stdio.h> void main() { int a=10; printf(“%d”,a); a=10 fun(a); printf(“%d”,a); a=10 } void fun(int x) { printf(“%d”,x) x=10 x++; printf(“%d”,x); x=11 } 39
  • 41. Example Program – Call by reference #include<stdio.h> void main() { int a=10; printf(“%d”,a); a=10 fun(a); printf(“%d”,a); a=11 } void fun(int x) { printf(“%d”,x) x=10 x++; printf(“%d”,x); x=11 } Wednesday, November 194, 12014
  • 42. Explanation a and x are referring to same location. So value will be over written. Wednesday, November 194, 22014
  • 43. Conclusion • Call by value => copying value of variable in another variable. So any change made in the copy will not affect the original location. • Call by reference => Creating link for the parameter to the original location. Since the address is same, changes to the parameter will refer to original location and the value will be over written. Wednesday, November 194, 32014

Editor's Notes

  • #13: We can also declare and define a variable in single shot like this. int a=10;
  • #17: Format specifiers %d is the format specifier. This informs to the compiler that the incoming value is an integer value. Other data types can be specified as follows: %c – character %f – float %lf – double %s – character array (string) Printf and scanf are defined under the header file stdio.h
  • #20: While – Entry controlled loop Do While – Exit controlled loop
  • #24: Header file to be included is string.h
  • #25: Header file to be included math.h
  • #33: For assigning more values we need to create an array of structure element like this Struct Person { Int id; Char name[5]; }P[10]; P[0].id = 1; P[0].name = “saran”; P[1].id = 2; P[1].name = “arya”; And so on till P[9]
  • #34: typedef struct student { int id; Char name[10]; }s; Now I can put s s1,s2; Instead of struct student s1,s2; //In case typedef is missed in struct definition as below struct student { int id; char name[10]; };