Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

C PROGRAMMING COURSE

technical training

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


INTRODUCTION TO C
• C is a general-purpose programming language
created by Dennis Ritchie at the Bell Laboratories in
1972.
• It is a very popular language, despite being old.
• The C programming language is a procedural and
general-purpose language that provides low-level
access to system memory.
• A program written in C must be run through a C
compiler to convert it into an executable that a
computer can run.
www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org
HISTORY OF C
• C programming language was developed in 1972 by
Dennis Ritchie at bell laboratories of AT&T
(American Telephone & Telegraph), located in the
U.S.A.

• Initially, C language was developed to be used


in UNIX operating system.

• Dennis Ritchie is known as the founder of the c


language.

• It was developed to overcome the problems of


previous languages such as B, BCPL, etc.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org
www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org
STRUCTURE OF C PROGRAMMING
Documentation section:
 Consists of the description of the program.
Preprocessor section:
 The preprocessor section contains all the header files used in a program.
Define section:
 Includes preprocessor directive, which contains symbolic constants.
E.g.: #define
Global declaration:
 The global section comprises of all the global declarations in the program.
Main function:
 For every C program, the execution starts from the main() function. It is
mandatory to include a main() function in every C program.
User defined functions:
 Includes all user-defined functions (functions the user provides). For
example, color(), sum(), division(), etc.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


/** //Documentation Section
* file: age.c
* author: you
* description: program to find our age.
*/

#include <stdio.h> //Preprocessor Section

#define BORN 2000 //Definition Section

int age(int current); //Global Declaration

int main(void) //Main() Function


{
int current = 2021;
printf("Age: %d", age(current));
return 0;
}

int age(int current)


{ //User Defined Function
return current - BORN;
}
INPUT / OUTPUT STATEMENTS
 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).

 There are mainly two of Input/Output functions are

used for this purpose.

 Unformatted I/O functions

 Formatted I/O functions

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


Formatted I/O : printf()
This is an output function. It is used to display a
text message and to display the mixed type Example 1:
(int, float, char) of data on screen. printf(“Hai students”)

SYNTAX: o/p : Hai students

Example 2:
printf("control strings",&v1,&v2,&v3,................&vn);
a=25
(Or) printf(“A value is %d”,a)

o/p : 25
printf("Message line or text line");
Note: %d is format specifies for integers

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


Formatted I/O : scanf()
The scanf() function is an input function.
It used to read the mixed type of data from
Example :
keyboard. Y
You can read integer, float and character data by printf("Enter the a b and c values");
using its control codes or format codes. scanf("%d%d%d", &a,&b,&c);

SYNTAX: Note: %d is format specifies for integers

scanf("control strings",&v1,&v2,......&vn);

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


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.
Syntax
type variable_list;

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


EXAMPLES OF VARIABLES

Example:
• int myInt=4;
• float myReal=2.5;
• char myChar=“a”;

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


DATA TYPES IN C
A data type specifies the type of data that a
variable can store such as integer, floating,
character, etc.
Basic Data Type Example: Employee Data
(Name, Employee ID, Age, Salary, Address,
Derived Data Type
Phone No, etc.)
Enumerated Name: String
ID: Integer
Void Salary: Float or Double

Bool Type Phone No: String

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


DATA TYPES IN C
Data Type Modifiers In C

Modifiers are C keywords that modify the


meaning of fundamental data types. It indicates
how much memory will be allocated to a
variable.
long
short
signed
unsigned

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


BASIC DATA TYPES IN C Data Types Memory Size Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
The C programming language has five
unsigned char 1 byte 0 to 255
short 2 byte −32,768 to 32,767
primitive or primary data types.
signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
Keyword Used Data Type int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
int Integer unsigned int 2 byte 0 to 65,535
short int 2 byte −32,768 to 32,767
signed short int 2 byte −32,768 to 32,767
float Floating-point
unsigned short int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to 2,147,483,647
double Double
signed long int 4 byte -2,147,483,648 to 2,147,483,647

