0% found this document useful (0 votes)
11 views

Programming Section 3

Uploaded by

BADR ESLAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming Section 3

Uploaded by

BADR ESLAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Programming

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)

• Click “Download the binary


release”
10

CODE BLOCKS
Step 1: Download

• Select your operating system


(windows XP/ vista/7/8x/ 10)
11

CODE BLOCKS
Step 1: Download

• Download the installer with


GCC Compiler

• Which includes compiler and


debuger
12

CODE BLOCKS
Step 1: Download

• The .exe file will start


download after 5 seconds.
13

CODE BLOCKS
Step 2: Install

• Go to downloads folder then


run the .exe file
14

CODE BLOCKS
Step 2: Install

• Accept the default options


15

CODE BLOCKS
Step 2: Install

• Accept the default options


16

CODE BLOCKS
Step 2: Install

• Accept the default options


17

CODE BLOCKS
Step 2: Install

• Accept the default options


18

CODE BLOCKS
Step 2: Install

• Accept the default options


19

CODE BLOCKS
Step 2: Install

• Accept the default options


20

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(){

printf(“hello world ”);

return 0;
}

25
What is a header file?

• A header file is a source file that has the .h extension.


• Header files contain functions.
• Whenever we require the definition of a function, then we simply
include that header file in which function is declared.
• #include<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 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 whitespace is allowed within the variable name.

•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

• we say Output, it means to display some data on screen,


printer, or in any file. C programming provides a set of built-in
functions to output the data on the computer screen.

• Printf( ) is used to print something on screen.

33
Printf() Basics

• Print sentence:
• Printf(“Hello world!”); Output: Hello world!
• Print variable:
• Float x=100.56;
• Printf(“ %f”, x); Output: 100.56

• Print text and variable:


• Int age=20;
• Printf(“My age is %d”, age); Output: My age is 20
34
Printf() Examples:

#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.

1. Single Line Comments


// write your comment here
2. Multi-Line Comments
/*
write your
Comment
here
*/

40
Single Line Multi-Line

41
THANK YOU

You might also like