1. Introduction to C Language
C is a general-purpose, procedural programming language used to develop system software, application software, embedded systems, and operating systems. It was developed by Dennis Ritchie at Bell Laboratories in the early 1970s.
C is often called a middle-level language because it combines high-level programming features with low-level capabilities, such as pointers and direct memory manipulation.
History of C Language
The development of C was influenced by earlier programming languages.
ALGOL: Influenced the development of structured programming concepts.
BCPL: Developed by Martin Richards in 1967.
B Language: Developed by Ken Thompson around 1969 and influenced by BCPL.
C Language: Developed by Dennis Ritchie around 1972 at Bell Laboratories.
Unix: C became widely important when much of the Unix operating system was rewritten in C.
ANSI C: The language was standardized by ANSI in 1989, commonly known as C89.
Later standards, including C99, C11, C17, and C23, introduced further improvements.
2. Features of C Language
1. Simple Language
C has a relatively small set of keywords and a clear syntax, making it suitable for learning programming fundamentals.
2. Structured Programming
A program can be divided into functions and smaller logical parts, making it easier to understand, test, and maintain.
3. Portability
C programs can often be transferred to different computer systems with little modification, provided suitable compilers and compatible libraries are available.
4. Fast Execution
C generally produces efficient machine code through compilation, making it useful for performance-sensitive applications.
5. Rich Set of Operators
C provides arithmetic, relational, logical, bitwise, assignment, and other operators.
6. Pointers
Pointers store memory addresses and allow programmers to work directly with memory, arrays, and dynamically allocated data.
7. Rich Standard Library
C provides standard library functions for input/output, string handling, mathematical calculations, memory management, and other common tasks.
8. Extensibility
Programmers can create their own functions and combine them with standard library functions to build larger programs.
9. Case-Sensitive
C distinguishes between uppercase and lowercase letters. For example, total, Total, and TOTAL are different identifiers.
10. Middle-Level Language
C supports high-level programming constructs while also providing low-level capabilities for system programming.
3. Basic Structure of a C Program
A C program commonly contains header files, a main() function, declarations, executable statements, and a return statement.
Example: First C Program
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}Output:
Hello, World!Explanation of the Program
1. #include <stdio.h>
This is a preprocessor directive that makes the declarations of standard input/output functions, such as printf(), available to the program.
2. int main(void)
This defines the main() function, where execution of a hosted C program begins. The return type int means the function returns an integer status.
3. {
The opening brace marks the beginning of the function body.
4. printf("Hello, World!\n");
The printf() function displays formatted output. The text inside quotation marks is a string literal, and \n represents a newline.
5. return 0;
This indicates successful program termination to the host environment.
6. }
The closing brace marks the end of the function body.
Important: A semicolon terminates many C statements. It is not placed after every line; for example, #include <stdio.h> does not end with a semicolon.
4. Character Set in C
The C character set consists of letters, digits, whitespace characters, and special characters used to write source code.
A. Letters
Uppercase letters:
AtoZLowercase letters:
atoz
B. Digits
Decimal digits:
0to9
C. Whitespace Characters
Space
Horizontal tab
Newline
Other whitespace characters recognized by the language
Whitespace helps separate tokens and format source code.
D. Special Characters
Examples include:
+ - * / = ; , . ( ) { } [ ] # < > &
These characters have specific meanings in C syntax.
5. Tokens in C
Tokens are the smallest meaningful lexical units of a C program. The compiler recognizes these units when processing source code.
The main token categories are keywords, identifiers, constants, string literals, operators, and punctuators.
5.1 Keywords
Keywords are reserved words that have predefined meanings in C. They cannot be used as ordinary identifiers.
Examples:
int, char, if, else, return, while, for, void
Example:
int age = 20;Here, int is a keyword.
5.2 Identifiers
Identifiers are names given to program elements such as variables, functions, and arrays.
Examples: age, total, main, studentName
Rules for identifiers:
They may contain letters, digits, and underscores.
They cannot begin with a digit.
They cannot contain spaces.
They cannot be keywords.
They are case-sensitive.
Valid identifiers: age, total_marks, num1
Invalid identifiers: 1num, total marks, float
5.3 Constants
Constants are fixed values that do not change during the relevant expression or program execution.
Examples:
Integer constant:
25Floating-point constant:
3.14Character constant:
'A'
Example:
int age = 20;Here, 20 is an integer constant.
5.4 String Literals
A string literal is a sequence of characters enclosed in double quotation marks.
Examples:
"Hello"
"C Programming"
"Welcome to class"Example statement:
printf("Welcome!");The text "Welcome!" is a string literal.
5.5 Operators
Operators are symbols used to perform operations on operands.
Examples:
Arithmetic:
+,-,*,/,%Relational:
==,!=,<,>Logical:
&&,||,!Assignment:
=,+=,-=
Example:
int sum = 10 + 20;Here, = and + are operators.
5.6 Punctuators
Punctuators are symbols used to organize and define the structure of C programs.
Examples include:
;— terminates many statements,— separates items( )— groups expressions or encloses function parameters{ }— delimit blocks[ ]— used in array declarations and subscripting
Example:
int numbers[5];Here, the square brackets are punctuators.
6. Comments in C
Comments are explanatory notes written in source code. They help programmers understand the purpose and logic of a program. Comments are ignored as comments by the compiler and do not directly execute.
C supports two common forms of comments.
6.1 Single-Line Comment
A single-line comment begins with // and continues to the end of the line.
// This program displays a message
printf("Hello");6.2 Multi-Line Comment
A multi-line comment begins with /* and ends with */. It can span multiple lines.
/*
This program demonstrates
a simple output statement.
*/
printf("Hello");Advantages of Comments
Make code easier to understand.
Explain complex logic.
Improve program maintenance.
Help other programmers work with the code.
Note: Traditional C block comments cannot be nested inside one another.
7. Writing, Compiling, and Executing a C Program
A computer does not directly execute ordinary C source code. The source code must be translated into executable machine code by a suitable toolchain.
Step 1: Write the Program
Open a C editor or IDE, such as Visual Studio Code with a suitable compiler setup, Code::Blocks, or another C development environment.
Type the program:
#include <stdio.h>
int main(void)
{
printf("My first C program\n");
return 0;
}Save the file with a .c extension, for example, first.c.
Step 2: Compile the Program
The compiler translates the source code into machine code or an intermediate object file, depending on the toolchain. Compilation reports errors such as missing semicolons or undeclared identifiers.
For example, with GCC:
gcc first.c -o firstStep 3: Execute the Program
Run the generated executable.
On Linux or macOS:
./firstOn Windows, depending on the terminal and compiler:
first.exeStep 4: Observe the Output
My first C programCommon Types of Errors
Syntax error: Breaking the language's grammar, such as omitting a required semicolon.
Compile-time error: An error reported during compilation, such as using an undeclared variable.
Runtime error: A problem that occurs while the program is running.
Logical error: The program runs but produces an incorrect result because the logic is wrong.
8. Practical Examples
Example 1: Display Your Name
#include <stdio.h>
int main(void)
{
printf("My name is Ram.\n");
return 0;
}Output:
My name is Ram.Example 2: Display Multiple Lines
#include <stdio.h>
int main(void)
{
printf("Name: Ram\n");
printf("Course: BICTE\n");
printf("Subject: C Programming\n");
return 0;
}Output:
Name: Ram
Course: BICTE
Subject: C ProgrammingExample 3: Display a Calculation
#include <stdio.h>
int main(void)
{
printf("Sum = %d\n", 10 + 20);
return 0;
}Output:
Sum = 30In this example, %d is a format specifier used to display an integer value.
9. Summary
C is a procedural, general-purpose programming language developed by Dennis Ritchie around 1972.
Important C features include efficiency, portability, structured programming, and pointer support.
A basic program commonly includes a header, a
main()function, statements, and a return status.Tokens are the smallest meaningful lexical units of a C program.
Comments document the program and are not executed as code.
Writing, compiling, and executing are essential stages of running a C program.
0 Komentar