KEMBAR78
Fundamentals of Programming Constructs.pptx
Fundamentals
Programming
Constructs
Father of C
 C Programming was developed by Dennis Ritchie for
creating system applications that directly interact
with the hardware devices such as drivers, kernels,
etc.
 C programming is considered as the base for other
programming languages, that is why it is known as
mother language.
Features of C
MOTHER/SYSTEM/PRO
CEDURAL/STRUCTURE
D
MACHINE
DEPENDENT
RICH LIBRARIES DYNAMIC MEMORY
ALLOCATION
First C Program #include <stdio.h>
void main()
{
//Print a statement
printf(“Welcome");
}
How C Works
Output
Loader
• CPU
Linker
• Executable
code(.exe)
Compiler
• Obj codes(.o)
Preprocessor
• IncludeFiles
and replaced
Symbols
Editor or IDE
• Source
Code(.c) and
Header(.h)
Variable
A variable is a name of the
memory location.
It is used to store data.
Its value can be changed,
hence it is called a variable
Syntax:
datatype varName;
Eg: int no;
Rules for a
Variable
A variable can have alphabets, digits, and
underscore.
•A variable name can start with the
alphabet, and underscore only. It can't
start with a digit.
•No space is allowed within the variable
name.
•A variable name must not be any
reserved word or keyword, e.g. int, float,
etc
Variables
VALID
 int a;
 int _ab;
 int a30;
INVALID
 int 2;
 int simple interest;
 int float;
Variables
DECLARATION
 int a;  int a = 80;
INITIALISATION
Types of Variables
LOCAL GLOBAL STATIC AUTOMATIC EXTERNAL
Local Variable
DEFINITION
 A variable that is declared inside the
function or block is called a local
variable.
 It is available only for the function or
the block
 You must have to initialize the local
variable before it is used.
EXAMPLE
void main()
{
int x=10;//local variable
}
Automatic Variable
DEFINITION
 The variables which are declared inside a
block are known as automatic or local
variables;
 These variables allocates memory
automatically upon entry to that block and
free the occupied memory upon exit from that
block.
 We can explicitly declare an automatic
variable using auto keyword.
EXAMPLE
void main(){
int x=10;//local (also automatic)
auto int y=20;//automatic variable
}
Global Variable
DEFINITION
 A variable that is declared outside the
function or block is called a global
variable.
 Any function can change the value of
the global variable.
 It is available to all the functions.
EXAMPLE
int value=20;//global variable
void main()
{
int x=10;//local variable
}
Static Variable
DEFINITION
 A variable that is declared with the static
keyword is called static variable.
 It retains its value between multiple function
calls.
 The local variable will print the same value
for each function call, e.g, 11,11,11 and so on.
 But the static variable will print the
incremented value in each function call, e.g.
11, 12, 13 and so on.
EXAMPLE
void function1(){
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
External Variable
DEFINITION
 We can share a variable in multiple C source
files by using an external variable.
 To declare an external variable, use extern
keyword.
EXAMPLE
extern int x=10;
//external variable (also global)
Datatype
 A data type specifies the type of data that a
variable can store such as number, character
or decimal etc.
 The basic data types are integer-based and
floating-point based.
 C language supports both signed and
unsigned literals.
 The memory size of the basic data types may
change according to 32 or 64-bit operating
system.
Datatype
int are whole numbers that can have both zero,
positive and negative values but no decimal values.
float and double and are used to hold real
numbers.
char is used for declaring character type variables
Void is an incomplete type. It means "nothing" or
"no type".
If you need to use a large/small number, you can
use a type specifier long/short
Signed and
unsigned
 In C, signed and unsigned are type modifiers.
You can alter the data storage of a data type
by using them. For example,
 unsigned int x;
 int y;
 Here, the variable x can hold only zero and
positive values because we have used the
unsigned modifier.
 Considering the size of int is 4 bytes, variable
y can hold values from -231 to 231-1, whereas
variable x can hold values from 0 to 232-1.
Data Types Memory Size Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
short 2 byte −32,768 to 32,767
signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
int 4 byte -2,147,483,648 to 2,147,483,647
signed int 2 byte −32,768 to 32,767
unsigned int 4 byte 0 to 4,294,967,295
short int 2 byte −32,768 to 32,767
signed short int 2 byte −32,768 to 32,767
unsigned short int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to 2,147,483,647
signed long int 4 byte -2,147,483,648 to 2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
float 4 byte
double 8 byte
long double 10 byte
printf()
scanf()
The printf() function is used for printing
output to the console.
The scanf() function is used for reading
input data from the console.
The format string can be %d (integer)etc
1.scanf("%d",&x);
printf("sum of 2 numbers:%d ",result);
Syntax:
printf("format string",argument_list);
scanf("format string",&argument);
Format
Specifier
Type Format Specifier
int %d.%i
unsigned int %u
short int %hd
long int %ld, %li
unsigned long int %lu
char %c
signed char %c
unsigned char %c
float %f
double %lf
long double %Lf
The Format specifier determines the
format of the input and output.
The format string always starts with a '%'
character.
Now that was
a lot of
theory!!!
Let's do some
coding
#include<stdio.h>
void main()
{
int no1,no2;
int sum=0;
printf("Enter no1");
scanf("%d",&no1);
printf("Enter no2");
scanf("%d",&no2);
sum = no1+no2;
printf("The sum is %d",sum);
}
Let's convert the flowchart
to a program in C
#include<stdio.h>
void main()
{
int principal,time,rate;
float simpleInterest;
printf("Enter p,n,r");
scanf("%d%d%d",& principal,& time,& rate);
simpleInterest =(principal * time * rate)/100;
printf("The sum is %f", simpleInterest);
}
Program 2
Your turn to try!!
 import
Good! Try one More!
 Write an algorithm for converting Celsius to Fahrenheit
 Draw a flowchart
 And covert it into a Code in C
 T(°F) = T(°C) × 1.8 + 32
Great Going! Now Try all 3 together!

Fundamentals of Programming Constructs.pptx

  • 1.
  • 2.
    Father of C C Programming was developed by Dennis Ritchie for creating system applications that directly interact with the hardware devices such as drivers, kernels, etc.  C programming is considered as the base for other programming languages, that is why it is known as mother language.
  • 3.
  • 4.
    First C Program#include <stdio.h> void main() { //Print a statement printf(“Welcome"); }
  • 5.
    How C Works Output Loader •CPU Linker • Executable code(.exe) Compiler • Obj codes(.o) Preprocessor • IncludeFiles and replaced Symbols Editor or IDE • Source Code(.c) and Header(.h)
  • 6.
    Variable A variable isa name of the memory location. It is used to store data. Its value can be changed, hence it is called a variable Syntax: datatype varName; Eg: int no;
  • 7.
    Rules for a Variable Avariable can have alphabets, digits, and underscore. •A variable name can start with the alphabet, and underscore only. It can't start with a digit. •No space is allowed within the variable name. •A variable name must not be any reserved word or keyword, e.g. int, float, etc
  • 8.
    Variables VALID  int a; int _ab;  int a30; INVALID  int 2;  int simple interest;  int float;
  • 9.
    Variables DECLARATION  int a; int a = 80; INITIALISATION
  • 10.
    Types of Variables LOCALGLOBAL STATIC AUTOMATIC EXTERNAL
  • 11.
    Local Variable DEFINITION  Avariable that is declared inside the function or block is called a local variable.  It is available only for the function or the block  You must have to initialize the local variable before it is used. EXAMPLE void main() { int x=10;//local variable }
  • 12.
    Automatic Variable DEFINITION  Thevariables which are declared inside a block are known as automatic or local variables;  These variables allocates memory automatically upon entry to that block and free the occupied memory upon exit from that block.  We can explicitly declare an automatic variable using auto keyword. EXAMPLE void main(){ int x=10;//local (also automatic) auto int y=20;//automatic variable }
  • 13.
    Global Variable DEFINITION  Avariable that is declared outside the function or block is called a global variable.  Any function can change the value of the global variable.  It is available to all the functions. EXAMPLE int value=20;//global variable void main() { int x=10;//local variable }
  • 14.
    Static Variable DEFINITION  Avariable that is declared with the static keyword is called static variable.  It retains its value between multiple function calls.  The local variable will print the same value for each function call, e.g, 11,11,11 and so on.  But the static variable will print the incremented value in each function call, e.g. 11, 12, 13 and so on. EXAMPLE void function1(){ int x=10;//local variable static int y=10;//static variable x=x+1; y=y+1; printf("%d,%d",x,y); }
  • 15.
    External Variable DEFINITION  Wecan share a variable in multiple C source files by using an external variable.  To declare an external variable, use extern keyword. EXAMPLE extern int x=10; //external variable (also global)
  • 16.
    Datatype  A datatype specifies the type of data that a variable can store such as number, character or decimal etc.  The basic data types are integer-based and floating-point based.  C language supports both signed and unsigned literals.  The memory size of the basic data types may change according to 32 or 64-bit operating system.
  • 17.
    Datatype int are wholenumbers that can have both zero, positive and negative values but no decimal values. float and double and are used to hold real numbers. char is used for declaring character type variables Void is an incomplete type. It means "nothing" or "no type". If you need to use a large/small number, you can use a type specifier long/short
  • 18.
    Signed and unsigned  InC, signed and unsigned are type modifiers. You can alter the data storage of a data type by using them. For example,  unsigned int x;  int y;  Here, the variable x can hold only zero and positive values because we have used the unsigned modifier.  Considering the size of int is 4 bytes, variable y can hold values from -231 to 231-1, whereas variable x can hold values from 0 to 232-1.
  • 19.
    Data Types MemorySize Range char 1 byte −128 to 127 signed char 1 byte −128 to 127 unsigned char 1 byte 0 to 255 short 2 byte −32,768 to 32,767 signed short 2 byte −32,768 to 32,767 unsigned short 2 byte 0 to 65,535 int 4 byte -2,147,483,648 to 2,147,483,647 signed int 2 byte −32,768 to 32,767 unsigned int 4 byte 0 to 4,294,967,295 short int 2 byte −32,768 to 32,767 signed short int 2 byte −32,768 to 32,767 unsigned short int 2 byte 0 to 65,535 long int 4 byte -2,147,483,648 to 2,147,483,647 signed long int 4 byte -2,147,483,648 to 2,147,483,647 unsigned long int 4 byte 0 to 4,294,967,295 float 4 byte double 8 byte long double 10 byte
  • 20.
    printf() scanf() The printf() functionis used for printing output to the console. The scanf() function is used for reading input data from the console. The format string can be %d (integer)etc 1.scanf("%d",&x); printf("sum of 2 numbers:%d ",result); Syntax: printf("format string",argument_list); scanf("format string",&argument);
  • 21.
    Format Specifier Type Format Specifier int%d.%i unsigned int %u short int %hd long int %ld, %li unsigned long int %lu char %c signed char %c unsigned char %c float %f double %lf long double %Lf The Format specifier determines the format of the input and output. The format string always starts with a '%' character.
  • 22.
    Now that was alot of theory!!! Let's do some coding
  • 23.
    #include<stdio.h> void main() { int no1,no2; intsum=0; printf("Enter no1"); scanf("%d",&no1); printf("Enter no2"); scanf("%d",&no2); sum = no1+no2; printf("The sum is %d",sum); } Let's convert the flowchart to a program in C
  • 24.
    #include<stdio.h> void main() { int principal,time,rate; floatsimpleInterest; printf("Enter p,n,r"); scanf("%d%d%d",& principal,& time,& rate); simpleInterest =(principal * time * rate)/100; printf("The sum is %f", simpleInterest); } Program 2
  • 25.
  • 26.
  • 27.
     Write analgorithm for converting Celsius to Fahrenheit  Draw a flowchart  And covert it into a Code in C  T(°F) = T(°C) × 1.8 + 32 Great Going! Now Try all 3 together!

Editor's Notes

  • #4 C language is considered as the mother language of all the modern programming languages because most of the compilers, JVMs, Kernels, etc. are written in C language, and most of the programming languages follow C syntax, for example, C++, Java, C#, etc. It provides the core concepts like the array, strings, functions, file handling, etc. that are being used in many languages like C++, Java, C#, etc. 2. It is System Programming Language because it can be used to do low-level programming (for example driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is written in C A procedure is known as a function, method, routine, subroutine, etc. A procedural language specifies a series of steps for the program to solve the problem. A procedural language breaks the program into functions, data structures, etc. C is a procedural language. In C, variables and function prototypes must be declared before being used. A structured programming language is a subset of the procedural language. Structure means to break a program into parts or blocks so that it may be easy to understand. In the C language, we break the program into parts using functions. It makes the program easier to understand and modify.
  • #5 Every C program uses libraries, which give the ability to execute necessary functions. For example, the most basic function called printf, which prints to the screen, is defined in the stdio.h header file. To add the ability to run the printf command to our program, we must add the following include directive to our first line. The second part of the code is the actual code which we are going to write. The first code which will run will always reside in the main function. Void means that nothing will return from the function.
  • #6 1) C program (source code) is sent to preprocessor first. The preprocessor is responsible to convert preprocessor directives into their respective values. The preprocessor generates an expanded source code.It removes comments , expands the code. 2) Expanded source code is sent to compiler which compiles the code and converts it into assembly code. 3) The assembly code is sent to assembler which assembles the code and converts it into object code. Now a(.o) file is generated. 4) The object code is sent to linker which links it to the necessary libraries. Then it is converted into executable code. A simple.exe file is generated. 5) The executable code is sent to loader which loads it into memory and then it is executed. After execution, output is sent to console.
  • #8 Give sensible name , not single letters for variable names
  • #11 5 types of variable in C
  • #13 The variables which are declared inside a block are known as automatic or local variables; these variables allocates memory automatically upon entry to that block and free the occupied memory upon exit from that block.
  • #15 Memory allocation for static variable happens only once wen it is loaded in the memory. For int X everytime u enter the function a memory is allocated…hence every time u print X it will be 11 But for Y since its static memory allocation happens only once and every time +1 is added it will be reflected in the same memory so it will go on as 11 12 13
  • #16 The variables which are declared inside a block are known as automatic or local variables; these variables allocates memory automatically upon entry to that block and free the occupied memory upon exit from that block.
  • #18 In C, floating-point numbers can also be represented in exponential. For example,
  • #20 Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For example, 0, -5, 10 float and double are used to hold real numbers. The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type) is 8 bytes. Keyword char is used for declaring character type variables. For example,
  • #22 Varies for 32bit/64bit
  • #24 #include<stdio.h> void main() { int no1,no2; int sum=0; printf("Enter no1"); scanf("%d",&no1); printf("Enter no2"); scanf("%d",&no2); sum = no1+no2; printf("The sum is %d",sum); }
  • #25 #include<stdio.h> void main() { int p,n,r; float si; printf("Enter p,n,r"); scanf("%d%d%d",&p,&n,&r); si=(p*n*r)/100; printf("The sum is %f",si); }
  • #28 T(°F) = T(°C) × 1.8 + 32