Programming Basics
Introduction to C programming
How does a program run?
● Step 1 → Write lines of codes.
● Step 2 → Compile.
● Step 3 → Execute.
● Step 4 → Get the output.
IDE vs Compiler
● IDE – Integrated Development Environment
● Compiler → translates to machine level(01010)
A basic C program
Hello World in Detail :
● stdio.h is a header file
● #include is a preprocessor directive.
● int main() : where the program execution
begins.
● { and } symbols mark the beginning and end of
a block of code.
● return 0; terminates the main() function
Identifiers
● name given to entities such as variables,
functions.
● Example - int money ;
● int money;
Here, money is identifier.
● must be different from keywords
Keywords
● predefined, reserved words.
● Example : int money;
int is a keyword .
Variable
● a variable is a container to hold data.
● Example: int score = 95;
Rules for naming a variable
● can have letters , digits and underscore only.
● The first letter of a variable should be either a
letter or an underscore
Data Types in C
Integer
● positive and negative values but no decimal
values. Eg : 0, -5, 10 .
● Int id ;
● Here, id is a variable of type integer.
● Int id,age;
Floating types
● can hold real numbers such as: 2.34, -9.382,
5.0 etc.
● float price; // 4 bytes size
● double price; // 8 bytes size
● Both are floating types.
Character types
● Keyword char is used for declaring such
variables.
● char test = 'h';
● test is a character variable.
● The value of test is 'h'.
● No string type in C . alternative = char array
● Eg: char num[ ] = “hello”
I/O in C programming
● Two commonly used functions are printf() and
scanf().
● Function scanf() reads input.
● Function printf() prints output.
Format specifiers and ampersand
● scanf("%d”,&a); for int , &a means the address
of variable a
● scanf("%f”,&a); for float
● Scanf(“%c”,&ch);
● Scanf(“%s”,&word);
● printf("%d is value of a",a);
Operators
● to perform tasks including arithmetic, conditional and bitwise
operations
● Types :
1. Arithmetic Operators
2. Increment and Decrement Operators
3. Assignment Operators
4. Relational Operators
5. Logical Operators
6. Others (Conditional,Bitwise and Special Operators)
Arithmetic Operators
Increment and decrement operators
● Increment ++ increases the value by 1
● Decrement -- decreases the value by 1
● Example :
int a=5;
a++ . value of a is now 6
● Similarly, for a-- , a is 4
++ and -- operator as prefix and
postfix
● Postfix a++ → original value of a is returned
first then, a is incremented by 1.
● Prefix ++a → value of a is incremented by 1
then, it returns the value.
Assignment Operators
Relational Operators
Logical Operators
Decision Making
● If else
● Nested if else
● Loops
if and if else statement
1 . if (testExpression)
{
// statements
}
2 . if (testExpression) {
// codes inside the body of if
}
else {
// codes inside the body of else
}
Flowcharts of if and if else
Nested if...else statement
if (testExpression1)
{ // statements to be executed if testExpression1 is true
}
else if(testExpression2)
{ // to be executed if testExpression1 is false and testExpression2 is true
}
...
else
{ // statements to be executed if all test expressions are false
}
Loops
● are used to repeat a specific block of code.
● Types -
1 . for loop
2 . while loop
3 . do while loop
For loop
for (initializationStatement; testExpression;
updateStatement)
{
// codes
}
While loop
while (testExpression)
{
//codes
}
Do while loop
do
{
// codes
}
while (testExpression);
Break and continue statement
● Break→ terminates the loop
● Continue → skips some statement
● Syntax :
break;
continue;
Switch case
switch (n)
{
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to constant2;
break;
. . .
default:
// code to be executed if n doesn't match any constant
}
Functions
● a block of code that performs a specific task.
● Two types of functions :
1 . User defined functions
2 . Standard library functions
Standard library functions
● built-in functions
● handle tasks such as mathematical
computations, I/O processing, etc.
● Example : printf() and scanf()
User defined functions
Advantage of user defined functions
● Clean,easier to maintain.
● Reusability.
● Easy to debug.

Programming basics

  • 1.
  • 2.
    How does aprogram run? ● Step 1 → Write lines of codes. ● Step 2 → Compile. ● Step 3 → Execute. ● Step 4 → Get the output.
  • 3.
    IDE vs Compiler ●IDE – Integrated Development Environment ● Compiler → translates to machine level(01010)
  • 4.
    A basic Cprogram
  • 5.
    Hello World inDetail : ● stdio.h is a header file ● #include is a preprocessor directive. ● int main() : where the program execution begins. ● { and } symbols mark the beginning and end of a block of code. ● return 0; terminates the main() function
  • 6.
    Identifiers ● name givento entities such as variables, functions. ● Example - int money ; ● int money; Here, money is identifier. ● must be different from keywords
  • 7.
    Keywords ● predefined, reservedwords. ● Example : int money; int is a keyword .
  • 8.
    Variable ● a variableis a container to hold data. ● Example: int score = 95;
  • 9.
    Rules for naminga variable ● can have letters , digits and underscore only. ● The first letter of a variable should be either a letter or an underscore
  • 10.
  • 11.
    Integer ● positive andnegative values but no decimal values. Eg : 0, -5, 10 . ● Int id ; ● Here, id is a variable of type integer. ● Int id,age;
  • 12.
    Floating types ● canhold real numbers such as: 2.34, -9.382, 5.0 etc. ● float price; // 4 bytes size ● double price; // 8 bytes size ● Both are floating types.
  • 13.
    Character types ● Keywordchar is used for declaring such variables. ● char test = 'h'; ● test is a character variable. ● The value of test is 'h'. ● No string type in C . alternative = char array ● Eg: char num[ ] = “hello”
  • 14.
    I/O in Cprogramming ● Two commonly used functions are printf() and scanf(). ● Function scanf() reads input. ● Function printf() prints output.
  • 15.
    Format specifiers andampersand ● scanf("%d”,&a); for int , &a means the address of variable a ● scanf("%f”,&a); for float ● Scanf(“%c”,&ch); ● Scanf(“%s”,&word); ● printf("%d is value of a",a);
  • 16.
    Operators ● to performtasks including arithmetic, conditional and bitwise operations ● Types : 1. Arithmetic Operators 2. Increment and Decrement Operators 3. Assignment Operators 4. Relational Operators 5. Logical Operators 6. Others (Conditional,Bitwise and Special Operators)
  • 17.
  • 18.
    Increment and decrementoperators ● Increment ++ increases the value by 1 ● Decrement -- decreases the value by 1 ● Example : int a=5; a++ . value of a is now 6 ● Similarly, for a-- , a is 4
  • 19.
    ++ and --operator as prefix and postfix ● Postfix a++ → original value of a is returned first then, a is incremented by 1. ● Prefix ++a → value of a is incremented by 1 then, it returns the value.
  • 20.
  • 21.
  • 22.
  • 23.
    Decision Making ● Ifelse ● Nested if else ● Loops
  • 24.
    if and ifelse statement 1 . if (testExpression) { // statements } 2 . if (testExpression) { // codes inside the body of if } else { // codes inside the body of else }
  • 25.
    Flowcharts of ifand if else
  • 26.
    Nested if...else statement if(testExpression1) { // statements to be executed if testExpression1 is true } else if(testExpression2) { // to be executed if testExpression1 is false and testExpression2 is true } ... else { // statements to be executed if all test expressions are false }
  • 27.
    Loops ● are usedto repeat a specific block of code. ● Types - 1 . for loop 2 . while loop 3 . do while loop
  • 28.
    For loop for (initializationStatement;testExpression; updateStatement) { // codes }
  • 30.
  • 32.
    Do while loop do { //codes } while (testExpression);
  • 34.
    Break and continuestatement ● Break→ terminates the loop ● Continue → skips some statement ● Syntax : break; continue;
  • 36.
    Switch case switch (n) { caseconstant1: // code to be executed if n is equal to constant1; break; case constant2: // code to be executed if n is equal to constant2; break; . . . default: // code to be executed if n doesn't match any constant }
  • 37.
    Functions ● a blockof code that performs a specific task. ● Two types of functions : 1 . User defined functions 2 . Standard library functions
  • 38.
    Standard library functions ●built-in functions ● handle tasks such as mathematical computations, I/O processing, etc. ● Example : printf() and scanf()
  • 39.
  • 40.
    Advantage of userdefined functions ● Clean,easier to maintain. ● Reusability. ● Easy to debug.