Programming Section 3
Programming Section 3
Section - 3
Introduction to C
• C is a programming language developed in 1972.
• C is highly portable i.e., software written for one computer can be run
on another computer.
• It is developed for creating system applications that directly interact
with the hardware devices.
• C is also a compiled language, which means that source code must be
compiled into machine code before it can be executed.
3
Facts of C
• C was invented to write an operating system called UNIX.
• Today c is the most widely used and popular system programming
language.
• Most of software have been implemented using C.
• Today’s most popular Linux OS, Oracle and RDBMS Mysql have benn
written in C.
• It can be defined by the following ways:
❖ Mother language
❖ System programming language
❖ Procedure-oriented programming language
❖ Structured programming language
❖Mid-level programming language
4
why C ?
• Easy to learn and write programs.
• Structured language.
• Many common programs are written in C.
• Programs that are created with C run very quickly.
5
What is a compiler?
• A compiler is a program that translates a high-level language
(for example, C, C++, and Java) into a low-level language
(object program or machine program).
6
What is a compiler?
• To write C programs, you typically use a text editor to write
source code files with a .c extension. You then use a compiler to
translate the source code into machine code that can be
executed on your computer. Common C compilers include GCC,
Clang, and Microsoft Visual C++.
7
How to download
codeblocks
8
9
CODE BLOCKS
Step 1: Download
• Go to
• Downloads - Code::Blocks
(codeblocks.org)
CODE BLOCKS
Step 1: Download
CODE BLOCKS
Step 1: Download
CODE BLOCKS
Step 1: Download
CODE BLOCKS
Step 2: Install
CODE BLOCKS
Step 2: Install
CODE BLOCKS
Step 2: Install
CODE BLOCKS
Step 2: Install
CODE BLOCKS
Step 2: Install
CODE BLOCKS
Step 2: Install
CODE BLOCKS
Step 2: Install
CODE BLOCKS
Step 3: Launch the program
21
CODE BLOCKS
Step 4: Verify the compiler’s path
1. Go to settings
2. “compiler”
3. Select GNU GCC Compiler
4. Select toolchain executable
5. Check the compiler’s installation
directory
22
CODE BLOCKS
Step 4: Verify the debugger’s path
1. Go to settings
2. “Debugger”
3. Expand GDB/CDB debugger
4. Select default
5. In executable path provide the full path
name of “gdb.exe”
C Program Structure
1
Header
2
Main()
3
Variable Declaration
4
Body
5
return
23
CODE BLOCKS 24
Simple example of a program
# include <stdio.h>
int main(){
return 0;
}
25
What is a header file?
26
Variable in C language
• 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.
• the syntax to declare a variable:
• Example:
int a;
float b;
char c;
27
Data types:
28
Variable Declaration in C
int i, j, k;
char c, ch;
float f, salary;
double d;
int d = 3, f = 5;
char x = 'x';
29
Rules for defining variables
•A variable name can start with the alphabet, and underscore only. It
can't start with a digit.
•No special characters are allowed within the variable name ($,@,&,%).
•A variable name must not be any reserved word or keyword, e.g. int,
float, etc 30
Keywords in
C
Format specifiers
•The Format specifier is a string used in the formatted input and output
functions. The format string determines the format of the input and
output. The format string always starts with a '%' character.
Data type Format type
int %d or %i It is used to print the signed integer value where signed
integer means that the variable can hold both positive and
negative values.
float %f It is used for printing the decimal floating-point values. By
default, it prints the 6 values after '.'.
char %c It is used to print the unsigned character.
Double %lf it can easily accommodate about 15 to 17 digits after or
before a decimal point. 32
Outputs
33
Printf() Basics
• Print sentence:
• Printf(“Hello world!”); Output: Hello world!
• Print variable:
• Float x=100.56;
• Printf(“ %f”, x); Output: 100.56
#include <stdio.h>
#include <stdio.h> int main(){
int main(){ int x = 30;
printf(“hello world”); float y = 22.5;
char z = ‘a’;
printf(“the value of x = %d \n”,x);
return 0;
printf(“the value of y = %f \n”,y);
printf(“the value of z = %c \n”,z);
} return 0;
35
Arithmetic Operators
36
Examples:
Output
37
Examples:
Output
Examples:
Output
comment
• Comments in C language are used to provide information about lines of code. It is widely used for
documenting code.
• There are 2 types of comments in the C language.
40
Single Line Multi-Line
41
THANK YOU