Class 7 (Introduction To C)
Class 7 (Introduction To C)
Class 7 (Introduction To C)
6
S
Features of C
Procedural Language: Instructions are executed step by step.
Portable: We can move a C program from one platform to
another, and run it without any or minimal changes.
Speed: It is faster than most programming languages like Java,
Python, etc.
General Purpose: C programming can be used to develop
operating systems, databases, embedded systems, and many more.
Structured Language: It has the ability to divide and hide all the
information and instruction. Code can be partitioned into C using
functions or code block.
Limited number of Keywords.
7
S
Why to Learn C?
C helps us to understand the internal architecture of a
computer, how computer stores, and retrieves information.
It is easy to learn other programming languages like Java,
Python, etc., if we learn C.
Opportunity to work on open source projects is increased, if
we go through C.
Some of the largest open source projects, such as Linux
kernel, Python interpreter, SQLite database, etc., are written
in C programming.
8
S
Applications of C
Embedded system
Operating system
Graphical User Interface (GUI) (Ex: Adobe’s Photoshop)
Browsers and their extensions (Ex: Google's Chromium)
Databases (Ex: MySQL)
Compiler production
Internet of Things (IoT) applications
Gaming (Ex: Tic-Tac-Toe)
9
S
First C Program
#include<stdio.h> //Header file
#include<conio.h> //Header file
void main() //Main function
{
printf(“Hello!!! How are you?”); //Statement
}
10
S
First C Program (Cont…)
Header File
The files, which are specified in the include section is called as
Header File.
include is a directory, where all the header files like stdio.h, conio.h,
etc. are kept.
Angular brackets < and > instruct the preprocessor to look in the
standard folder, where all header files are kept.
Header file is given in .h extension.
These are precompiled files that have some predefined functions.
conio.h stands for “Console Input Output”, which manages
input/output on console based application.
stdio.h stands for Standard Input Output.
11
S
First C Program (Cont…)
Main Function
Main function is compulsory for any C program.
It is the “Entry Point or Start Point” of a program.
When a file is executed, from the main function the flow goes as per
our choice.
There may or may not be other functions written by us in a program.
void is a keyword.
After the main function has been declared, we have to specify the
opening and closing parentheses (Curly Brackets { }).
Semicolon “;” determines the end of the statement.
12
S
First C Program (Cont…)
Main Function (Cont…)
printf(): It generates the output by passing the text written inside
double quotes. Syntax:
printf(“format string”, argument);
Format string can be %d, %c, etc.
13
S
First C Program (Cont…)
How to Write/Run
Create a C program (Notepad or any other Text Editor) and save the
file as file_name.c.
Compile the program, which generates an .exe file (executable file).
Run the program.
The .exe file created after compilation is run and not the source file
i.e. .c file.
Different compiler supports different option for compiling and
running.
14
S
Compilation Process
There are 4 phases through which a program transformed
into an executable form:
Preprocessor: The source code is first passed to the pre-processor.
The preprocessor expands this code, and then, this expanded code or
pre-processed code is passed to the compiler.
Compiler: The compiler converts the expanded code into assembly
code.
Assembler: The assembly code is converted into object code by using
an assembler. The name of the object file generated by the assembler
is the same as the source file. The extension of the object file in DOS
is ‘.obj’, and in UNIX, the extension is ‘.o’.
Linker: It combines the object code of library files with the object
code of our program. The output of the linker is the executable file.
15
S
Compilation Process (Cont…)
Invalid variable
int 5;
int x y;
int if;
18
S
Variable (Cont…)
Types of Variable
Local Variable: A variable that is declared inside the function or
block is called as a local variable.
void name(){
int x=10; //Local variable
}
Global Variable: A variable that is declared outside the function or
block is called as a global variable. The value of a global variable can
be modified by a function.
int value=20; //Global variable
void name(){
int x=10; //Local variable
} 19
S
Variable (Cont…)
Types of Variable (Cont…)
Static Variable: A static variable is declared with the static keyword.
It is kept in the memory till the end of the program, whereas a normal
variable is destroyed, when a function is over.
The static variable inside the function holds the value not only till the
end of the function block, but till the end of the entire program.
20
S
Variable (Cont…)
Types of Variable (Cont…)
External Variable: We can share a variable in multiple C source files
by using an external variable. To declare an external variable, we need
to use extern keyword.
external_file.h
extern int x=10; //External variable
external.c
#include “external_file.h”
#include <stdio.h>
void value()
{
printf(“External variable: %d”, x);
}
21
22
23