char Character unsigned long int 4 byte 0 to 4,294,967,295


float 4 byte 1.2E-38 to 3.4E+38

void Void double 8 byte 1.7E-308 to 1.7E+308


long double 16 byte 3.4E-4932 to 1.1E+4932

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


DERIVED DATA TYPES IN C
 An array in C is a collection of multiple values of a similar
 Derived data types are primary data types that are
data type and is stored in a contiguous memory location.
grouped together.
 The pointer data type is used to store the address of another

 You can group many elements of similar data types. variable.

 These data types are defined by the user.  Structure is a data type that can store variables of similar or

 The following are the derived data types in C: different data types. The size of the structure is the sum of

 Array the storage size required by each variable.

 A union is a group of elements with similar or different data


 Pointers
types. In a union, the memory location is the same for all the
 Structure
elements.
 Union

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


ENUMERATED DATA TYPE IN C

 In C programming, an enumeration type

(also called enum) is a data type that #include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};


consists of integral constants.
int main()
 To define enums, the enum keyword is {
enum week day;
used. day = Wed;
printf("%d",day);
return 0;
}

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


VOID DATA TYPE IN C

 A data type that has no values or operators and is used to represent nothing.
 It is used in three kinds of situations:
 Function returns as void
Ex: void exit (int status);
 Function arguments as void
Ex: int rand(void);
 Pointers to void
Ex: void *malloc( size_t size);

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


BOOLEAN DATA TYPE IN C

 In C, Boolean is a data type that contains two types of values, i.e., 0 and 1.

 Basically, the bool type value represents two types of behavior, either true or false.

Here, '0' represents false value, while '1' represents true value.

Syntax:

bool variable_name;

Example:

bool x=true;

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


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

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


EXAMPLES OF KEYWORDS

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


IDENTIFIERS IN C
 Identifier is a name, used identify elements in C Programming.

 Created by a programmer to identify various program elements.

 Identifiers are the names of variables, arrays, functions,


structure and etc.

 C Programming language is case-sensitive.

 Example are: firstname, FIRSTNAME, FirstName will be


treated differently.

 Programmer can use underscores _ in between identifiers.


e.g.) first_name.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


IDENTIFIERS RULES FOR DECLARATION

First character must be a letter or underscore _.

Characters in identifiers are limited up to 31 in ANSI C compiler(it


may vary).

Blankspaces and commas are not allowed within an Identifier.

Identifiers should not be starts with a digit.

Identifier name should not be the keyword.

Identifiers must be short and meaningful.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


EXAMPLES OF IDENTIFIERS

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


OPERATORS IN C
An operator is a symbol that operates on a value or a
variable. For example: + is an operator to perform addition.

C has a wide range of operators to perform various


operations.

Example:

int a = 100 + 50; // 150 (100 + 50)


int b = a + 250; // 400 (150 + 250)
int c = b + b; // 800 (400 + 400)

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


TYPES OF OPERATORS IN C

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


UNARY OPERATORS

Unary operators are

used on a single

operand in order to

calculate the new

value of that variable.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


ARITHMETIC OPERATORS
Arithmetic Operators are

operators which are used within

the equation to perform a

number of basic mathematical

calculations.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


RELATIONAL OPERATORS

Relational Operators are

used for the purpose of

comparison of two or more

numerical values stored in

an operand.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


LOGICAL OPERATORS
Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the
expression is completely dependent on the original expression or value or variable.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


BITWISE OPERATORS
Bitwise operators are used for manipulating OPERATOR EXAMPLE EXPLANATION
data at the bit level (binary data) << left shift x=x<<2 Before: 0000 1111 x is 15 (8+4+2+1)
After: 0011 1100 x is 60 (32+16+8+4)

>> right shift x=x>>2 Before: 0000 1111 x is 15 (8+4+2+1)


After: 0000 0011 x is 3 (2+1)

& bit-wise AND x=x&28 0000 1111 & 0001 1010 =0000 1010
15 28 10

