Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

KONERU LAKSHMAIAH EDUCATION FOUNDATION

(Deemed to be University estd, u/s, 3 of the UGC Act, 1956)


(NAAC Accredited “A++” Grade University)
Green Fields, Guntur District, A.P., India – 522502

Department of School of Competitive Coding

Introduction to C Program:
C language Tutorial with programming approach for beginners and
professionals helps you to understand the C language tutorial easily. Our C
tutorial explains each topic with programs.

The C Language is developed by Dennis Ritchie for creating system


applications that directly interact with the hardware devices such as drivers,
kernels, etc.

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:

1. Mother language
2. System programming language
3. Procedure-oriented programming language
4. Structured programming language
5. Mid-level programming language

1) C as a mother language

C language is considered as the mother language of all the modern


programming languages because most of the compilers, JVMs, Kernels, etc. are
written in C language, and most of the programming languages follow C syntax,
for example, C++, Java, C#, etc.

It provides the core concepts like the array, strings, functions, file handling, etc.
that are being used in many languages like C++, Java, C#, etc.

2) C as a system programming language

A system programming language is used to create system software. C language


is a system programming language because it can be used to do low-level
programming (for example driver and kernel). It is generally used to create
hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is
written in C.

It can't be used for internet programming like Java, .Net, PHP, etc.

3) C as a procedural language

A procedure is known as a function, method, routine, subroutine, etc. A


procedural language specifies a series of steps for the program to solve the
problem.

A procedural language breaks the program into functions, data structures, etc.

C is a procedural language. In C, variables and function prototypes must be


declared before being used.

4) C as a structured programming language

A structured programming language is a subset of the procedural


language. Structure means to break a program into parts or blocks so that it may
be easy to understand.

In the C language, we break the program into parts using functions. It makes the
program easier to understand and modify.

5) C as a mid-level programming language

C is considered as a middle-level language because it supports the feature of


both low-level and high-level languages. C language program is converted into
assembly code, it supports pointer arithmetic (low-level), but it is machine
independent (a feature of high-level).

A Low-level language is specific to one machine, i.e., machine dependent. It is


machine dependent, fast to run. But it is not easy to understand.

A High-Level language is not specific to one machine, i.e., machine


independent. It is easy to understand.
Environment setup:
There are many compilers available for c and c++. You need to download any
one. Here, we are going to use Turbo C++. It will work for both C and C++. To
install the Turbo C software, you need to follow following steps.

1. Download Turbo C++


2. Create turboc directory inside c drive and extract the tc3.zip inside
c:\turboc
3. Double click on install.exe file
4. Click on the tc application file located inside c:\TC\BIN to write the c
program

1) Download Turbo C++ software

You can download turbo c++ from many sites. download Turbo c++

2) Create turboc directory in c drive and extract the tc3.zip

Now, you need to create a new directory turboc inside the c: drive. Now extract
the tc3.zip file in c:\truboc directory.

3) Double click on the install.exe file and follow steps

Now, click on the install icon located inside the c:\turboc


It will ask you to install c or not, press enter to install.

Change your drive to c, press c.

Press enter, it will look inside the c:\turboc directory for the required files.

Select Start installation by the down arrow key then press enter.

Now C is installed, press enter to read documentation or close the software.

4) Click on the tc application located inside c:\TC\BIN


Now double click on the tc icon located in c:\TC\BIN directory to write the c
program.

In windows 7 or window 8, it will show a dialog block to ignore and close the
application because fullscreen mode is not supported. Click on Ignore button.

Now it will showing following console.


Structure of a C program:
The structure of a C program means the specific structure to start the
programming in the C language. Without a proper structure, it becomes difficult
to analyze the problem and the solution. It also gives us a reference to write
more complex programs.

Let's first discuss about C programming.

C programming

C language combines the power of a low-level language and a high-level


language. The low-level languages are used for system programming, while the
high-level languages are used for application programming. It is because such
languages are flexible and easy to use. Hence, C language is a widely used
computer language.

It supports various operators, constructors, data structures, and loop constructs.


The features of C programming make it possible to use the language for system
programming, development of interpreters, compilers, operating systems,
graphics, general utilities, etc. C is also used to write other applications, such
as databases, compilers, word processors, and spreadsheets.

The essential features of a C program are as follows:

Pointers: it allows reference to a memory location by the name assigned to it in


a program.

Memory allocation: At the time of definition, memory is assigned to a variable


name, allowing dynamic allocation of the memory. It means that the program
itself can request the operating system to release memory for use at the
execution time.

Recursion: When a function calls itself, it is known as recursion.

Bit-manipulation: It refers to the manipulation of data in its lowest form. It is


also known as bits. The computer stores the information in binary format (0 and
1).

Let's start with the importance of specifying the structure of a C program.


Importance of structure of a C program

Sometimes, when we begin with a new programming language, we are not


