A high-level language (HLL) is a programming language such as C, FORTRAN, or Pascal that enables a programmer to
write programs that are more or less independent of a particular type of computer. Such languages are
considered high-level because they are closer to human languages and further from machine languages
Linux command mkdir dir1 dir2 dir3 → create 3 directories under root
cd dir1 → change current directory to dir1
ls -a = list all files and folders /dir1$cat>OS → create a file OS in dir1
ls <folderName> = list files in folder C programming → content of the file OS
cd <folderName> = change directory <ctrl+d> → save the file OS
cd / = go to root cat empty1 empty2>>empty3 → combine two files in another file
cd .. = go up one folder date +%d-%m-%y → display date
man <command> = shows manual who → check the current users
cat <fileName> = show content of file
mkdir = create new folder sulata@hamsa:~$ mkdir assign_c
cp image.jpg newimage.jpg = copy and rename a file sulata@hamsa:~$ cd assign_c
cp image.jpg <folderName>/ = copy to folder sulata@hamsa:~/assign_c$ vi assign1.c
mv filename.txt filename2.txt = rename file sulata@hamsa:~/assign_c$ ls
rm <filename> .. = delete file (s) assign1.c
rm -r <foldername>/ = delete folder sulata@hamsa:~/assign_c$cat assign1.c
touch empty1 empty2 → create two files empty1 sulata@hamsa:~/assign_c$cp assign1.c assignment1.c
and empty2 without using cat command sulata@hamsa:~/assign_c$rm assign1.c
echo “Ram is a good boy”>empty1 → write in file
empty1 without using cat command
Creating the program
Program must be entered into a file
Filename can consist of letters, digits and special characters, followed by a dot and a letter c
Example: hello.c, program.c, assign1.c
Creating file: Use vi text editor to create the file
Command for calling the editor and creating the file: vi <filename>
Example: vi assign1.c
If assign1.c is an existing file it will be loaded using the command vi assign1.c
Otherwise the command vi assign1.c creates a new file for receiving the new program
Any corrections in the existing program can be done using the editor
File assign1.c is saved on disk after editing
Source program: The program that is entered into the file assign1.c is the source program as it represents the original
form of the program
#include <stdio.h>
void main()
{printf (“hello, world\n”);}
esc:wq → write and quit → for storing the program in disk
Compilation
Compilation of the program is required to make the source program suitable for execution by the computer
Compilation translates the source program into object program and stores the object program into a new file
Command for compilation: cc assign1.c/gcc assign1.c
Linking
Linking is the process of putting together other programs files and functions that are required by the program in the file
assign1.c
For example, if the program is using printf function, then the object code of this function should be brought from the
stdio (standard input output) library of the system and linked to the main program
Linking is done at the time of using cc command
Executable object code: The compiled and linked program is called the executable object code and is stored
automatically in another file named a.out
Executing the program
The command for execution is ./a.out. It loads the executable object code into the computer memory and executes the
instructions.
Basic programs
Tells the compiler to include information about
the standard input/output library
1. Print the words → hello, world
stdio.h: Header file, stdio is standard input and
#include <stdio.h>
output
void main()
{printf (“hello, world\n”);} Every program must have a main function and program starts to execute from
main
Library function with the argument In C, a function may call other function , calling function provides a list of
“hello, world\n” arguments to the function it calls.
hello, world → sequence of characters The parenthesis after the function name surrounds the argument list
in double quotes = character string = main is defined to be a function that expects no argument, which is indicated
string constant by the empty list ( )
\n → C notation for newline character
Compilation: cc hello.c
C compiler program name
Run the program: ./a.out
Output: hello, world
2. Program to print Fahrenheit-Celsius #include <stdio.h>
#include <stdio.h> void main()
Start void main() {float fahr, Cel;
{ int fahr, Cel; //declaration statement printf(“Ënter fahr\n”);
Input printf(“Ënter fahr\n”); scanf (“%f”, &fahr);
fahr scanf (“%d”, &fahr); Cel=5*(fahr-32)/9;
Cel=5*(fahr-32)/9; printf(“%f,%f\n”, fahr, Cel);}
Cel=5*(fahr-32)/9 printf(“%d,%d\n”, fahr, Cel);}
%f → Format specifier for float
%d → Format specifier for integer Input: fahr = 0
Print scanf: library function to input fahr
Cel Output: 0.000000, -17.777779
&fahr: address of fahr
Input: fahr = 0
Stop Output: 0, -17 #include <stdio.h>
void main()
{int i;
float fahr, Cel;
printf(“Ënter fahr\n”);
Print 10 different (fahr, Cel) pair for (i=0;i<=9;i++)
{scanf (“%f”, &fahr);
Cel=5*(fahr-32)/9;
printf(“%f,%f\n”, fahr, Cel);}}
3. Print n numbers in the screen 4. Add n numbers and print the sum in the screen
#include <stdio.h> #include <stdio.h>
void main() void main()
{int number, i, n=10; {int number, i, n=10, sum=0;
printf(“Ënter number\n”); printf(“Ënter number\n”);
for (i=1;i<=n;i++) for (i=1;i<=n;i++)
{scanf (“%d”, &number); {scanf (“%d”, &number);
printf(“%d\n”, number);}} sum=sum+number;}
printf(“%d\n”, sum);}
5. Add 5 numbers and print the sum.
i=1;
#include <stdio.h> while(i<=5)
void main() {scanf (“%d”, &n);
{int i, sum=0, n; sum=sum+n;
printf(“Ënter number\n”); i=i+1;}
for (i=1;i<=5;i++) i=1;
{scanf (“%d”, &n) do
sum=sum+n;} {scanf (“%d”, &n);
printf(“%d\n”, sum);} sum=sum+n;
i=i+1;}while(i<=5);
Use of conditional statement
Item no. Quantity Price/unit
6. While purchasing certain items, a discount of 10% is offered if the
quantity purchased is more than 1000. If quantity and price per unit are 1 500 200/-
input through the keyboard, write a program to calculate the total
expenses. 2 1050 300/-
3 900 400/-
#include <stdio.h>
void main() Expenses=(500x200)+(900x400)+(
{int item=3, tot=0, i;
dis=% of discount (1050x300)-((1050x300x10)/100))
tot = total expenses
int Quantity, Rate; item = total number of items
for (i=1;i<=item;i++) Rate = price per unit
{printf(“Ënter Quantity and Rate\n”);
Start Is yes
scanf(“%d, %d”, &Quantity, &Rate); Quantity>1000
if(Quantity>1000) dis=0,tot=0,item=3 ?
tot=tot+((Quantity*Rate)-(Quantity*Ra no dis=10
te*10)/100); Input
Else Quantity, Rate
tot=tot+(Quantity*Rate);}
printf(“%d\n”, tot);} Is item = 0?
yes Print
End
item = item-1 tot
no
7. Write a program to print the largest of three 8. Add A and B.
numbers. #include <stdio.h>
#include <stdio.h> void main()
void main() {int A, B;
{int n1, n2, n3; printf(“Ënter A and B\n”);
printf(“Ënter n1, n2, n3\n”); scanf(“%d, %d”, &A, &B);
scanf(“%d,%d,%d”, &n1, &n2, &n3); printf(“%d\n”, A+B);}
if (n1>n2)
{if(n1>n3) 9. Write a program to print quotient and remainder.
printf(“n1 is the largest number\n”);
else #include <stdio.h>
printf(“n3 is the largest number\n”);} void main()
else {int dividend, divisor, quot, rem;
{if(n2>n3) printf(“Ënter dividend and divisor\n”);
printf(“n2 is the largest number\n”); scanf(“%d, %d”, ÷nd, &divisor);
else quot=dividend/divisor;
printf(“n3 is the largest number\n”);} rem=dividend%divisor
} printf(“%d,%d\n”, quot, rem);}
Input: n1=1, n2=3, n3=6
Output: n3 is the largest number
10. Write a program to print quotient 11. Write a program to enter elements in an array of size 6.
and remainder using function.
Function declaration #include <stdio.h>
#include <stdio.h> void main()
int quotient (int a, int b) {int i, arr[6];
{return a/b}; printf(“Ënter array elements\n”);
int remainder (int a, int b) for(i=0;i<=5;i++)
{return a%b}; Function call scanf(“%d”, &arr[i]);
void main() for(i=0;i<=5;i++)
{int dividend, divisor, quot, rem; printf(“%d”, arr[i]);}
printf(“Ënter dividend and divisor\n”);
scanf(“%d, %d”, ÷nd, &divisor); Print the mark of 100 students in the paper computing lab.
quot=quotient(dividend, divisor); Need to declare 100 variables for storing the computing lab mark of 100 students
rem=remainder(dividend, divisor); stu1, stu2, stu3,…….stu100 → variable name
printf(“%d,%d\n”, quot, rem);} int stu1, stu2, stu3,………stu100; → declaration statement
stu1=45, stu2=54, stu3=44,……stu100=76; → assignment statement
Formal parameter = a, b printf(“%d,%d,%d,…..%d\n”,stu1,stu2,stu3,….stu100);
Actual parameter=dividend, divisor
int mark_cl[100]; → declaration statement
for(i=0;i<=99;i++)
dividend=16, divisor=5 scanf(“%d”, &mark_cl[i]);
for(i=0;i<=99;i++)
printf(“%d”, mark_cl[i]);
12. Enter elements in a two dimensional array 13. Write a program to search for an element 42 in an array of size 6.
of size 4x4.
#include <stdio.h>
void main()
#include <stdio.h> {int i, arr[6];
void main() printf(“Ënter array elements\n”);
{int i, j, arr[4][4]; for(i=0;i<=5;i++)
printf(“Ënter array elements\n”); scanf(“%d”, &arr[i]);
for(i=0;i<=3;i++) for(i=0;i<=5;i++)
{for(j=0;j<=3;j++) {if arr[i] = 42
scanf(“%d”, &arr[i][j]);} printf(“%d the element of the array is 42”, i);}}
for(i=0;i<=3;i++)
{for(j=0;j<=3;j++)
printf(“%d”, &arr[i][j]);
printf(“\n”);}}
Output:
0154
5673
1134
8976