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

Introduction To C

Uploaded by

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

Introduction To C

Uploaded by

Ravinder Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C Programming

Topperworld.in

Introduction to C

• C is a general-purpose programming language created by “Dennis Ritchie”


at the Bell Laboratories in 1972.
• It is a very popular language, despite being old.
• C is strongly associated with UNIX, as it was developed to write the UNIX
operating system.
• C programming is considered as the base for other programming languages,
that is why it is known as mother language.

⚫ It can be defined by the following ways:

➢ Mother language
➢ System programming language
➢ Procedure-oriented programming language
➢ Structured programming language
➢ Mid-level programming language

Features of C Language
Here are some of the most important features of the C language:

 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

©Topperworld
C Programming

Why Learn C ?

→ It is one of the most popular programming languages in the world


→ If you know C, you will have no problem learning other popular
programming languages such as Java, Python, C++, C#, etc, as the syntax
is similar
→ C is very fast, compared to other programming languages, like Java and
Python
→ C is very versatile; it can be used in both applications and technologies

Structure of the C program


• By structure, it is meant that any program can be written in this structure
only. Writing a C program in any other structure will hence lead to a
Compilation Error.

©Topperworld
C Programming

The structure of a C program is as follows:

Components of a C Program:
1. Header Files Inclusion – Line 1

[#include <stdio.h>]
⚫ The first and foremost component is the inclusion of the Header files in a C
program.
⚫ A header file is a file with extension .h which contains C function declarations
and macro definitions to be shared between several source files. All lines
that start with # are processed by a preprocessor which is a program invoked
by the compiler.
⚫ In the above example, the preprocessor copies the preprocessed code of
stdio.h to our file. The .h files are called header files in C.
Some of the C Header files:

• stddef.h – Defines several useful types and macros.


• stdint.h – Defines exact width integer types.
• stdio.h – Defines core input and output functions
• stdlib.h – Defines numeric conversion functions, pseudo-random network
generator, and memory allocation
• string.h – Defines string handling functions
• math.h – Defines common mathematical functions.

©Topperworld
C Programming

2. Main Method Declaration – Line 2

[int main()]

• The next part of a C program is to declare the main() function. It is the


entry point of a C program and the execution typically begins with the first
line of the main().
• The empty brackets indicate that the main doesn’t take any parameter.
• The int that was written before the main indicates the return type of
main(). The value returned by the main indicates the status of program
termination.

3. Body of Main Method – Line 3 to Line 6

[enclosed in {}]

• The body of a function in the C program refers to statements that are a


part of that function. It can be anything like manipulations, searching,
sorting, printing, etc.
• A pair of curly brackets define the body of a function. All functions must
start and end with curly brackets.

4. Statement – Line 4

[printf(“Hello World”);]

• Statements are the instructions given to the compiler. In C, a statement is


always terminated by a semicolon (;).
• In this particular case, we use printf() function to instruct the compiler to
display “Hello World” text on the screen.

©Topperworld
C Programming

5. Return Statement – Line 5

[return 0;]

• The last part of any C function is the return statement. The return
statement refers to the return values from a function.
• This return statement and return value depend upon the return type of
the function. The return statement in our program returns the value from
main().
• The returned value may be used by an operating system to know the
termination status of your program.

C first Program
To begin with, the “Hello World” program is the first step towards learning any
programming language and also one of the simplest programs you will learn. All
one needs to do is display the message “Hello World” on the screen.
Let’s look at the program and try to understand the terminologies involved in it.

// Simple C program to display "Hello World"

// Header file for input output functions


#include <stdio.h>

// main function -
// where the execution of program begins
int main()
{
printf("Hello World");

return 0;
}

Output: Hello World

©Topperworld
C Programming

➢ Compiling a C Program

• The compilation is the process of converting the source code of the C


language into machine code.
• As C is a mid-level language, it needs a compiler to convert it into an
executable code so that the program can be run on our machine.
• The C program goes through the following phases during compilation:

Let's understand through an example.

hello.c

1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello Topperworld");
5. return 0;
6. }

In the above example, the following steps are taken to execute a program:

o Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the
preprocessor converts the source code into expanded source code. The
extension of the expanded source code would be hello.i.

©Topperworld
C Programming

o The expanded source code is passed to the compiler, and the compiler
converts this expanded source code into assembly code. The extension of the
assembly code would be hello.s.
o This assembly code is then sent to the assembler, which converts the
assembly code into object code.
o After the creation of an object code, the linker creates the executable file.
The loader will then load the executable file for the execution.

©Topperworld

You might also like