Professional Documents
Culture Documents
1 CP
1 CP
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.
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
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.
It can't be used for internet programming like Java, .Net, PHP, etc.
3) C as a procedural language
A procedural language breaks the program into functions, data structures, etc.
In the C language, we break the program into parts using functions. It makes the
program easier to understand and modify.
You can download turbo c++ from many sites. download Turbo c++
Now, you need to create a new directory turboc inside the c: drive. Now extract
the tc3.zip file in c:\truboc directory.
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.
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.
C programming
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.
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
Documentation section
1. //name of a program
Or
1. /*
2. Overview of the code
3. .
4. */
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:
char = 1 byte
float = 4 bytes
int = 4 bytes
We can also declare user defined functions in the global variable section.
Main function
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()
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
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.
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.
Here, we will discuss the method to compile and run the C program with the
help of the command prompt.
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.
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.
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.
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.
Complete all the steps during the installation till the process gets completed.
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.
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.
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.
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:
Here, we have given the executable name as hello. We can define the name as
per our convenience.
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
o Keywords in C
o Identifiers in C
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
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: