Upload
david-raju-gollapudi
View
218
Download
0
Embed Size (px)
Citation preview
8/13/2019 PPT on Computers
1/25
8/13/2019 PPT on Computers
2/25
OVERVIEW History of c
Program in c
Sample program Data types
Variables
Input/output
Control statements
8/13/2019 PPT on Computers
3/25
Fundamentals and History of C
C is developed by Dennis Ritchie
C is a structured programming language
C supports functions that enables easymaintainability of code, by breaking large file intosmaller modules
Comments in C provides easy readability
C is a powerful language
8/13/2019 PPT on Computers
4/25
PROGRAM STRCTURE IN C
#include
#includevoid main()
{
--other statements
}
8/13/2019 PPT on Computers
5/25
HEADER FILES The files that are specified in the include section is
called as header file
These are precompiled files that has some functionsdefined in them
We can call those functions in our program bysupplying parameters
Header file is given an extension .h C Source file is given an extension .c
8/13/2019 PPT on Computers
6/25
MAIN FUNCTION This is the entry point of a program
When a file is executed, the start point is the mainfunction
From main function the f low goes as per theprogrammers choice.
There may or may not be other functions written byuser in a program
Main function is compulsory for any c program
8/13/2019 PPT on Computers
7/25
Sample program#include
#include
int main(){
printf(Hello);
return 0;
}
This program prints Hello on the screen when weexecute it
8/13/2019 PPT on Computers
8/25
HOW TO RUN 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 forcompiling and running. We give only the concepts.
8/13/2019 PPT on Computers
9/25
Points to remember
Case Sensitive
Case matters in C. A is not a
Add plenty of comments (/* */ or //)
Good layout, not:
main() {printf("Hello World\n");}
Use meaningful variable names
Initialize your variables Use parentheses to avoid confusion:
a=(10.0 + 2.0) * (5.0 - 6.0) / 2.0
8/13/2019 PPT on Computers
10/25
DATA TYPES 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 ordouble data
User defined data types Structures and enum fall under this category.
8/13/2019 PPT on Computers
11/25
VARIABLESVariables are data that will keep on changing
Declaration
;int a;
Definition=;
a=10; Usage
a=a+1; //increments the value of a by 1
8/13/2019 PPT on Computers
12/25
RULES FOR VARIBLE NAME
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.
8/13/2019 PPT on Computers
13/25
INPUT & 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
8/13/2019 PPT on Computers
14/25
OPERATORSArithmetic (+,-,*,/,%)
Relational (,=,==,!=)
Logical (&&,||,!)
Bitwise (&,|)
Assignment (=)
Compound assignment(+=,*=,-=,/=,%=,&=,|=) Shift (right shift >>, left shift
8/13/2019 PPT on Computers
15/25
OPERATORS(Contd.)Increment and Decrement Operators
++ Increment operator-- Decrement Operator
k++ or k-- (Post-increment/decrement)
k = 5;
x = k++; // sets x to 5, then increments k to 6
8/13/2019 PPT on Computers
16/25
(contd.)
++k or --k (Pre-increment/decrement)
k = 5;x = ++k; // increments k to 6 and then sets to the
resulting value, i.e., to 6
8/13/2019 PPT on Computers
17/25
CONTROL STATEMENTS
8/13/2019 PPT on Computers
18/25
Conditional Statements
if (condition)
{stmt 1; //Executes if Condition is true
}
else{
stmt 2; //Executes if condition is false
}
8/13/2019 PPT on Computers
19/25
In this we have some if(conditions)
They are
1. if2. Simple if
3. if else
4. compound if
5. nested if
8/13/2019 PPT on Computers
20/25
SWITCH & BREAK
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;
}
8/13/2019 PPT on Computers
21/25
LOOPS
8/13/2019 PPT on Computers
22/25
FOR LOOP 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
8/13/2019 PPT on Computers
23/25
WHILE LOOP The syntax for while loop
while(condn)
{
statements;}
Eg:
a=10;
while(a != 0) Output: 10987654321
{printf(%d,a);
a- -;
}
8/13/2019 PPT on Computers
24/25
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)
8/13/2019 PPT on Computers
25/25
THANKS