Module 1 Second
Module 1 Second
Art of
Programming
Methodology
ATK
Introduction to the C Language
ATK
Some Facts About C Programming Language
ATK
Uses of C Programming Language
Database Systems
Language Interpreters
Compilers and Assemblers
Operating Systems
Network Drivers
Word Processors
ATK
C Has Become Very Popular for Various Reasons
ATK
Features of C Programming Language
ATK
Advantages of C
ATK
Disadvantages of C
ATK
The limitations of C programming languages are as follows:
o Difficult to debug.
o C allows a lot of freedom in writing code, and that is why you can put an empty line or
white space anywhere in the program. And because there is no fixed place to start or
end the line, so it is difficult to read and understand the program.
o C compilers can only identify errors and are incapable of handling exceptions (run-
time errors).
o C provides no data protection.
o It also doesn't feature reusability of source code extensively.
o It does not provide strict data type checking
ATK
(for example an integer value can be passed for floating data type).
Structure of C Program
Documentation Section 1
Pre-processor/Link section 2
Definition section 3
Main function 5
ATK Subprogram 6
1. Documentation Section
The documentation section is a part of the program, where the programmer writes everything about the program.
Typically, the programmer writes the name of the program, about the author of the program and other details such as
the date, and the program description.
In short, it gives the reader an overview of the program.
#include<stdio.h>
#include<conio.h>
We use #include to declare header files in the link section.
ATK
3. Definition Section
This section of the program is used to declare the symbolic constant that will be used in the program. A
symbolic constant is a constant value given to a name that can't be changed in the program.
NOTE: Do not put a semicolon (;) at the end of the #define statements.
ATK
4.Global Declaration section
The Global Declaration section is a part of the program, where the programmer declares
variables that are used in more than one function.
Such variables are called global variables. User-defined functions are also declared in
this part of the code.
ATK
5.Main function section
• The main function is the section from where the actual program begins.
• The main function begins with opening curly braces and ends with closing curly braces.
• The program should have one main function.
ATK
6.Sub program:
ATK
ATK
ATK
FIGURE The Greeting Program
ATK
ATK
FIGURE Structure of a C Program
ATK
ATK
How to Write and Run a C Program in Linux
• We use Linux command-line tool, the Terminal, in order to compile a simple C program. To
open the Terminal, you can use the Ctrl+Alt+T shortcut.
ATK
1.Write a simple C program
Then save the file with .c extension. In this example, it is saved as sampleProgram.c
ATK
ATK
2.Compile the C program with gcc Compiler
In your Terminal, enter the following command in order to make an executable version of
the program you have written:
gcc filename.c
ATK
How to Write and Run a C Program in Windows
ATK
Step 1: Creating a Source Code
Source code is a file with C programming instructions in a high-level language. To create source code, we use any text editor
to write the program instructions. The instructions written in the source code must follow the C programming language
rules. The following steps are used to create a source code file in Windows OS…
The compilation is the process of converting high-level language instructions into low-level language
instructions. We use the shortcut key Alt + F9 to compile a C program in Turbo C.
The compilation is the process of converting high-level language instructions into low-level
language instructions.
ATK
• Whenever we press Alt + F9, the source file is going to be submitted to the Compiler.
• On receiving a source file, the compiler first checks for the Errors. If there are any
Errors then compiler returns List of Errors, if there are no errors then the source code is
converted into object code and stores it as a file with .obj extension.
• Then the object code is given to the Linker. The Linker combines both the object
code and specified header file code and generates an Executable file with
a .exe extension
ATK
ATK
ATK
The file which contains c program instructions in a high-level language is said
to be source code. Every c program source file is saved with .c extension, for
example, Sample.c.
ATK
printf() and scanf() in C
The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library
functions, defined in stdio.h (header file).
printf() function
The printf() function is used for output. It prints the given statement to the console.
printf("format string",argument_list);
scanf("format string",argument_list);
ATK
#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
ATK
#include<stdio.h>
int main(){
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}
ATK
Variables in C
• A variable is a name of the 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.
• Let's see the syntax to declare a variable:
type variable_list;
ATK
Rules for defining variables
1. A variable can have alphabets, digits, and underscore.
2. A variable name can start with the alphabet, and underscore only. It can't
start with a digit.
3. No whitespace is allowed within the variable name.
4. A variable name must not be any reserved word or keyword, e.g. int, float,
etc.
Types of Variables in C
There are many types of variables in c:
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Local Variable
• A variable that is declared inside the function or block is called a
local variable.
• It must be declared at the start of the block.
void function1(){
int x=10;//local variable
}
Global Variable
• A variable that is declared outside the function or block is
called a global variable. Any function can change the value of
the global variable. It is available to all the functions.
• It must be declared at the start of the block.
void function1(){
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
If you call this function many times, the local variable will print the same value for each
function call, e.g, 11,11,11 and so on. But the static variable will print the incremented
value in each function call, e.g. 11, 12, 13 and so on.
Automatic Variable
All variables in C that are declared inside the block, are automatic
variables by default. We can explicitly declare an automatic variable using
auto keyword.
void main(){
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
External Variable
We can share a variable in multiple C source files by using an external variable. To declare an external
variable, you need to use extern keyword.
myfile.h
extern int x=10;//external variable (also global)
program1.c
#include "myfile.h"
#include <stdio.h>
void printValue(){
printf("Global variable: %d", global_variable);
}
Data Types in C
Signed Data Type:
• A signed data type can represent both positive and negative numbers.
• The most significant bit (MSB) is used as the sign bit, indicating whether
the number is positive or negative.
• For example, in a 4-bit signed integer, the range of representable values is
from -8 (-2^3) to 7 (2^3 - 1).
• An unsigned data type can represent only non-negative (zero and positive)
numbers.
• All bits are used to represent magnitude, and there is no sign bit.
• For example, in a 4-bit unsigned integer, the range of representable values
is from 0 to 15 (2^4 - 1).
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
Basic Data Types
• The basic data types are integer-based and floating-point based. C language supports both
signed and unsigned literals.
• The memory size of the basic data types may change according to 32 or 64-bit operating
system.
Integer Types:
Void Type:
void: Represents the absence of type. It is often used to indicate
that a function does not return any value.
Derived Types:
Arrays: A collection of elements of the same data type.
Pointers: Variables that store memory addresses of other variables.
Structures: User-defined data types that can store multiple variables of different
types under one name.
Unions: Similar to structures but can only store one value at a time, sharing the
same memory for all its members.
Enumerations: User-defined data types that consist of named integer constants.
#include <stdio.h>
int main() {
int num1 = 1000;
short num2 = 200;
return 0;
} Output
int main() {
int num1 = 2147483647;
long num2 = 2147483648L; // Note the "L" suffix for a long constant.
return 0;
}
Output:
num1 (int): 2147483647
num2 (long): 2147483648
Keywords in C
• A keyword is a reserved word. You cannot use it as a variable name,
constant name, etc. There are only 32 reserved words (keywords) in the C
language.
• A list of 32 keywords in the c language is given below:
Keyword Identifier
Keyword is a pre-defined word. The identifier is a user-defined word
It must be written in a lowercase letter. It can be written in both lowercase and
uppercase letters.
Its meaning is pre-defined in the c Its meaning is not defined in the c
compiler. compiler.
It is a combination of alphabetical It is a combination of alphanumeric
characters. characters.
It does not contain the underscore It can contain the underscore character.
character.
C Operators
ATK
ATK
The precedence and associativity of C operators
category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
1) + : Addition
2) - : Subtraction
3) * : Multiplication
4) / : Division (integer division truncates the decimal part)
5) % : Modulus (remainder after integer division)
ATK
#include <stdio.h>
int main() {
int a = 10, b = 3;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
1) == : Equal to
2) != : Not equal to
3) > : Greater than
4) < : Less than
5) >= : Greater than or equal to
6) <= : Less than or equal to
• Relational operators in C are used to compare two values and
determine the relationship between them.
• These operators return a Boolean result (1 for true or 0 for false)
based on whether the specified condition is satisfied or not.
• Relational operators are commonly used in conditional
statements, loops, and decision-making processes.
if (num1 == num2) {
printf("%d is equal to %d\n", num1,
#include <stdio.h> num2);
} else {
int main() { printf("%d is not equal to %d\n", num1,
int num1, num2; num2);
ATK
• Logical operators in C are used to perform logical operations on Boolean
expressions or values (true or false).
• These operators are used in conditional statements to combine multiple
conditions and make decisions based on the truth values of those
conditions.
• The logical AND operator && takes two operands (Boolean expressions)
and returns 1 (true) if both operands are true. Otherwise, it returns 0
(false).
• The logical OR operator || takes two operands (Boolean expressions)
and returns 1 (true) if at least one of the operands is true. If both
operands are false, it returns 0 (false).
• The logical NOT operator ! takes a single operand (Boolean
expression) and returns 1 (true) if the operand is false. If the operand
is true, it returns 0 (false).
• It basically inverts the truth value of the operand.
#include <stdio.h>
int main() {
int num1 = 5, num2 = 10;
• The bitwise AND operator & performs a bitwise AND operation between the
corresponding bits of two operands.
• Each bit of the result is set to 1 only if the corresponding bits of both
operands are 1; otherwise, it is set to 0.
• For example: int result = 12 & 6; will assign 4 to the variable result since the
binary representation of 12 is 1100, and 6 is 0110. Performing bitwise AND
on these yields 0100, which is 4 in decimal.
Bitwise OR (|):
• The left shift operator << shifts the bits of the left operand to the
left by the number of positions specified by the right operand.
• Zeroes are filled in on the right side.
• For example: int result = 5 << 2; will assign 20 to the variable
result. In binary representation, 5 is 00000101, and left shifting it
by 2 positions gives 00010100, which represents 20 in decimal.
Right Shift (>>):
• The right shift operator >> shifts the bits of the left operand to the right by
the number of positions specified by the right operand.
• For signed integers, the sign bit (most significant bit) is replicated on the
left side when shifting right.
• For unsigned integers, zeroes are filled in on the left side.
• For example: int result = 16 >> 2; will assign 4 to the variable result. In
binary representation, 16 is 00010000, and right shifting it by 2 positions
gives 00000100, which represents 4 in decimal.
Assignment Operators:
1) = : Simple assignment
2) += : Add and assign
3) -= : Subtract and assign
4) *= : Multiply and assign
5) /= : Divide and assign
6) %= : Modulus and assign
7) &= : Bitwise AND and assign
8) |= : Bitwise OR and assign
9) ^= : Bitwise XOR and assign
10)<<= : Left shift and assign
11)>>= : Right shift and assign
• Assignment operators in C are used to assign values to
variables and perform specific operations in a single
statement.
• They combine the assignment operation (=) with another
operation (e.g., addition, subtraction, etc.) and assign the
result back to the variable on the left side of the operator.
#include <stdio.h>
int main() {
int x = 5; // Initialize x with 5
return 0;
ATK
}
Increment and Decrement Operators:
1) ++ : Increment
by 1
2) -- : Decrement
by 1
3) ++var and var++ : Pre-increment and post-
increment
4) --var and var-- : Pre-decrement and post-
decrement
Increment (++):
Explicit type casting allows you to explicitly convert a value of one data type to another
data type using a cast operator.
The cast operator has the syntax (type) expression, where type is the target data type, and
expression is the value to be converted.
Explicit type casting is useful when you want to override the default implicit conversions
or when you need to ensure that a particular type of operation is performed.
ATK
#include <stdio.h>
int main() {
double num1 = 3.14;
int num2;
return 0;
}
ATK
#include <stdio.h>
int main() {
// Implicit Type Conversion
int num1 = 5;
double num2 = 3.14;
double result = num1 + num2;
printf("Implicit Type Conversion:\n");
printf("num1 + num2 = %lf\n\n", result);
return 0;
}
ATK