3.1 Introduction to C Programming
C is a general-purpose, procedural, and middle-level programming language used for developing computer software, system programming, applications, games, and more. Known for its simplicity and efficiency, C is an excellent choice for beginners as it provides a strong foundation in programming concepts. C was developed by Dennis M. Ritchie at Bell Laboratories in 1972.
1. C Tokens
C Tokens are the building blocks of a C program. They are categorized into:
Keywords: Reserved words (e.g.,
int
,if
,while
,return
).Identifiers: Names for variables, functions, and arrays (e.g.,
main
,num
).Constants: Fixed values used in a program (e.g.,
10
,'A'
).Strings: Sequence of characters enclosed in double quotes (e.g.,
"Hello"
).Operators: Symbols that perform operations (e.g.,
+
,-
,*
,/
).Special Characters: Punctuation marks with specific functions (e.g.,
{
,}
,;
).
2. Operators
Arithmetic Operators:
+
,-
,*
,/
,%
.Relational Operators:
==
,!=
,<
,>
,<=
,>=
.Logical Operators:
&&
,||
,!
.Bitwise Operators:
&
,|
,^
,~
,<<
,>>
.Assignment Operators:
=
,+=
,-=
,*=
,/=
,%=
.Increment/Decrement:
++
,--
.
3. Input and Output (Formatted and Unformatted)
Formatted I/O:
Input:
scanf()
(e.g.,scanf("%d", &num);
)Output:
printf()
(e.g.,printf("Value: %d", num);
)
Unformatted I/O:
Input:
getchar()
,gets()
(e.g.,char c = getchar();
)Output:
putchar()
,puts()
(e.g.,puts("Hello");
)
4. Control Statements
Decision-Making:
if
,if-else
,else if
,switch
.Example:
Looping:
For Loop: Executes a block of code a fixed number of times.
While Loop: Executes as long as a condition is true.
Do-While Loop: Executes at least once, then continues based on a condition.
5. Functions
User-Defined Functions:
Functions created by the user for modularity and reusability.
Syntax:
Example:
Recursive Functions:
Functions that call themselves, typically used for tasks like calculating factorials or solving Fibonacci series.
Example:
6. Arrays
1D Arrays:
Syntax:
Example:
2D Arrays:
Syntax:
Example:
Multi-Dimensional Arrays:
Arrays with more than two dimensions (rarely used).
7. String Manipulations
String Functions (from
<string.h>
):strlen()
: Finds the length of a string.strcpy()
: Copies one string to another.strcat()
: Concatenates two strings.strcmp()
: Compares two strings.Example:
Conclusion
C is a versatile and efficient language that lays the groundwork for programming concepts. Its features, such as tokens, operators, control statements, arrays, functions, and string manipulations, empower developers to write structured and robust code. C’s simplicity and modularity make it an ideal choice for beginners and a powerful tool for developing a wide range of applications, from system software to games.
Last updated