Define Computer language, Translator, Standard input out C
148 views
29 slides
Feb 01, 2025
Slide 1 of 29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
About This Presentation
computer language and translator
Size: 572.61 KB
Language: en
Added: Feb 01, 2025
Slides: 29 pages
Slide Content
Agri -Informatics Dr. Shailendra Bhalawe Assistant Professor College of Agriculture Balaghat Jawaharlal Nehru Krashi Vishwavidyalaya Jabalpur (M.P.)
Introduction to Programming Languages Language is a mode of communication that is used to share ideas, opinions with each other . A programming language is a computer language that is used by programmers (developers) to communicate with computers. A programming language is a set of instructions and syntax used to create software programs . A programming language is a set of symbols, grammars and rules with the help of which one is able to translate algorithms to programs that will be executed by the computer. The programmer communicates with a machine using programming languages. Most of the programs have a highly structured set of rules. It is a set of instructions written in any specific language ( C, C++, Java, Python) to perform a specific task.
Some of the key features of programming languages include: Syntax : The specific rules and structure used to write code in a programming language. Data Types : The type of values that can be stored in a program, such as numbers, strings, and Booleans. Variables : Named memory locations that can store values. Operators : Symbols used to perform operations on values, such as addition, subtraction, and comparison. Control Structures : Statements used to control the flow of a program, such as if-else statements, loops, and function calls. Libraries and Frameworks: Collections of pre-written code that can be used to perform common tasks and speed up development. Paradigms : The programming style or philosophy used in the language, such as procedural, object-oriented, or functional. Algorithm : A step-by-step procedure for solving a problem or performing a task.
Types of programming language 1. Low-level programming language Low-level language is machine-dependent (0s and 1s) programming language. The processor runs low- level programs directly without the need of a compiler or interpreter, so the programs written in low-level language can be run very fast. Low-level language is further divided into two parts - i . Machine Language Machine language is a type of low-level programming language. It is also called as machine code or object code . Machine language is easier to read because it is normally displayed in binary or hexadecimal form (base 16) form. It does not require a translator to convert the programs because computers directly understand the machine language programs. The advantage of machine language is that it helps the programmer to execute the programs faster than the high-level programming language. There is software which translate programs into machine level language. Examples include operating systems like Linux, UNIX, Windows, etc. In this language, there is no need of compilers and interpreters for conversion and hence the time consumption is less. However, it is not portable and non-readable to humans.
Assembly Language Machine Code SUB AX, BX 0010101110000011 MOV CX, AX 100010111001000 MOV DX, 0 10111010000000000000000 ii. Assembly Language: Assembly language (ASM) is also a type of low-level programming language that is designed for specific processors. It represents the set of instructions in a symbolic and human-understandable form . It uses an assembler to convert the assembly language to machine language. The advantage of assembly language is that it requires less memory and less execution time to execute a program. The computer should have assembler to translate assembly level program to machine level program. Examples include ADA, PASCAL, etc. It is in human-readable format and takes lesser time to write a program and debug it. However, it is a machine dependent language.
2. High-level programming language: High-level programming language (HLL) is designed for developing user-friendly software programs and websites . This programming language requires a compiler or interpreter to translate the program into machine language (execute the program). The main advantage of a high-level language is that it is easy to read, write, and maintain. High-level programming language includes Python, Java, JavaScript, PHP, C#, C++, Objective C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language. A high-level language is further divided into three parts - Procedural Oriented programming language. ii. Object-Oriented Programming language iii. Natural language
i . Procedural Oriented programming language: Procedural Oriented Programming (POP) language is derived from structured programming and based upon the procedure call concept. It divides a program into small procedures called routines or functions . Procedural Oriented programming language is used by a software programmer to create a program that can be accomplished by using a programming editor like IDE, Adobe Dreamweaver, or Microsoft Visual Studio. The advantage of POP language is that it helps programmers to easily track the program flow and code can be reused in different parts of the program. The advantage of POP language is that it helps programmers to easily track the program flow and code can be reused in different parts of the program. Example: C, FORTRAN, Basic, Pascal, etc . Time: 1940s-1950s
ii. Object-Oriented Programming language Object-Oriented Programming (OOP) language is based upon the objects . In this programming language, programs are divided into small parts called objects . It is used to implement real-world entities like inheritance, polymorphism, abstraction, etc in the program to makes the program resusable , efficient, and easy-to-use. The main advantage of object-oriented programming is that OOP is faster and easier to execute, maintain, modify, as well as debug. Note: Object-Oriented Programming language follows a bottom-up approach. Example: C++, Java, Python, C#, etc .
iii. Natural language Natural language is a part of human languages such as English, Russian, German, and Japanese. It is used by machines to understand, manipulate, and interpret human's language. It is used by developers to perform tasks such as translation, automatic summarization, Named Entity Recognition (NER), relationship extraction, and topic segmentation . The main advantage of natural language is that it helps users to ask questions in any subject and directly respond within seconds.
Translators or Language Processors A program written in a high-level language is known as source code that is not understandable by the machine. It must be translated into a machine-understandable form before running. A translator is a computer program that converts source code into machine code that the computers understand and process. Generally, there are three types of translators : 1.Assembler 2.Compiler 3.Interpreter
1. Compiler A compiler translates the entire source code of a high-level programming language into machine code before execution. The process involves several stages: lexical analysis, syntax analysis, semantic analysis, optimization, and code generation . Examples: GCC (GNU Compiler Collection): Compiles languages like C, C++, and Fortran. javac : The Java compiler that translates Java source code into bytecode . Clang: A compiler for the C language family (C, C++, Objective-C). Usage: Suitable for programs that need to be executed multiple times, as the translation happens once, and the machine code can be executed repeatedly. Examples: System software, application software, and games.
2. Interpreter An interpreter translates and executes code line by line . It does not produce an intermediate machine code file. Each line of code is read, translated to machine code, and executed immediately. Examples: Python Interpreter: Executes Python code. Ruby Interpreter: Executes Ruby code. JavaScript Engines (like V8 in Chrome): Executes JavaScript code. Usage: Suitable for scripting and scenarios where code is executed only once or needs to be tested frequently. Examples: Web scripting, automation scripts, prototyping.
3. Assembler An assembler translates assembly language code into machine code. Assembly language is a low-level programming language that uses mnemonics to represent machine instructions. The output of an assembler is a machine code file that can be executed by the computer’s CPU. Examples: NASM ( Netwide Assembler): Assembles code for x86 architecture. MASM (Microsoft Macro Assembler): Assembles code for Microsoft platforms. GAS (GNU Assembler): Part of the GNU Binutils package, assembles code for various architectures. Usage: Used for writing system software, device drivers, and real-time applications where performance and hardware control are critical. Examples: Operating systems, embedded systems, firmware.
Standard input and output in C : Introduction: Standard input and output in C refer to interacting with the user through the keyboard (input) and screen (output). Functions like scanf () and printf () are used to handle standard I/O operations . Input-output operations in C are essential for interacting with users and handling data. These operations allow a program to take input from the user or external files and display results. The input-output statements in C are part of the standard library, enabling programmers to perform tasks like reading data from the console, writing to the console, and managing files efficiently. Learning these functions is crucial for creating interactive and data-driven programs. So here, we’ll explore various input and output operations, their functions, and examples to help you use them effectively in your C programs .
Input in C: Input in C programming means gathering data from the user or external sources for processing. The most common way is through the console, where functions like scanf () are used for formatted input, and getchar () reads a single character. Additionally, data can be read from files using functions like fscanf () and fgets (). For example , scanf ("%d", &num); takes an integer input from the user. Output in C: Output in C programming means displaying results or information from a program on the console or writing it to a file. Console output functions like printf () allow formatted data to be printed, while putchar () is used for single characters. File output operations in C involve writing data to a file using functions like fprintf () and fputs (). For example , printf ("Result: %d", result); displays the value of a variable.
Function Category Description scanf() Input (Console) Reads formatted input (e.g., integers, floats, strings) from the user. getchar() Input (Console) Reads a single character from the user. gets() Input (Console) Reads a string from the user (deprecated due to safety issues). fscanf() Input (File) Reads formatted data from a file. fgets() Input (File) Reads a line of text from a file. printf() Output (Console) Prints formatted output to the console. putchar() Output (Console) Outputs a single character to the console. puts() Output (Console) Outputs a string to the console. fprintf() Output (File) Writes formatted data to a file. fputs() Output (File) Writes a string to a file. fopen() File Operation Opens a file for reading, writing, or appending. fclose() File Operation Closes an opened file. fread() Input (File) Reads binary data from a file. fwrite() Output (File) Writes binary data to a file. feof() File Operation Checks if the end of a file has been reached. perror() Output (Error) Prints an error message to the standard error stream Input and Output Functions in C Programming Below is the list of input and output functions in C language
Input Functions in C Below is a detailed explanation of common input functions in C programming: 1. scanf () Reads formatted input from the user, such as integers, floats, and strings. Example: int num; printf ("Enter a number: "); scanf ("%d", &num); // Takes integer input printf ("You entered: %d\n", num); 2. getchar () Reads a single character from the user. Example: char ch ; printf ("Enter a character: "); ch = getchar (); // Reads one character printf ("You entered: %c\n", ch ); 3. gets() Reads a string from the user, including spaces (deprecated due to safety concerns). Example: char str [50]; printf ("Enter a string: "); gets( str ); // Reads the entire string printf ("You entered: %s\n", str );
4. fscanf () Reads formatted data from a file. Example: FILE *file = fopen ("data.txt", "r"); int num; fscanf (file, "%d", &num); // Reads an integer from the file printf ("The number in the file is: %d\n", num); fclose (file); 5. fgets () Reads a line of text from a file or the console. Example: FILE *file = fopen ("data.txt", "r"); char line[100]; fgets (line, 100, file); // Reads a line from the file printf ("The line in the file is: %s\n", line); fclose (file);
Output Functions in C Below is the explanation of common output functions in C programming: 1. printf () Prints formatted output to the console. Example: int num = 10; printf ("The number is: %d\n", num); // Prints the value of num 2. putchar () Outputs a single character to the console. Example: char ch = 'A'; putchar ( ch ); // Outputs the character 'A' printf ("\n"); 3. puts() Outputs a string to the console with a newline at the end. Example: char str [] = "Hello, World!"; puts( str ); // Prints the string with a newline
4. fprintf () Writes formatted data to a file. Example: FILE *file = fopen ("output.txt", "w"); int num = 20; fprintf (file, "The number is: %d\n", num); // Writes to the file fclose (file); 5. fputs () Writes a string to a file. Example: FILE *file = fopen ("output.txt", "w"); fputs ("This is a test string.", file); // Writes the string to the file fclose (file);
What is File Input/Output in C? File Input/Output in C allows programs to interact with files for reading and writing data. Using file handling functions like fopen (), fclose (), fscanf (), and fprintf (), you can store data persistently or retrieve it for processing. File input-output in C language is crucial for managing large datasets and ensuring data can be saved beyond program execution. File Input Output in C (Functions to Handle Files) 1. fopen () Opens a file for reading, writing, or appending. FILE *file = fopen ("example.txt", "w"); // Opens a file for writing if (file == NULL) { printf ("Error opening file.\n"); } else { printf ("File opened successfully.\n"); fclose (file); // Close the file }
2. fclose () Closes an opened file to free resources. FILE *file = fopen ("example.txt", "r"); if (file != NULL) { fclose (file); // Closes the file } 3. fprintf () Writes formatted data to a file. FILE *file = fopen ("example.txt", "w"); fprintf (file, "Hello, File I/O in C!\n"); // Write data fclose (file); 4. fscanf () Reads formatted data from a file. FILE *file = fopen ("example.txt", "r"); char str [50]; fscanf (file, "%s", str ); // Read data printf ("File content: %s\n", str ); fclose (file);
5. fgets () and fputs () Reads and writes strings. FILE *file = fopen ("example.txt", "w"); fputs ("This is a test string.", file); // Write string fclose (file); file = fopen ("example.txt", "r"); char line[100]; fgets (line, 100, file); // Read string printf ("File content: %s\n", line); fclose (file);
Input - scanf() - fscanf() Output - printf () - fprintf () Formatted and Unformatted Input/Output in C Formatted and Unformatted Input/Output in C programming are two categories of I/O operations. They differ in how data is read or written and the level of control over the formatting. 1. Formatted Input/Output in C Formatted Input/Output allows you to control how data is read or displayed using format specifiers . You can format numbers, strings, or characters with specific rules, making it versatile for structured data. Functions Used:
Input - getchar () - gets() - fgets () Output - putchar () - puts() - fputs () 2. Unformatted Input/Output in C Unformatted Input/Output deals with raw data without specific formatting. It is suitable for reading or writing single characters or strings directly. Functions Used:
Category Formatted I/O Unformatted I/O Functions scanf (), printf (), fscanf (), fprintf () getchar(), putchar(), gets(), puts() Data Handling Controlled by format specifiers. Raw data without specific formatting. Examples Taking numbers and strings with format rules. Reading characters or raw strings. Use Case Structured and precise input/output. Simple or quick character/string I/O Difference Between Formatted and Unformatted Input Output in C