C Programming Lecture - 01
C Programming Lecture - 01
Introduction to C Language
• IT Consultant: Royel Tulip Sea Pearl Beach Resort and Spa, Coxbazar
• : Easytrax World Ltd, Dhaka
• Coordinator, Pipilika
• Instructor: CCNA Program, CISCO Local Academy
• Director: Data & Design Research Lab, SUST
About myself
• PhD in Computing, Curtin University, Australia
• M.Sc. in Software Engineering, Sweden
• M.Sc. in Informatics, Spain
• B.Sc. in Computer Science and Information Technology, IUT, Bangladesh
• Research Interest:
• Class URL:
• C programming is highly efficient. That’s the main reason why it’s very
popular despite being more than 47 years old.
• Standard C programs are portable. The source code written in one system
works in another operating system without any change.
• C99. In late 1990’s, several new features like inline functions, several new
data types and flexible array-members were added to the C standard. This is
commonly known as C99.
• C11. The C11 standard has new features like type generic macros, atomic
operations, anonymous structures that doesn’t exist in C99.
• All these three standards are also known by the name of ANSI C.
Features of C Programming Language
• A procedural language.
• Use of Modularity.
• General purpose.
What will you gain if you learn C?
• Under Windows XP / Vista / 7 / 8.x / 10 section, click the link with mingw-
setup(highlighted row) either from Sourceforge.net or FossHub.
• Open the Code::Blocks Setup file and follow the instructions (Next > I agree >
Next > Install); you don’t need to change anything. This installs the Code::Blocks
with gnu gcc compiler, which is the best compiler to start with for beginners.
• Now, open Code::Blocks and go to File > New > Empty file (Shortcut: Ctrl +
Shift + N)
• Write the C code and save the file with .c extension. To save the file, go to File >
Save (Shortcut: Ctrl + S).
Important: The filename should end with a .c extension, like: hello.c, your-
program-name.c
• To run the program, go to Build > Build and Run (Shortcut: F9). This will build
the executable file and run it.
The fun begins: Your first C program
• “Hello, World!” is a simple program that displays “Hello, World!” on the screen.
Since, it’s a very simple program, it is used to illustrate the basic syntax of any
programming language.
• #include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
How “Hello, World!” program works?
• The code inside the curly braces { } is the body of main() function.
The main() function is mandatory in every C program.
• int main() { }
This program doesn’t do anything but, it’s a valid C program.
How “Hello, World!” program works?
• All C program starts from the main() function and it’s mandatory.
• You can use the required header file that’s necessary in the program. For
example: To use sqrt() function to calculate square root and pow() function
to find power of a number, you need to include math.h header file in your
program.
• The C program ends when the program encounters the return statement
inside the main()function. However, return statement inside the main
function is not mandatory.
• To indicate the storage area, each variable should be given a unique name
(identifier). Variable names are just the symbolic representation of a
memory location. For example:
int playerScore = 95;
• Note: You should always try to give meaningful names to variables. For example: firstNameis
a better variable name than fn.
Rules for naming a variable
int number = 5; //
integer variable number = 5.5; // error
double number; // error
• Here, the type of number variable is int. You cannot assign floating-
point (decimal) value 5.5 to this variable. Also, you cannot redefine
the type of the variable to double. By the way, to store decimal
values in C, you need to declare its type to either double or float.
C Programming Data Types
Fundamental Data Types
▫ Integer types
▫ Floating type
▫ Character type
Derived Data Types
▫ Arrays
▫ Pointers
▫ Structures
▫ Enumeration
int - Integer data types
• Integers are whole numbers that can have both positive and negative values
but no decimal values. Example: 0, -5, 10
• In C programming, keyword int is used for declaring integer variable. For
example:
int id; Here, id is a variable of type integer.
• You can declare multiple variable at once in C programming. For example:
int id, age;
• The size of int is either 2 bytes(In older PC's) or 4 bytes. If you consider an
integer having size of 4 byte( equal to 32 bits), it can take 232 distinct states
as: -231,-231+1, ...,-2, -1, 0, 1, 2, ..., 231-2, 231-1. If you try to store larger
number than 231-1, i.e,+2147483647 and smaller number than -231, i.e, -
2147483648, program will not run correctly.
• Similarly, int of 2 bytes, it can take 216 distinct states from -215 to 215-1.
float - Floating types
• Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc.
You can declare a floating point variable in C by using
either float or double keyword. For example:
float accountBalance; double bookPrice;
• Here, both accountBalance and bookPrice are floating type variables.
• Size qualifiers
• Size qualifiers alters the size of a basic type. There
are two size qualifiers, long and short. For example:
• long double i; The size of double is 8 bytes.
However, when long keyword is used, that variable
becomes 10 bytes.
• Learn more about long keyword in C programming.
• There is another keyword short which can be used if
you previously know the value of a variable will
always be a small number.
C Qualifiers
• Sign qualifiers
• Integers and floating point variables can hold both negative
and positive values. However, if a variable needs to hold
positive value only, unsigned data types are used. For
example:
• // unsigned variables cannot hold negative value unsigned int
positiveInteger;There is another qualifier signed which can
hold both negative and positive only. However, it is not
necessary to define variable signed since a variable is signed
by default.
• An integer variable of 4 bytes can hold data from -231 to 231-1.
However, if the variable is defined as unsigned, it can hold
data from 0 to 232-1.
• It is important to note that, sign qualifiers can be applied
to int and char types only.
C Input Output (I/O)