C Programming Language
Lecture-01
Md. Sohag Hossain
Maintenance Engineer (Principal Officer)
Rajshahi Krishi Unnayan Bank (RAKUB)
Structured Programming
Structured Programming is a type of programming that generally converts large
or complex programs into more manageable and small pieces of code.
These small pieces of codes are usually known as functions or modules or sub-
programs of large complex programs.
It is known as modular programming and minimizes the chances of function
affecting another.
What is C Programming Language?
C is a general-purpose programming language that is extremely popular, simple, and
flexible to use. It is a structured programming language that is machine-independent
and extensively used to write various applications, Operating Systems like
Windows, and many other complex programs like Oracle database, Git, Python
interpreter, and more.
Features
• Procedural Language
• Fast and Efficient
• Modularity
• Statically Type
• General-Purpose Language
• Rich set of built-in Operators
• Libraries with Rich Functions
• Middle-Level Language
• Portability
• Easy to Extend
Advantages of Structured Programming Approach:
1. Easier to read and understand
2. User Friendly
3. Easier to Maintain
4. Mainly problem based instead of being machine based
5. Development is easier as it requires less effort and time
6. Easier to Debug
7. Machine-Independent, mostly.
Disadvantages of Structured Programming Approach:
1. Since it is Machine-Independent, So it takes time to convert into machine
code.
2. The converted machine code is not the same as for assembly language.
3. The program depends upon changeable factors like data-types. Therefore it
needs to be updated with the need on the go.
4. Usually the development in this approach takes longer time as it is language-
dependent. Whereas in the case of assembly language, the development
takes lesser time as it is fixed for the machine.
Compiler Vs Interpreter
Compiler Interpreter
A compiler translates the entire An interpreter translates the entire
source code in a single run. source code line by line.
It consumes less time i.e., it is faster It consumes much more time than the
than an interpreter. compiler i.e., it is slower than the
compiler.
It is more efficient. It is less efficient.
CPU utilization is more. CPU utilization is less as compared
to the compiler.
Both syntactic and semantic errors Only syntactic errors are checked.
can be checked simultaneously.
The compiler is larger. Interpreters are often smaller than
compilers.
It is not flexible. It is flexible.
The localization of errors is difficult. The localization of error is easier
than the compiler.
A presence of an error can cause the A presence of an error causes only a
whole program to be re-organized. part of the program to be re-
organized.
The compiler is used by the language An interpreter is used by languages
such as C, C++. such as Java.
Keyword and Identifier
• Keywords and Identifiers are the building blocks of any program in C
programming.
Keywords are reserved words, that have some special meaning in C
programming.
Identifiers are names, given to variables, functions, pointers, etc. in a program.
• There are 32 keywords in the C programming language.
• Keywords cannot be used as Identifiers in C programming.
Rules for naming identifiers
1. A valid identifier can have letters (both uppercase and lowercase letters),
digits and underscores.
2. The first letter of an identifier should be either a letter or an underscore.
3. You cannot use keywords like int, while etc. as identifiers.
4. There is no rule on how long an identifier can be. However, you may run
into problems in some compilers if the identifier is longer than 31 characters.
Variable
• A variable is a name of the memory location. It is used to store data. Its
value can be changed, and it can be reused many times.
• It is a way to represent memory location through symbol so that it can be
easily identified.
• Let's see the syntax to declare a variable:
• type variable_list;
• The example of declaring the variable is given below:
• int a;
• float b;
• char c;
C Variable Types
The C variables can be classified into the following types:
1. Local Variables
2. Global Variables
3. Static Variables
4. Automatic Variables
5. Extern Variables
6. Register Variables
1. Local Variables in C
A Local variable in C is a variable that is declared inside a function or a block of
code. Its scope is limited to the block or function in which it is declared.
Example of Local Variable in C
// C program to declare and print local variable inside a
// function.
#include <stdio.h>
void function()
{
int x = 10; // local variable
printf("%d", x);
}
int main() {
function();
}
2. Global Variables in C
A Global variable in C is a variable that is declared outside the function or a block
of code. Its scope is the whole program i.e. we can access the global variable
anywhere in the C program after it is declared.
Example of Global Variable in C
// C program to demonstrate use of global variable
#include <stdio.h>
int x = 20; // global variable
void function1() { printf("Function 1: %d\n", x); }
void function2() { printf("Function 2: %d\n", x); }
int main()
{
function1();
function2();
return 0;
}
3. Static Variables in C
A static variable in C is a variable that is defined using the static keyword. It can
be defined only once in a C program and its scope depends upon the region where
it is declared (can be global or local).
The default value of static variables is zero.
Syntax of Static Variable in C
static data_type variable_name = initial_value;
4. Automatic Variable in C
All the local variables are automatic variables by default. They are also known as
auto variables.
Their scope is local and their lifetime is till the end of the block. If we need, we
can use the auto keyword to define the auto variables.
The default value of the auto variables is a garbage value.
Syntax of Auto Variable in C
auto data_type variable_name;
or
data_type variable_name; (in local scope)
5. External Variables in C
External variables in C can be shared between multiple C files. We can declare
an external variable using the extern keyword.
Their scope is global and they exist between multiple C files.
Syntax of Extern Variables in C
extern data_type variable_name;
6. Register Variables in C
Register variables in C are those variables that are stored in the CPU
register instead of the conventional storage place like RAM. Their scope
is local and exists till the end of the block or a function.
These variables are declared using the register keyword.
The default value of register variables is a garbage value.
Syntax of Register Variables in C
register data_type variable_name = initial_value;
Data Types
• 1. Integer – We use these for storing various whole numbers, such as 5, 8,
67, 2390, etc.
• 2. Character – It refers to all ASCII character sets as well as the single
alphabets, such as ‘x’, ‘Y’, etc.
• 3. Double – These include all large types of numeric values that do not come
under either floating-point data type or integer data type.
• 4. Floating-point – These refer to all the real number values or decimal
points, such as 40.1, 820.673, 5.9, etc.
• 5. Void – This term refers to no values at all. We mostly use this data type
when defining the functions in a program.
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 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
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
Input and Output functions
• printf()
• scanf()
• getchar()
• putchar()
• gets()
• puts()
Operators in C
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Other Operators
Types of Errors in C
Errors in C programming can disrupt the intended functionality of a program,
causing issues such as failed compilation, program crashes, or incorrect output.
There are several common types of errors:
Syntax Error
Run-Time Error
Logical Error
Semantic Error
Linker Error
1. Syntax Errors
Errors occur when you violate the rules of writing C syntax is said to be “Syntax
errors.” This compiler error indicates that this must be fixed before the code will be
compiled. The compiler identifies these errors as “compile-time errors.”
void main()
{
int a //here semi colon(;)missed
}
2. Run-Time Errors
Errors which are occurred after a successful compilation of the program are said to
be “run-time errors.” Number divisible by zero, array index out of bounds, string
index out of bounds, etc., are the most frequent run-time errors. These errors can’t
be very hard to detect at the compile time.
void main()
{
int a=10;
int c=a/0;// Here number divisible zero error occurs
}
3. Linker Errors
These errors are generated after compilation; we link the different object files with
the main’s object using the Ctrl+F9 shortcut key. These errors occur when the
executable program cannot be generated. This may be because of wrong function
declaration, importing incorrect header files, etc. Most frequent linker error is
writing Main() instead of a main() method.
void Main() // Here Main() method used instead of main() method
{
}
4. Logical Errors
If our expectation is one thing and the resulting output is another, then that kind of
error we call “Logical error.” Suppose we want the sum of the two numbers, but the
given output is the multiplication of 2 digits; this is a Logical error. Detecting such
errors is possible by performing line-by-line debugging.
void Main()
{
printf("%d",sum(10,20));
}
int sum(int a, int b)
{
return x*y;//expectation is sum but we are multiplying the
numbers
}
5. Semantic Errors
The C compiler generates this error only when the written code is in an
incomprehensible format.
void main()
{
int x, y, z;
x + y = z; //semantic error }
Conditional Statements
• There are the following types of conditional statements in C.
• If statement
• If-Else statement
• Nested If-else statement
• If-Else If ladder
• Switch statement
Loops in C Programming cont..
Entry Controlled Vs Exit Controlled Loop
Entry Controlled Loop Exit Controlled Loop
Test condition is checked first, and then Loop body will be executed first, and then
loop body will be executed. condition is checked.
If Test condition is false, loop body will If Test condition is false, loop body will be
not be executed. executed once.
for loop and while loop are the examples of do while loop is the example of Exit
Entry Controlled Loop. controlled loop.
Entry Controlled Loops are used when Exit Controlled Loop is used when
checking of test condition is mandatory checking of test condition is mandatory
before executing loop body. after executing the loop body.
Thanks