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

Escape Sequence in C, Connecting

with Devices, &


Introduction to Python
Few more C programming Concepts

• Escape sequence: An escape sequence string literal is used to represent a


character that cannot be directly typed on the keyboard.

• An escape sequence in C language starts with a backslash (\) and


consists of only a single character

• Most common escape sequence is: \n (new line)


Write a C Program which rings a bell
Write a C Program which rings a bell

#include <stdio.h>

int main() {
printf("I am IITJ Student\a\n");

return 0;
}
Write a C program to print: I am Ω”
Write a C program to print: I am Ω”

#include <stdio.h>

int main(){
printf("I am \u03A9");

return 0;
}
Write a C program to print: "I ♥ Dogs”
Write a C program to print: "I ♥ Dogs”

#include <stdio.h>

int main() {

printf("I \u2665 Dogs\n");

return 0;
}
C program to change the text color
#include <stdio.h>

int main() {
// ANSI escape codes to set text color
printf("\033[1;31m"); // Set text to bright red
printf("This text is red!\n");

printf("\033[0;32m"); // Set text to green


printf("This text is green!\n");

printf("\033[0m"); // Resets the text to default color


printf("This text is back to default color.\n");

return 0;
}
What is this?
#include <stdio.h>
\033 -> escape sequence for color
int main() {
// ANSI escape codes to set text color
printf("\033[1;31m"); // Set text to red
printf("This text is red!\n");

printf("\033[0;32m"); // Set text to green


printf("This text is green!\n");

printf("\033[0m"); // Resets the text to default color


printf("This text is back to default color.\n");

return 0;
}
Many more escape sequences - study on your
own
Simple system level program
• Creating a system-level program in C can involve various tasks such as
le manipulation, process control, or interacting with the operating
system's system calls.

• A system-level program refers to software that operates at a low level in


the computing environment, directly managing and interacting with the
hardware or the operating system's kernel.

• These programs provide the fundamental functionalities necessary for the


hardware and the operating system to operate and serve higher-level
application software.
fi
Write a c program to run a linux OS command
Write a c program to run a linux OS command
#include <stdlib.h>
#include <stdio.h>

int main() {
printf("Running the 'ls' command to list directory contents:\n");

// Execute the 'ls' command using system()


int status = system("ls");

if (status == -1) {
• system() // If system() fails
printf("Failed to execute command\n");
} else {
// Output the return status of the 'ls' command
printf("\nCommand executed successfully with return status %d\n", WEXITSTATUS(status));
}

return 0;
}

Process Completion Status


https://www.gnu.org/software/libc/manual/html_node/Process-Completion-Status.html -> for WEXITSTATUS(status)
How can you access devices directly from your
program?

• Write a C program to connect to a printer and print simple.txt le

fi
Get the CUPS library

• sudo apt-get install libcups2-dev


#include <cups/cups.h>
#include <stdio.h>

int main() {
// Name of the le to print
const char * lename = "sample.txt";
// Name of the printer (NULL for the default printer)
const char *printer = NULL;

// Check if CUPS server is running and get the default printer if none speci ed
if ((printer = cupsGetDefault()) == NULL) {
fprintf(stderr, "Error: No default printer available\n");
return 1;
}

printf("Default printer: %s\n", printer);


printf("File to print: %s\n", lename);

// Add the job to the default printer queue


int job_id = cupsPrintFile(printer, lename, "C Program Print Job", 0, NULL);

if (job_id == 0) {
fprintf(stderr, "Failed to print le: %s\n", cupsLastErrorString());
return 1;
}

printf("Print job successfully submitted. Job ID: %d\n", job_id);


return 0;
}
fi
fi
fi
fi
fi
fi
Write a C program to access usb camera
Write a C program to access usb camera

• Install OpenCV

• sudo apt update

• sudo apt install libopencv-dev


// Create a window for display.
cvNamedWindow("Camera Output", CV_WINDOW_AUTOSIZE);

// Read and display frames from the camera until a key is pressed
while (1) {
// Capture frame-by-frame
IplImage* frame = cvQueryFrame(capture);
#include <opencv2/opencv.h>
#include <stdio.h> // If the frame is empty, break immediately
if (!frame)
int main() { break;
// Create a VideoCapture object and open the input le
// Change '0' to '1' or '2' // Display the resulting frame
//if you have multiple cameras to select which one to use cvShowImage("Camera Output", frame);
CvCapture* capture = cvCaptureFromCAM(0);
// Press 27 (ESC) to exit
// Check if camera opened successfully char c = (char)cvWaitKey(25);
if (!capture) { if (c == 27)
fprintf(stderr, "Error: Unable to open camera\n"); break;
return -1; }
}
// When everything done, release the video capture and write object
cvReleaseCapture(&capture);
cvDestroyWindow("Camera Output");
return 0;
}
fi
Object Oriented Programming

• What is OOP? Why not C?


C Programming Language

• C programming is structured and procedural;

• it focuses on steps and procedures the program must take to reach a


desired state.

• C is a powerful, high-performance language that forms the basis of


many other programming languages, including C++ and Objective-C,
which incorporate object-oriented concepts.
Object Oriented Programming Language
• Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications
and computer programs. It utilizes several principles such as encapsulation, inheritance, and polymorphism
to create more modular, reusable, and manageable code.

• Encapsulation: This involves bundling the data (attributes) and the methods (functions or operations)
that operate on the data into a single unit or class. This encapsulation provides a protective barrier that
prevents the code from being accessed directly by other parts of the program, except through the
methods de ned in the class.

• Inheritance: This allows a class to inherit attributes and methods from another class, referred to as its
parent class. This feature helps in reducing redundancy and increases reusability. For example, a 'Dog'
class could inherit from a more general 'Animal' class.

• Polymorphism: This allows methods to do di erent things based on the object it is acting upon. This
means a single function can work in di erent ways on di erent objects.

• Abstraction: This principle involves hiding complex realities while exposing only the necessary parts of
objects and features to the outside world. It helps in handling complexity by hiding unnecessary details
and showing only essential attributes of an object
fi
ff
ff
ff
Object Oriented Programming

• OOP in C is non-trivial

• We will use Python


Python

• Make sure that Python is installed on your system. You can download and
install Python from python.org

• To check if Python is installed, you can type python --version or python3 --


version in your command line
Hello Word in Python
• print("Hello, World!")
• Create the Program File:
• Open a text editor (like Notepad, Visual Studio Code, or any IDE that supports Python).
• Copy and paste the code above into the editor.
• Save the file with a .py extension, for example, hello_world.py.
• Open a command line interface (Terminal on macOS and Linux, Command Prompt or
PowerShell on Windows)

• Navigate to the directory where you saved your hello_world.py file.


• Run the program by typing python hello_world.py and press Enter.
Add two numbers in Python
Add two numbers in Python
# This program adds two numbers provided by the user

# Get numbers from the user


number1 = oat(input("Enter the rst number: "))
number2 = oat(input("Enter the second number: "))

# Add the numbers


sum = number1 + number2

# Display the result


print("The sum of", number1, "and", number2, "is", sum)
fl
fl
fi

You might also like