| bit-wise OR x=x|28 0000 1111 | 0001 1010 =0001 1111


15 28 31

^ bit-wise XOR x=x^28 0000 1111 ^ 0001 1010 =0001 0101


15 28 21

~ bit inversion x=x~28 Before: 0000 1111 x is 15 (8+4+2+1)


After: 1111 0000 x is -2,147,483,633

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


ASSIGNMENT OPERATORS
Assignment Operators help us to assign the value or result of an expression to a variable and the value on the
right side must be of the same data type as the variable on the left side.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


CONDITIONAL OPERATOR
 The conditional operator is the one and only
ternary operator in the C programming
language.

 It can be used as an alternative for if-else


condition if the 'if else' has only one
statement each.

 The conditional operator takes an expression Example:

and executes the first statement if the A=10


expression evaluates to be true, and the B=0
second statement if the expression evaluates B=(A==10) ? 10 : 20
to be false. Output : B=10

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


SEPCIAL OPERATORS
C Language Offers Some Special Operators for Programmers….
OPERATOR EXAMPLE EXPLANATION
The Comma Operator for(i=0,j=1;i>10:i++,j++) The Comma operator can be used to link the related expressions
together.
While(c<10,c--)
Type cast Operator float s= 12.5; converts a to the specified type.
int a;
a = (int) s;
now a value is 12.
Reference operator or data_type x; It returns the pointer address of the variable. This is called "referencing"
Address Operator ("&") data_type *pt; operator.
pt = &x
Dereference operator ("*") data_type *pt; Used to "dereferencing" the pointer.
or Pointer Operator

Double Pointer operator data_type **pt; Double Pointer is, that double pointer points to another pointer variable
("**") address.
sizeof operator sizeof (data_type) sizeof returns the size of a variable or datatype

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


COMMENTS IN C
Comments can be used to explain code, and

to make it more readable.

It can also be used to prevent execution


Example:
when testing alternative code.
// This is a comment
Comments can be
printf("Hello World!");

singled-lined
/* The code below will print the words Hello World!
multi-lined to the screen, and it is amazing */
printf("Hello World!");

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


FORMAT SPECIFIERS IN C
The format specifiers are used in C for input and output purposes.

Using this concept the compiler can understand that what type of data is in a variable
during taking input using the scanf() function and printing using printf() function.

Example:

print(“Value of a is %d”,234)

scanf(“Enter the age %d”,&age)

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


FORMAT SPECIFIERS IN C
Format Specifier Type %lu Unsigned int or unsigned long
%c Character %lli or %lld Long long
%d Signed integer
%llu Unsigned long long
%e or %E Scientific notation of floats
%o Octal representation
%f Float values
%g or %G Similar as %e or %E %p Pointer

%hi Signed integer (short) %s String


%hu Unsigned Integer (short) %u Unsigned int
%i Unsigned integer
%x or %X Hexadecimal representation
%l or %ld or %li Long
%n Prints nothing
%lf Double
%Lf Long double %% Prints % character

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


FORMAT SPECIFIERS IN C
Format Specifier Example Output
%8d x=900
%-8d printf("%8d", x); 900
printf("%-8d",x); 900
%08d int x=12;
printf("%08d", x); 00000012
%.2f float x=12.2;
printf("%.2f", x); 12.20
%24s char blogName[] = “aticleworld";
%24.6s printf("%24s", blogName); aticleworld
printf("%24.6s", blogName); aticle

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


ESCAPE SEQUENCE IN C
Generally, an escape sequence begins with a backslash ‘\’
followed by a character or characters.

The c compiler interprets any character followed by a ‘\’ as an


escape sequence.

Escape sequences are used to format the output text and are
not generally displayed on the screen.

Each escape sequence has its own predefined function.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