aware about the basic structure of a program. The sections of a program usually
get shuffled and the chance of omission of error rises. The structure of a
language gives us a basic idea of the order of the sections in a program. We get
to know when and where to use a particular statement, variable, function,
curly braces, parentheses, etc. It also increases our interest in that
programming language.

Thus, the structure helps us analyze the format to write a program for the least
errors. It gives better clarity and the concept of a program.

Here, we will discuss the sections of a C program, some practical examples


with explanations, steps to compile and execute a C program.

Let's start.

Sections of a C program
The sections of a C program are listed below:

1. Documentation section
2. Preprocessor section
3. Definition section
4. Global declaration
5. Main function
6. User defined functions

Let's discuss it in detail.

Documentation section

It includes the statement specified at the beginning of a program, such as a


program's name, date, description, and title. It is represented as:

1. //name of a program

Or

1. /*
2. Overview of the code
3. .
4. */

Both methods work as the document section in a program. It provides an


overview of the program. Anything written inside will be considered a part of
the documentation section and will not interfere with the specified code.

Preprocessor section

The preprocessor section contains all the header files used in a program. It
informs the system to link the header files to the system libraries. It is given by:

1. #include<stdio.h>
2. #include<conio.h>

The #include statement includes the specific file as a part of a function at the
time of the compilation. Thus, the contents of the included file are compiled
along with the function being compiled. The #include<stdio.h> consists of the
contents of the standard input output files, which contains the definition of
stdin, stdout, and stderr. Whenever the definitions stdin, stdout, and stderr are
used in a function, the statement #include<stdio.h> need to be used.

There are various header files available for different purposes. For example, #
include <math.h>. It is used for mathematic functions in a program.

Define section

The define section comprises of different constants declared using the define
keyword. It is given by:

1. #define a = 2
Global declaration

The global section comprises of all the global declarations in the program. It is
given by:

1. float num = 2.54;


2. int a = 5;
3. char ch ='z';

The size of the above global variables is listed as follows:

char = 1 byte

float = 4 bytes

int = 4 bytes

We can also declare user defined functions in the global variable section.

Main function

main() is the first function to be executed by the computer. It is necessary for a


code to include the main(). It is like any other function available in the C
library. Parenthesis () are used for passing parameters (if any) to a function.
The main function is declared as:

 main()

We can also use int or main with the main (). The void main() specifies that the
program will not return any value. The int main() specifies that the program can
return integer type data.int main()

Or

 void main()

Main function is further categorized into local declarations,


statements, and expressions.

Local declarations

The variable that is declared inside a given function or block refers to as local
declarations.

1. main()
2. {
3. int i = 2;
4. i++;
5. }

Statements

The statements refers to if, else, while, do, for, etc. used in a program within
the main function.

Expressions

An expression is a type of formula where operands are linked with each other
by the use of operators. It is given by:

1. a - b;
2. a +b;
User defined functions

The user defined functions specified the functions specified as per the
requirements of the user. For example, color(), sum(), division(), etc.

The program (basic or advance) follows the same sections as listed above.

Return function is generally the last section of a code. But, it is not necessary
to include. It is used when we want to return a value. The return function returns
a value when the return type other than the void is specified with the function.

Return type ends the execution of the function. It further returns control to the
specified calling function. It is given by:

 return;

Or

 return expression ;

For example,

return 0;

Examples

Let's begin with a simple program in C language.

Example 1: To find the sum of two numbers given by the user

It is given by:

/* Sum of the two It is the comment section. Any statement described in it is not
numbers */ considered as a code. It is a part of the description section in a code.
The comment line is optional. It can be in a separate line or part of an
executable line.

#include<stdio.h> It is the standard input-output header file. It is a command of the


preprocessor section.
int main() main() is the first function to be executed in every program. We have
used int with the main() in order to return an integer value.

{… The curly braces mark the beginning and end of a function. It is


} mandatory in all the functions.

printf() The printf() prints text on the screen. It is a function for displaying
constant or variables data. Here, 'Enter two numbers to be added' is the
parameter passed to it.

scanf() It reads data from the standard input stream and writes the result into the
specified arguments.

sum = a + b The addition of the specified two numbers will be passed to the sum
parameter in the output.

return 0 A program can also run without a return 0 function. It simply states that
a program is free from error and can be successfully exited.

1. /* Sum of two numbers */


2. #include<stdio.h>
3. int main()
4. {
5. int a, b, sum;
6. printf("Enter two numbers to be added ");
7. scanf("%d %d", &a, &b);
8. // calculating sum
9. sum = a + b;
10. printf("%d + %d = %d", a, b, sum);
11. return 0; // return the integer value in the sum
12.}
Output

Compile and execution of a C program

Here, we will discuss the method to compile and run the C program with the
help of the command prompt.

The steps involved in a complete program execution are as follows:

1. Create a program
2. Compile a program
3. Run or execute a program
4. Output of the program

