INTRODUCTION TO
C PROGRAMMING
WHAT IS C?
• C is a general-purpose programming language developed in the early
1970s by Dennis Ritchie at Bell Labs.
• It is widely used for system programming and developing operating
systems, compilers, and applications.
• C was designed to be efficient and flexible, allowing programmers to
work closely with the hardware.
• It serves as a foundation for many modern programming languages,
influencing languages like C++, Java, and Python.
CHARACTERISTICS OF C
• C is known for its speed and performance, making it suitable for high-performance
applications.
• C programs can be compiled on different types of machines with minimal changes.
• C has a vast standard library that provides numerous built-in functions for various
tasks.
• Allows manipulation of bits, bytes, and addresses, providing a strong control over
system resources.
• These characteristics make C a powerful language for developing system-level
software, as well as applications that require direct interaction with hardware.
WHY LEARN C?
• Understanding C helps in learning C++, Java, and many other
languages.
• C is extensively used for writing operating systems, drivers, and
embedded systems.
• Many algorithms and data structures are implemented in C, making it
a useful language for computer science students.
THE DEVELOPMENT ENVIRONMENT
• Text Editors and IDEs: Examples include Code::Blocks, Dev-C++,
Eclipse, and Visual Studio Code.
• https://code.visualstudio.com/download
• Compilers: GCC (GNU Compiler Collection) is widely used to compile C
programs.
• https://sourceforge.net/projects/mingw/files/latest/download
BASIC STRUCTURE OF A C PROGRAM
• Syntax Overview:
• #include <stdio.h>: Preprocessor directive to include the standard
input-output library.
• int main() { ... }: Main function where the execution starts.
• return 0;: Indicates successful termination of the program.
#include <stdio.h> // Preprocessor directive to include the
standard input/output library
int main() {
// The main function where the program execution starts
printf("Hello, World! ");
// printf is a function from stdio.h that outputs the text
"Hello, World!" to the console
// \n is used to insert a new line after the message
return 0; // Indicates that the program finished successfully
}
COMMENTS IN C PROGRAMMING
• Comments are non-executable lines in C programs, used to explain the code, improve
readability, and provide context for future reference. They are ignored by the compiler.
• Single-Line Comment
// This is a single-line comment
int a = 10; // Variable declaration
• Multi-Line Comment
Used for longer comments that span multiple lines.
/*
This is a
multi-line comment
*/
/*
The following code calculates
the sum of two numbers
*/
• int sum = a + b;
#include <stdio.h>
int main() {
// Declare variables
int num1 = 5; // First number
int num2 = 10; // Second number
/*
* Calculate the sum of num1 and num2
* and store it in the result variable
*/
int result = num1 + num2;
// Print the result
printf("The sum is: %d", result);
return 0; // Return statement
}
ESCAPE SEQUENCES
• Escape sequences are special character combinations that allow you to
represent certain characters in string literals that may not be easily
typed or printed. They begin with a backslash (\) followed by one or
more characters.
#include <stdio.h>
int main() {
printf("Hello, World!\n"); // Newline
printf("Tab\tSpace\n"); // Tab
printf("Quote: \"Hello!\"\n"); // Double quote
printf("Backslash: \\\n"); // Backslash
printf("Alert: \a\n"); // Alert (may produce a sound)
return 0;
}
C LANGUAGE - VARIABLES
• A storage location in memory with a specific name, used to store data
values.
C Language - Variable Naming Rules
Start with a letter or underscore (_):
A variable name must begin with an alphabetic character (a-z, A-
Z) or an underscore.
Example: int count; or float _rate;
• Can contain letters, digits, and underscores:
• After the first character, you can use letters, digits (0-9), and
underscores.
• Example: int age1; or double price_rate;
• No spaces allowed:
• Variable names cannot have spaces between characters.
• Example: int myVar; (valid) vs int my Var; (invalid)
• No special characters allowed:
• Only underscores (_) are allowed, no other special characters like @, #,
&, etc.
• Example: int total_sum; (valid) vs int total$sum; (invalid)
• Case-sensitive:
• C distinguishes between uppercase and lowercase letters, so myVar and
myvar are different.
• Example: int MyVar; vs int myvar;
• Cannot be a keyword:
• You cannot use reserved keywords (like int, for, if) as variable names.
• Example: int for; (invalid)
• Reasonable length:
• Variable names can be of any length, but it's recommended to keep them
short and meaningful (ideally less than 31 characters for portability).
• Example: float interestRate;
• Avoid starting with an underscore:
• Although technically allowed, starting with an underscore is typically
reserved for system and library variables/functions.
• int age;
• float salary;
• char grade;
DATA TYPES IN C PROGRAMMING
• int: Typically stores whole numbers.
• Size: Usually 4 bytes (32 bits).
• Example: int age = 25;
• Floating-Point Types:
• float: Stores single-precision floating-point numbers.
• Size: Usually 4 bytes (32 bits).
• Example: float price = 19.99;
--------------------------------------------------------------------------------------------------------------------
• double: Stores double-precision floating-point numbers (more precision).
• Size: Usually 8 bytes (64 bits).
• Example: double pi = 3.141592653589793;
• char (Character)
• Used to store a single character.
• Size: Typically 1 byte.
• char initial = 'A'; // Variable initial of type char
DERIVED DATA TYPES
• Array
• A collection of elements of the same data type.
• int numbers[5] = {1, 2, 3, 4, 5}; // Array of integers
• ----------------------------------------------
• Pointer
• A variable that stores the address of another variable.
• Int i = 10; // initialize an integer variable
• Int *p = &i; // Initialize a pointer to the variable “I”
FORMAT SPECIFIERS IN C:
• %d is a format specifier used in the printf function to indicate that the
corresponding argument is an integer
• %f: For floating-point numbers.
• %c: For single characters.
• %s: For strings (character arrays).
• %x: For hexadecimal representation of an integer.
#include <stdio.h>
int main() {
// Basic Data Types
int age = 30; // Integer variable
float height = 5.8; // Float variable
double salary = 45000.50; // Double variable
char initial = 'B'; // Char variable
// Output
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Salary: %.2f\n", salary);
printf("Initial: %c\n", initial);
return 0;
}