Docsity

Features of C language, Study notes of C programming

Partial preview of the text

Download Features of C language and more Study notes C programming in PDF only on Docsity!

Programming in C

Syllabus of Programming in C Unit I

Basic ConceptsBit - Smallest Unit of Memory is called Bit.Byte- Collection of 8 bit is called Byte.Instruction- Command given by the user.  Program- set of instruction.  Software- Collection of Program. Introduction C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in

1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used programming languages. Why Learn C?  C is considered mother of all programming languages as many later languages like Java, PHP and JavaScript have borrowed syntax/features directly or indirectly from the C.  If a person learns C programming first, it helps to learn any modern programming language as it provide a deeper understanding of the fundamentals of programming and underlying architecture of the operating system like pointers, working with memory locations etc.  C is widely used in operating systems , embedded systems, compilers, databases, networking, game engines, and real-time systems for its efficiency to work in low resource environment and hardware-level support. Writing First Program in C Language

Comment - Line 4[// This prints "Hello World"] The comments are used to add notes in your program that are ignored by the compiler and are not the part of executable program. Statement - Line 4 [printf("Hello World");] In C, a statement is always terminated by a semicolon (;) Return Statement - Line 5 [return 0;] The return statement refers to the return values from a function. This return statement and return value depend upon the return type of the function. The return statement in our program returns the value from main(). The returned value may be used by an operating system to know the termination status of your program. The value 0 typically means successful termination. Application of C Language  C is used to develop core components of operating systems such as Windows, Linux, and macOS.  C is applied to program embedded systems in small devices such as washing machines, microwave ovens, and printers.  C is utilized to create efficient and quick game engines.  C is employed to construct compilers , assemblers , and interpreters.  C is applied to develop efficient database engines.  C is employed to create programs for devices and sensors of Internet of Things (IoT). Example - a house automation system comprising temperature sensors and controllers that is often prepared with C.  C is employed for creating lightweight and speedy desktop applications. The widely used text editor Notepad++ employs C for performance-sensitive sections.

Steps in Learning C Building a C

most of the compilers, JVM, Kernels, etc. are written in C language.

2. C is a system programming language: It is used to create system software. It is a system programming language because it can be used to do low level programming. It can be used to create hardware devices, Os, drivers, kernel, etc. It can't be used for internet programming like Java, .net, PHP, etc. 3.C as a procedural language : A procedure is known as a function, method, routine, subroutine, etc. A procedural language specifies a series of steps for the program to solve the problem. A procedural language breaks the program into functions, data structure, etc. In C, variable and function must be declared before being used. 4.C is a structured programming language: Structure means to break a program into parts or blocks so that it may be easy to understand. In C, we break the program into parts using functions. It makes the program easier to understand and modify. 5.Recursion: Recursion means that we can create a function that can call itself multiple times until a given condition is true, just like loops. It provides code reusability and backtracking functionality. 6.Extensibility: we can easily (and quickly) extend a C program. If a code is already written, we can add new features with a few alterations. It allows new features, functionalities, and operations to be added to an existing C program. 7.Portability: C programs are machine-independent, which means that we can run a fraction of code created in C on various machines with none or some machine-specific changes. Hence, it provides the functionality of using a single code on multiple systems depending on the requirement. 8.Dynamic Memory Management: It means we can utilize and manage the data structure size data structure in C during runtime. C also provides several predefined functions to work with memory allocation. For instance, we can use the free() function to free up the allocated memory

at any time. Similarly, there are other functions, such as malloc(), malloc (), and realloc(), to perform operations on data structure and memory allocations. 9.Case Sensitive: C is a case sensitive language, meaning that identifiers like Variable 'a' and Variable 'A' are treated as different entities.

10. Rich Libraries: C supports various inbuilt functions and libraries that create development fast. We just have to use them, without worrying about how they work. These libraries are called header files in c language. 11. Mid-Level Language: C combines elements of both high level and low level language. High-level language are like giving instructions in plain English, we tell the computer what to do (e.g. Add two numbers), Low-level language are like giving instructions directly to computer hardware. Variables It is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times. It is a way to represent memory location through symbol so that it can be easily identified. Syntax: type variable_list; Ex- int a; float b; char c; Rules for defining variables  A variable can have alphabets, digits and underscore.  A variable name can start with the alphabet and underscore only. It can't start with a digit.  No whitespace is allowed within the variable name.  A variable name must not be any reserved word or keyword,e.g. int, float,etc.

5. Cost

6. Reliability

7. Simplicity

What is a Programming Language?

A programming language is a way for people to give instructions to a computer. It uses special words and rules to create programs that tell the computer what to do. These instructions can make the computer perform tasks like solving problems, playing games, or running apps. Examples of programming languages are Python, Java, and C++. There are Two Types of Programming languages:  High-Level Language  Low-Level Language High-Level Language A high-level language (HLL) is a human-readable programming language that simplifies coding by hiding complex hardware details, letting developers focus on logic and functionality.  Designed to make writing code simpler and faster.  Allow developers to build large programs more easily.  Easier to find and fix mistakes.  Can work on different computers with minimal adjustments.  Usually slower in performance compared to machine- oriented languages.  Provide many ready-made features to speed up coding.  Good for beginners and widely used for everyday software.  Examples include JavaScript, Ruby, Swift, and PHP. Low-Level Language

A low-level language is a machine-oriented programming language that provides minimal abstraction from hardware, offering direct control over memory and system resources for maximum performance and efficiency.  Provide direct access to the computer’s hardware.  Require detailed knowledge of how a computer works.  Less user-friendly, making programming more challenging.  Harder to find and solve errors in code.  Not easily adapted for use on different hardware.  Used mostly for specific tasks needing high performance or precise control.  Often faster and use fewer system resources.  Examples include Binary code and Assembly languages like MIPS or ARM.

High-Level Language Vs Low-Level Language

Parameters High-Level Language Low-Level Language Abstraction Level High abstraction, closer to human language Low abstraction, closer to machine code Ease of Use Easier to learn and use More complex and harder to learn Portability Highly portable across different systems Less portable, often system-specific Development Faster development Slower development

code is error free then it generates the object code

or machine code.

It has four steps i.e.

1. Pre-processor

2. Compiler

3. Assembler

4. Linker

Source Code Executable code Fig: Compiling a program

include int main() { Printf(“Hello”); getch(); } 010101010100101010010101010 010101010010101010100101010 101010101101010101010100101 010100101011011101101001010 101011010101110100101010010 101010100101010101010001011 000110101010101001010011000 Pre- Processor Compiler Assembler Other Object^ Linker files Librari es Object Code Assembly Code Expanded Code

What is assembly Language?

Assembly language is a programming language that's very close to the computer's native machine code but uses easy-to-remember words (mnemonics) instead of 0s and 1s, allowing humans to give direct instructions to the computer. It acts as a bridge between human-readable code and the binary instructions that a CPU can execute, though it's specific to a computer's architecture and requires an assembler program to convert the mnemonic code into machine-readable instructions. Here's a breakdown of what that means:  Low-Level: Assembly is a low-level language, meaning it gives instructions that are very close to how the computer's hardware (like the CPU) works.  Human-Readable (Relative): Computers understand machine code, which is just binary numbers (0s and 1s). Assembly uses short, memorable codes (mnemonics) for operations, like ADD for addition or SUB for subtraction.  One-to-One (Almost): Each assembly instruction usually translates directly into one machine code instruction. This gives programmers fine- grained control but makes coding complex.  Assembler: Since a computer can't directly understand assembly language, a special program called an assembler converts the assembly code into the binary machine code that the CPU can execute.  Machine-Dependent: Assembly language is tied to a specific computer's architecture, so code written for one type of computer won't work on another.

What is Machine Language?

Machine language is the low level programming language. Machine language can only be represented by 0s and 1s. For example: To write 120 in the

Machine Language Assembly Language Machine language is hardware dependent. Assembly language is the machine dependent and it is not portable.

Evolution of Major Programming Language

The following are the language before C and various version of C language.

  1. CPL (Common Programming Language) -The CPL was invented by Martin Richards at the University of Cambridge in the year 1960s 2. BCPL (Basic Combined Programming Language)- The BCPL was invented by Martin Richards at the University of Cambridge in year 1966. BCPL allows the user direct access to the computer memory. It is an extension of CPL. 3. B Language- It is derived from BCPL. It was introduced in the year 1969 by Ken Thompson & Dennis Ritchie at Bell Lab, USA. The B language is similar to BCPL. 4. C Language- It is derived from the B Language. Introduced in the year 1972 by Dennis Ritchie at Bell Lab, USA. The Language was mainly developed to create an OS called UNIX. 90% of the UNIX OS code is written in C. 5. ANSI C (C89)- In 1983, the ANSI (American National Standard Institute) formed a committee to frame standard specifications for the C Language. 6. C 90- In year 1990, the ANSI C was got ISO (International Organization for Standardization) with the new features like new library files, new processor commands. 7.C99- In 1995, many new features were added to the C90 to create a new version of it. This new version of C got ISO standardization in the year 1999 with the name C99.

Syntax: In the C programming language, syntax refers to the set of rules

that dictate how the language's elements (keywords, identifiers, operators, punctuation, etc.) must be arranged to form valid and meaningful statements, expressions, and program structures. It defines the grammar of the language, much like grammar in spoken languages governs sentence construction.

Semantics : In programming, semantics refers to the meaning and

interpretation of code, detailing what a specific statement or program actually does or what its intended behaviour is. For example , if price = 12.99 is syntactically correct, its semantics explain that it means "assign the value 12.99 to the variable price".

Pseudocode : Pseudocode is a simple way to write code that is not meant

to be run on a computer. It uses basic English words & phrases to describe what a program should do. Pseudocode helps programmers plan out their code before

writing it in a specific programming language like C.

What is the need for Pseudocode?

  1. It helps programmers plan out their code before writing it in a specific language. This can save time & prevent errors.
  2. It is easy to read & understand, even for people who don't know how to code. This makes it good for explaining ideas to others.
  3. It is not tied to any specific programming language, so it can be used as a starting point for coding in any language.
  4. It helps break down complex problems into smaller, easier to manage steps.
  5. It can be used to create a basic outline or structure for a program before filling in the details with actual code. Example START DECLARE num1, num2, num3, average as float INPUT num1, num2, num SET average = (num1 + num2 + num3) / 3 PRINT average END Here's how this pseudocode would translate into actual C code:  C #include int main() { float num1, num2, num3, average; printf("Enter three numbers: "); scanf("%f %f %f", &num1, &num2, &num3); average = (num1 + num2 + num3) / 3; printf("The average is: %.2f", average); return 0;

Clear and Unambiguous : The algorithm should be unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning.  Well-Defined Inputs : If an algorithm says to take inputs, it should be well- defined inputs. It may or may not take input.  Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-defined as well. It should produce at least 1 output.  Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.  Feasible: The algorithm must be simple and practical, such that it can be executed with the available resources. It must not contain some future technology or anything.  Language Independent: The Algorithm designed must be language- independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be the same, as expected.  Input : An algorithm has zero or more inputs. Each that contains a fundamental operator must accept zero or more inputs.  Output : An algorithm produces at least one output. Every instruction that contains a fundamental operator must accept zero or more inputs.  Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy to interpret. By referring to any of the instructions in an algorithm one can clearly understand what is to be done. Every fundamental operator in instruction must be defined without any ambiguity.  Finiteness: An algorithm must terminate after a finite number of steps in all test cases. Every instruction which contains a fundamental operator must be terminated within a finite amount of time.

Effectiveness: An algorithm must be developed by using very basic, simple, and feasible operations so that one can trace it out by using just paper and pencil. Advantages of Algorithms:  It is easy to understand.  An algorithm is a step-wise representation of a solution to a given problem.  In an Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program. Disadvantages of Algorithms:  Writing an algorithm takes a long time so it is time-consuming.  Understanding complex logic through algorithms can be very difficult.  Branching and Looping statements are difficult to show in Algorithms. Example: Algorithm 1: Add two numbers entered by the user Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num Step 5: Display sum Step 6: Stop Algorithm 2: Find the largest number among three numbers Step 1: Start Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c. Step 4: If a > b If a > c Display a is the largest number. Else Display c is the largest number. Else If b > c Display b is the largest number.