C Programming Chapter-1 Notes
C Programming Chapter-1 Notes
COURSE OUTCOMES(CO’S)
After the completion of the course, the student will be able to:
CO1-Understand the foundational concepts of the C language, including its history and basic
building blocks.
CO2-Utilize conditional and unconditional control flow statements to write efficient programs.
Syllabus Content:
Features of C Language
Simple
Machine Independent or Portable
Mid-level programming language
Structured programming language
Rich Library
Memory Management
Fast Speed
Pointers
Recursion
Extensible
1) Simple:
Provides a structured approach to break the problem into parts
the rich set of library functions, data types, etc.
2) Rich Library:
Provides a lot of inbuilt functions that make the development
fast.
3) Memory Management:
It supports the feature of dynamic memory allocation.
We can free the allocated memory at any time by calling the
free() function in C.
4) Speed
The compilation and execution time of C language is fast
5) Pointer
Can directly interact with the memory by using the pointers.
Can use pointers for memory, structures, functions, array, etc.
6) Extendability
Ability to extend the existing software by adding some new
features.
7) Recursion:
o A function that calls itself is known as a recursive function. And,
this technique is known as recursion.
Application of C
Operating System
Embedded Systems
GUI
Game Development
Drivers
Database Design
Section Description
Consists of the description of the program, programmer's name, and creation date. These
Documentation
are generally written in the form of comments.
All header files are included in this section which contains different functions from the
Link
libraries. A copy of these header files is inserted into your code before compilation.
Includes preprocessor directive, which contains symbolic constants. E.g.: #define allows
Definition
us to use constants in our code. It replaces all the constants with its value in the code.
Global Includes declaration of global variables, function declarations, static global variables, and
Declaration functions.
Main() For every C program, the execution starts from the main() function. It is mandatory to
Function include a main() function in every C program.
Includes all user-defined functions (functions the user provides). They can contain the
Subprograms inbuilt functions and the function definitions declared in the Global Declaration section.
These are called in the main() function.
Example:
Here, we are using int, char and auto keywords. We can simply use
auto keyword to deduce the data type of any variable.
In this example, instead of using int and character array, we can
just use auto keyword which will automatically identify the data
type for storage purposes.
int num = 10; //int keyword
char univ[10] = “REVA"; //char keyword
These two lines can be modified as: (without knowing the data-
type)
auto num = 10; //auto keyword is used to deduce the data type of a
variable
auto univ= “REVA";
Constant Example
Integer constant 10, 20, 30 etc.
Example:
#include <stdio.h>
int main() {
const int NUM = 10;
const float PI = 3.14159;
const char LETTER = 'A';
const char *MESSAGE = "Hello, world!";
printf("Integer constant: %d\n", NUM);
printf("Floating-point constant: %f\n", PI);
printf("Character constant: %c\n", LETTER);
printf("String constant: %s\n", MESSAGE);
return 0;
}
Types Description
Arithmetic Operators These operators are used to perform mathematical operations such as addition,
subtraction, multiplication, division, and modulus. The arithmetic operators in C
are + (addition), - (subtraction), * (multiplication), / (division), and % (modulus).
Relational Operators These operators are used to compare two values and return a boolean value (true or
false) depending on the result of the comparison. The relational operators in C are
== (equal to), != (not equal to), < (less than), > (greater than), <= (less than or
equal to), and >= (greater than or equal to).
Logical Operators These operators are used to perform logical operations such as AND, OR, and
NOT. The logical operators in C are && (AND), || (OR), and ! (NOT).
Assignment Operators These operators are used to assign values to variables. The assignment operators in
C are = (simple assignment), += (add and assign), -= (subtract and assign), *=
(multiply and assign), /= (divide and assign), and %= (modulus and assign).
Bitwise Operators These operators are used to perform bit-level operations such as bitwise AND,
bitwise OR, and bitwise NOT. The bitwise operators in C are & (bitwise AND), |
(bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), and >> (right
shift).
Conditional Operator This is a ternary operator that is used to evaluate a condition and return one of two
values based on the result of the evaluation. The conditional operator in C is ? :
(ternary conditional operator).
Example:
#include <stdio.h>
int main() {
int a = 5, b = 2;
float c = 7.5, d = 2.0;
// Arithmetic operators
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("c + d = %f\n", c + d);
printf("c - d = %f\n", c - d);
printf("c * d = %f\n", c * d);
printf("c / d = %f\n", c / d);
// Increment and decrement operators
printf("a++ = %d\n", a++);
printf("a = %d\n", a);
printf("++b = %d\n", ++b);
printf("b = %d\n", b);
// Relational operators
printf("a == b : %d\n", a == b);
printf("a != b : %d\n", a != b);
printf("c > d : %d\n", c > d);
printf("c < d : %d\n", c < d);
printf("a >= b : %d\n", a >= b);
printf("a <= b : %d\n", a <= b);
// Logical operators
// Bitwise operators
printf("a & b : %d\n", a & b);
printf("a | b : %d\n", a | b);
printf("a ^ b : %d\n", a ^ b);
printf("~a : %d\n", ~a);
printf("a << 1 : %d\n", a << 1);
printf("b >> 1 : %d\n", b >> 1);
return 0;
}
#include <stdio.h>
int main()
{
printf("Hello\nworld!\n"); // Output: Hello
// world!
printf("First\tSecond\tThird\n"); // Output: First Second Third
printf("Backslash: \\ \n"); // Output: Backslash: \
printf("Double quote: \" \n"); // Output: Double quote: "
printf("Single quote: \' \n"); // Output: Single quote: '
printf("Alert: \a \n"); // Produces an alert sound
printf("Backspace: ab\bcd\n"); // Output: acd
printf("Carriage return: 123\rXYZ\n"); // Output: XYZ23
printf("Form feed: 123\fXYZ\n"); // Output: 123
// XYZ
printf("Vertical tab: 123\vXYZ\n"); // Output: 123
// XYZ
return 0;
}
1.4 DATATYPES IN C
Data types in C programming language refer to the type of data that
can be stored in a variable. In C, each variable must have a specified
data type, which determines the size of the memory required to store
the data and the operations that can be performed on that data.
The need for data types in C arises from the fact that C is a strongly-
typed language, which means that the type of data stored in a variable
must be declared before it can be used. This allows the compiler to
perform type checking and ensure that operations are performed only
on data types that are compatible with each other.
Using data types in C ensures that the program works as intended,
with the correct data being used in each operation. It also helps in
reducing the memory usage of a program, as the compiler knows the
size of each variable and can allocate memory accordingly.
Note : The use of data types enables the programmer to write more
efficient and readable code by using the appropriate data type for each
variable or constant.
In C programming language, there are several data types available,
which can be categorized as follows:
int: integer type, used to pointers: variables that hold the memory
store integer values. addresses of other variables.
enum: user-defined data type used
float: floating-point type, structures: user-defined data types that to assign names to integral
used to store floating-point combine a group of variables of different constants, making the code more
values with a single data types under a single name. readable.
precision.
1.5 VARIABLES IN C
• Variables are a fundamental concept in programming, including
in C language.
• Variables are used to store the values that are used by the
program to perform various tasks.
• In C, variables are declared with a name, a data type, and an
optional initial value.
• In C programming language, a variable is a named location in
memory that stores a value of a certain data type.
Declaring a variable in C:
To declare a variable in C, we need to specify its name and its data
type.
Syntax datatype variable_name;
Initializing a Variable
We can also initialize a variable when you declare it, by assigning a
value to it.
Syntax datatype variable_name = value;
Example:
1.6 SIMPLE I/O (INPUT/OUTPUT) FUNCTIONS IN C
• Input and Output statement are used to read and write the data in
C programming.
• These are embedded in stdio.h (standard Input/Output header
file).
• Input means to provide the program with some data to be used
in the program
• Output means to display data on screen or write the data to a
printer or a file.
stdin: This file is used to receive the input (usually is keyboard file,
but can also take input from the disk file).
stdout: This file is used to send or direct the output (usually is a
monitor file, but can also send the output to a disk file or any other
device).
stderr: This file is used to display or store error messages.
Unformatted Input/Output functions in C
They allow us to read and write raw data without any specific
formatting.
Here are a few examples:
Unformatted Input Functions:
1. getchar(): Reads a single character from the standard input.
2. gets(): Reads a string of characters from the standard input.
Unformatted Output Functions:
putchar(): Writes a single character to the standard output.
puts(): Writes a string of characters to the standard output.
#include <stdio.h>
int main() {
// Unformatted Input
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: ");
putchar(ch);
// Unformatted Output
char str[] = "Hello, World!";
puts(str);
return 0;
}
In this example, the getchar() function is used to read a single
character from the user, and putchar() is used to display the entered
character. The puts() function is used to write the string "Hello,
World!" to the standard output.
Formatted input/output functions in C
They allow us to read and write data with specific formatting.
Here are some commonly used formatted I/O functions:
Formatted Input Functions:
scanf(): Reads input from the standard input based on the specified
format.
fscanf(): Reads input from a file based on the specified format.
sscanf(): Reads input from a string based on the specified format.
Formatted Output Functions:
printf(): Writes output to the standard output based on the specified
format.
fprintf(): Writes output to a file based on the specified format.
sprintf(): Writes output to a string based on the specified format.
Format specifiers in c
%d int
%u unsigned int
%f float
%lf double
%c char
%s char* (string)
%p Pointer
#include <stdio.h>
int main() {
// Formatted Input
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Sum: %d\n", num1 + num2);
// Formatted Output
int value = 42;
printf("The value is: %d\n", value);
return 0;
}
In this example, the scanf() function is used to read two integers from
the user. The format specifier %d is used to indicate that integers are
expected. The values are stored in the variables num1 and num2, and
their sum is printed using printf().