Download C language basic concepts and more Study notes Programming Languages in PDF only on Docsity!
Programming in C
Syllabus of Programming in C
Unit I Basic Concepts
- Bit - 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 program
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. Examples: int a; Int _ab; int 2; Int a30; int a b; These are correct These are incorrect
Programming Rules:
• Every Program should have main () function because it is a starting
point of function.
• C statement should be terminated by semicolon (;)
• All statement should be written in lowercase letters.
• Blank spaces may be inserted between words.
• The opening and closing braces should be equal.
Programming Domains
1. Scientific Application
2. Business Application
3. Artificial Intelligence
4. Govt. Application
5. System Programming
6. Scripting Language
7. Military
8. Health case
- 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
Parameters High-Level Language Low-Level Language Development Speed Faster development time Slower development time Examples Python, Java, C++, JavaScript Assembly language, Machine code Memory Management Automatic memory management Manual memory management Error Handling Built-in error handling features Limited error handling, requires manual checks Performance Generally slower execution Generally faster execution Use Cases Application development, scripting, web development System programming, embedded systems, device drivers
What is Compilation?
The Compilation is a process of converting the source code into
object code. It is done with the help of compiler.
The compiler checks the source code for the syntactical or
structural errors, and if the source code is error free then it
generates the object code or machine 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 computer system its representation is 1111000. So it is very difficult to learn. To overcome this problem, the assembly language is invented. Assembly languages use numbers, symbols, and abbreviations instead of 0s and 1s. For example: For addition, subtraction and multiplications it uses symbols likes Add, sub and Mul, etc.
Below is a table of differences between Machine Language and Assembly Language: Machine Language Assembly Language Machine language is only understood by the computers. Assembly language is only understood by human beings not by the computers. In machine language data only represented with the help of binary forma t(0s and 1s), hexadecimal and octal, decimal. In assembly language data can be represented with the help of mnemonics such as Mov, Add, Sub, End etc. Machine language is very difficult to understand by the human beings. Assembly language is easy to understand by the human being as compare to machine language. Modifications and error fixing cannot be done in machine language. Modifications and error fixing can be done in assembly language. Machine language is very difficult to memorize so it is not possible to learn the machine language. Easy to memorize the assembly language because some alphabets and mnemonics are used. Execution is fast in machine language because all data is already present in binary format. Execution is slow as compared to machine language. There is no need of translator. The machine understandable form is the machine language. Assembler is used as translator to convert mnemonics into machine understandable form. Machine language is hardware dependent. Assembly language is the machine dependent and it is not portable.
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?
- It helps programmers plan out their code before writing it in a specific language. This can save time & prevent errors.
- 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.
- It is not tied to any specific programming language, so it can be used as a starting point for coding in any language.
- It helps break down complex problems into smaller, easier to manage steps.
- 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; } Finding the largest number in a list SET largest = list[0] FOR each item in list IF item > largest THEN SET largest = item ENDIF NEXT PRINT largest
Checking if a number is prime SET isPrime = true FOR i = 2 TO number/ IF number MOD i == 0 THEN SET isPrime = false EXIT loop ENDIF NEXT IF isPrime THEN PRINT number, " is prime" ELSE PRINT number, " is not prime" ENDIF
Algorithm : An algorithm in C programming refers to the implementation of a
step-by-step procedure to solve a specific problem or perform a task using the C programming language.
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. Else Display c is the greatest number. Step 5: Stop
Flowchart : Diagrammatic representation of an algorithm is
called flowchart. Flowcharts include inputs , outputs , sequence
of actions, decision points , sequence of actions, and process
measurements. Symbols used in the flowchart are mentioned
below:
Name Symbol Shape Purpose
Terminal Oval
start/stop/begin/en
d
Input/outpu
t
Parallelogra
m
Input/output of
data
Process Rectangle
To represent a
process
Decision
box
Diamond
Decision operation
that determines
which of the
alternative paths to
be followed
Connector Circle
Used to connect
different parts of
the flowchart
Flow Arrows
Join 2 symbols and
also represent flow
of execution