CP Practical-I
CP Practical-I
Theory:
C Basic Syntax
Syntax basically refers to the protocols to be followed while writing a program. It is very necessary
to follow proper syntax while coding to get the desired set of output. The C basic syntax consists of
header files, main function, and program code. This is the most fundamental structure in the C
program. A C program necessarily consists of the main function because the execution of the
program starts from this line. Without the main function, the program execution does not start.
Example2:
#include<stdio.h>
#include<conio.h>
int main() //main function with integer return type
{ clrscr();
printf(“C programming language”); //print statement to display output on the screen
return 0; // indicates that the main function returns null value getch();
}
1.Header Files
G H RAISONI INSTITUTE OF ENGINEERING & TECHNOLOGY
(Approved by AICTE, New Delhi and Recognized by DTE, Maharashtra)
Autonomous Institute Affiliated to Rashtrasant Tukadoji Maharaj Nagpur University, Nagpur Accredited
by NAAC with A+ Grade
Shraddha Park, B-37-39/1, MIDC, Hingna-Wadi Link Road, Nagpur-440016 (INDIA)
Phone Nos.: +91-07104-236102 Fax: +91-07104-236100
Email:- [email protected] Web: - www.ghrietn.raisoni.net
First Year Engineering Department
In the above code, we have used two header files.
#include<stdio.h>
#include<conio.h>
C has a series of predefined functions embedded in the header files in the C library. We use these
functions to perform a specific task with the help of a header file.
What are header files?
Header files contain a bunch of files that help the user to include the predefined functions according
to his requirements. You can add header files using the pre-processor directive #include.
2. Main Function
When your operating system runs a program in C, it gives the control of the computer to the C
program. If a program consists of only one line of code in the main function, then the program can
run successfully. As the program execution starts from this line, it becomes compulsory for every C
program to have the main function.
• clrscr() – This function is predefined in the header file <conio.h>. clrscr() helps to clear the
screen.
• printf() – This is another function that helps you to display the message on the output screen.
This function is defined in <stdio.h>. In the above program, it is pretty clear that the printf()
function helps you display the output message: “Welcome to DataFlair”.
• getch() – This function is defined in conio.h and is used to hold the output screen until we hit
the next key after we run the program.
3. Tokens in C
A C syntax/program consists of a series of tokens. They can be identifiers, variables, symbols. We
can say that they are the smallest units of a program that convey a specific meaning to the compiler.
There are 5 types of tokens in the C language. They are:
1. Keywords
2. Identifiers/Variables
3. Constants
4. Punctuators
5. Operators
Keywords
The following list shows the reserved words in C. These reserved words may not be used as
constants or variables or any other identifier names.
auto double int struct
breaks else long switch
case enum register typedef
char extern return union
const short float unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers
G H RAISONI INSTITUTE OF ENGINEERING & TECHNOLOGY
(Approved by AICTE, New Delhi and Recognized by DTE, Maharashtra)
Autonomous Institute Affiliated to Rashtrasant Tukadoji Maharaj Nagpur University, Nagpur Accredited
by NAAC with A+ Grade
Shraddha Park, B-37-39/1, MIDC, Hingna-Wadi Link Road, Nagpur-440016 (INDIA)
Phone Nos.: +91-07104-236102 Fax: +91-07104-236100
Email:- [email protected] Web: - www.ghrietn.raisoni.net
First Year Engineering Department
A C identifier is a name used to identify a variable, function, or any other user-defined item. An
identifier starts with a letter A to Z, a to z, or an underscore '_' followed by zero or more letters,
underscores, and digits (0 to 9).
C does not allow punctuation characters such as @, $, and % within identifiers. C is a case sensitive
programming language. Thus, Manpower and manpower are two different identifiers in C. Here are
some examples of acceptable identifiers − mohd zara abc move_name a_ 123 myname50 _temp j
a23b9 retVa
Constants in C
Constants are those values that cannot be changed during program execution.
Punctuators in C
Punctuators are used for formatting the C program statements and expressions.
Examples for punctuators are : [ ] { } ( ) , ; : * =
Let us talk about one of the punctuators i.e., semicolons
A semicolon is used to show the ending of a statement. It acts as a separator between two statements.
If you miss this statement at the end of any line, the compiler would join the statement with the next
line of code and will give a syntax error.
Operators in C
There are various operators in C that we use in order to perform different mathematical operations.
They are:
• Arithmetic Operators : +, -, *, /
• Increment/Decrement Operators : ++,– •
Relational Operators: ==, <, >, <=, >=, !=
• Logical operators : &&, ||, !
• Conditional Operators : ?:
• Detailed Algorithm :-
Step 1) Include all the necessary header files like stdio.h
Step 2) Declare the variables num1, num2 and result as int variables.
Step 3) Take input for both the variables num1 and num2.
Step 4) Carry Out the addition operation using the expression result =num1 + num2; Step
5) Display the result variable using the printf() function.
Program :-
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int num1, num2;
int result;
printf("Enter the first number : \n);
scanf("%d", &num1);
printf("Enter the second number : \n);
G H RAISONI INSTITUTE OF ENGINEERING & TECHNOLOGY
(Approved by AICTE, New Delhi and Recognized by DTE, Maharashtra)
Autonomous Institute Affiliated to Rashtrasant Tukadoji Maharaj Nagpur University, Nagpur Accredited
by NAAC with A+ Grade
Shraddha Park, B-37-39/1, MIDC, Hingna-Wadi Link Road, Nagpur-440016 (INDIA)
Phone Nos.: +91-07104-236102 Fax: +91-07104-236100
Email:- [email protected] Web: - www.ghrietn.raisoni.net
First Year Engineering Department
scanf("%d", &num2);
result = num1 + num2; printf("The
sum of %d and %d is
",num1,num2,result);
}
Flowchart :-
Start
Stop
Output :
G H RAISONI INSTITUTE OF ENGINEERING & TECHNOLOGY
(Approved by AICTE, New Delhi and Recognized by DTE, Maharashtra)
Autonomous Institute Affiliated to Rashtrasant Tukadoji Maharaj Nagpur University, Nagpur Accredited
by NAAC with A+ Grade
Shraddha Park, B-37-39/1, MIDC, Hingna-Wadi Link Road, Nagpur-440016 (INDIA)
Phone Nos.: +91-07104-236102 Fax: +91-07104-236100
Email:- [email protected] Web: - www.ghrietn.raisoni.net
First Year Engineering Department
Conclusion :- Hence, we have implemented the algorithm and flowchart using the C language.