Create a program

It refers to the code created in any text editor. We can also compile and run the
C code in any software, such as Visual studio.

Compile a program

If refers to the process of checking the errors in the code. The computer displays
all the errors (if any) found in our specified C code. We can make further
changes to correct those errors.

Run or execute a program

The next step is the run or execution part. A program is assembled and linked
without any error. The computer performs different operations, such as
decoding, ALU operations to run a program.

Output of the program

It is the last part of the program where the output of the specified code is
produced as the result.
But, where to write a program and how to open a command prompt to run that
program. Do not worry; it is very easy to do all these steps. Let's start with the
step to compile and run a C program.

Step to compile and run a C program

We need first to ensure that the gcc compiler is already present on our PC or
not. If it is not installed, we need first to install the gcc compiler. We can also
try other methods to run a C program. But here, we have used the gcc compiler.

Step: 1 - gcc complier installation

We can directly install the latest gcc complier through the


link: https://jmeubank.github.io/tdm-gcc/

Complete all the steps during the installation till the process gets completed.

Step: 2 - creating a C program

Create a C program using the simple text editor. Here, we have used notepad. It
is shown below:

1. #include<stdio.h>
2. main()
3. {
4. printf("Hello, Welcome to the C programming \n");
5. return;
6. }

Now, save the file in any directory with the extension (.c). For example, we
have saved the file with the name 'welcome.c' on the desktop.

Step: 3 - Opening the command prompt

Open the cmd or command prompt on our computer. We can simply type cmd
on the search or the run option. The Command prompt will appear.

Step: 4 - rechecking the gcc complier

After the command prompt opens, type 'gcc -v' and press Enter. It will appear
as the image shown below:
It shows that gcc is successfully installed on our PC.

Step: 5 - go to the source directory

We now need to specify the source directory on the cmd. Type 'cd space source
directory' and press Enter. Since, we have saved our text editor file on the
desktop, so we will specify the source directory as desktop. It is given by:

 cd desktop
Step: 6 - compile the source code

Run the command 'gcc space full name of the file saved with the extension (.c)'
and press Enter, as shown below:

 gcc welcome.c

If there is any error in our file, it will appear. Otherwise, move on to the step 7.
Step: 7 - Compile the source code

The executable file is not yet named because we have not told the compiler to
perform any such task. So, we will first name the executable file by running the
command 'gcc space -o space (name of executable file) space (name of the
original file with the extension)' and press Enter. It is given by:

 gcc -o hello welcome.c

Here, we have given the executable name as hello. We can define the name as
per our convenience.

Step: 8 - Run the program

It is the last step to run a program. We will run the program in the command
prompt only. Here, we will type the name of the executable file without any
extension. The executable name will be the same as specified in step 7. It is
given by:

 hello

Press Enter. The output will appear on the command prompt, as shown below:

Similarly, we can run multiple C programs using the same steps, as discussed
above.
C Program

In this tutorial, all C programs are given with C compiler so that you can
quickly change the C program code.

File: main.c

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

Tokens in C:
Tokens in C is the most important element to be used in creating a program in
C. We can define the token as the smallest individual element in C. For
`example, we cannot create a sentence without using words; similarly, we
cannot create a program in C without using tokens in C. Therefore, we can say
that tokens in C is the building block or the basic component for creating
a program in C language.

Classification of tokens in C

Tokens in C language can be divided into the following categories:

o Keywords in C
o Identifiers in C

Let's understand each token one by one.

Keywords in C:
Keywords in C can be defined as the pre-defined or the reserved words having
its own importance, and each keyword has its own functionality. Since
keywords are the pre-defined words used by the compiler, so they cannot be
used as the variable names. If the keywords are used as the variable names, it
means that we are assigning a different meaning to the keyword, which is not
allowed. C language supports 32 keywords given below:
Auto double int Struct

Break else long Switch

Case enum register Typedef

Char extern return Union

Const float short Unsigned

Continue for signed Void

Default goto Sizeof Volatile

Do If Static While

Identifiers in C:
Identifiers in C are used for naming variables, functions, arrays, structures, etc.
Identifiers in C are the user-defined words. It can be composed of uppercase
letters, lowercase letters, underscore, or digits, but the starting letter should be
either an underscore or an alphabet. Identifiers cannot be used as keywords.
Rules for constructing identifiers in C are given below:

o The first character of an identifier should be either an alphabet or an


underscore, and then it can be followed by any of the character, digit, or
underscore.
o It should not begin with any numerical digit.
o In identifiers, both uppercase and lowercase letters are distinct. Therefore,
we can say that identifiers are case sensitive.
o Commas or blank spaces cannot be specified within an identifier.
o Keywords cannot be represented as an identifier.
o The length of the identifiers should not be more than 31 characters.
o Identifiers should be written in such a way that it is meaningful, short,
and easy to read.

You might also like