ESCAPE SEQUENCE IN C Example 1:
printf("You\nare\nlearning\n\'c\' language\n\"Do you k
now C language\"");
Output:
You are learning
'c' language
"Do you know C language“
Example 2:
printf("\n horizontal tab escape sequence tutorial");
printf(" \n 34543 \t 345435 ");
printf(" \n 123 \t 678 ");
Output:
horizontal tab escape sequence tutorial
34543 345435
123 678
www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org
ASCII CODE IN C
 ASCII is abbreviated as the “American Standard Code for Information

Interchange”.

 It is a character encoding schema that is used for electronic

communication.

 ASCII contains numbers, each character has its own number to represent.

 We have 256 character to represent in C (0 to 255) like character (a-z, A-

Z), digits (0-9) and special character like !, @, # etc.

 This each ASCII code occupied with 7 bits in the memory.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


CONSTANTS IN C
A constant is a value or variable that can't be changed in the program.

List of Constants:
Constant Example
Decimal Constant 10, 20, 450 etc.
Real or Floating-point Constant 10.3, 20.2, 450.6 etc.
Octal Constant 021, 033, 046 etc.
Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.
Character Constant 'a', 'b', 'x' etc.
String Constant "c", "c program", "c in javatpoint" etc.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


CONSTANTS IN C
Two way to define constants:
 const keyword
 #define preprocessor (Macro)

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


LITERALS IN C
 Literals are data used for representing fixed values.

 They can be used directly in the code.

 For example: 1, 2.5, 'c' etc.

 Types of literals:
 Integer literal
 Float literal
 Character literal
 String literal
 Backslash Character Literals (Escape Sequences)
www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org
LITERALS IN C Character literal:
A character literal is created by enclosing a
Integer literal: single character inside single quotation
An integer is a numeric literal(associated with marks.
numbers) without any fractional or exponential part. Ex: 'a', 'm', 'F', '2', '}' etc.
1. Decimal Numer (base 10) Ex: 0, -9, 22 etc String literal:
2. Octal Number (base 8) Ex: 021, 077, 033 etc A string literal is a sequence of characters
3. Hexadecimal Number (base 16) Ex: 0x7f, 0x2a, etc enclosed in double-quote marks.
Float literal: Ex: "study", "tonight", "c programming", etc.
A floating-point literal is a numeric literal that has Backslash Character Literals (Escape Sequences):
either a fractional form or an exponent form. Sometimes, it is necessary to use characters
Ex: 100.50 0.000127 -0.77E-5 that cannot be typed or has special meaning in
C programming.
Ex: \n, \t, etc.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


PROGRAMMING ERRORS IN C
 Errors are the problems or the faults that occur in
the program, which makes the behavior of the
program abnormal, and experienced developers can
also make these faults.
 Programming errors are also known as the bugs or
faults, and the process of removing these bugs is
known as debugging.
 These errors are detected either during the time of
compilation or execution.
 Thus, the errors must be removed from the program
for the successful execution of the program.

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


PROGRAMMING ERRORS IN C
Syntax error:
 Syntax errors are also known as the compilation errors as Logical error:
they occurred at the compilation time.  The logical error is an error that leads to an
Ex: if(.) // syntax error undesired output. These errors produce the incorrect
Run-time error: output, but they are error-free, known as logical
 Sometimes the errors exist during the execution-time even errors.
after the successful compilation known as run-time errors. Semantic error:
Ex: int b=2/0; // run time error  Semantic errors are the errors that occurred when
Linker error: the statements are not understandable by the
 Linker errors are mainly generated when the executable file compiler.
of the program is not created. This can be happened either Ex: int b = "javatpoint"; // semantic error
due to the wrong function prototyping or usage of the
wrong header file.
Ex: int Main() { } // linkage error

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org


Next Class…………………….
STATIC KEYWORD IN C
 Static is a keyword used in C programming language. Syntax:

 It can be used with both variables and functions. Static Variable:


 Static keyword can be used in the following situations:
static data_type variable_name;
 Static global variable
Static Function:
 Static function
static void func()
 Static local variable
 Static member variables {

 Static method printf("Hello javaTpoint");

www.ciits.org cognitiveiitsolutions@gmail.com | admin@ciits.org

You might also like