c Language Tutorial Keypoint-Version 1
c Language Tutorial Keypoint-Version 1
This C tutorial series will help you to get started in the C programming
language. By learning C, you will understand basic programming
concepts.
WHAT IS C
• In 1988, the American National Standards Institute (ANSI) had formalized the C
language.
• C was invented to write UNIX operating system.
• C is a successor of 'Basic Combined Programming Language' (BCPL) called B
language.
• Linux OS, PHP, and MySQL are written in C.
• C has been written in assembly language.
• Database Systems
• Language Interpreters
• Compilers and Assemblers
• Operating Systems
• Network Drivers
• Word Processors
1
C HAS BECOME VERY POPULAR FOR VARIOUS REASON
ADVANTAGES OF C
DISADVANTAGES OF C
• Difficult to debug.
• 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 isn't easy to read and understand the program.
2
• C compilers can only identify errors and are incapable of handling exceptions (run-
time errors).
• C provides no data protection.
• It also doesn't feature the reusability of source code extensively.
• It does not provide strict data type checking (for example, an integer value can be
passed for floating datatype).
C INSTALLATION
To start learning C programming, you only have to install the C compiler in your system, and
nowadays C and C++ both compilers come as a single integrated package, which serves the
purpose of C and C++ both program development.
WHAT IS COMPILER IN C ?
The C compiler is a software application that transforms the human-readable C program code
to machine-readable code. The process of transforming the code from High-Level Language
to Machine Level Language is called "Compilation". The human-readable code is the C
program that consists of digits letters, special symbols, etc. which is understood by human
beings. On the other hand, machine language is dependent on the processor and processor
understands zeroes and ones (binary) only. All C program execution is based on a processor
which is available in the CPU; that is why entire C source code needs to be converted to the
binary system by the compiler.
This tutorial is written for Windows, Unix / Linux, and MAC users. All code has been tested,
and it works correctly on all three operating systems.
To use C compiler in Windows, you can install any one software mentioned below.
If you are using UNIX / Linux, then most probably C compiler called GCC is already in your
system. To check if you have it installed, you can type cc or gcc at the command prompt.
$ gcc -v
If for some reason it is not installed on your system, you can download it from
gcc.gnu.org/install.
Xcode development environment came with GNU C/C++ compiler; You can install it from
Apple's website.You can download Xcode from developer.apple.com/technologies/tools.
3
C IDENTIFIERS
Identifiers are names given to different entities such as constants, variables, structures,
functions, etc.
Example: -
int amount;
double totalbalance;
In the above example, amount and totalbalance are identifiers and int, and double are
keywords.
• An identifier can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e. letters &
digits) and underscore( _ ) symbol.
• Identifier names must be unique
• The first character must be an alphabet or underscore.
• You cannot use a keyword as identifiers.
• Only the first thirty-one (31) characters are significant.
• It must not contain white spaces.
• Identifiers are case-sensitive.
4
C KEYWORDS
The C Keywords must be in your information because you can not use them as a variable
name.
You can't use a keyword as an identifier in your C programs, its reserved words in C library
and used to perform an internal operation. The meaning and working of these keywords are
already known to the compiler.
#include<stdio.h>
int main()
{
float a, b;
printf("Showing how keywords are used.");
return 0;
}
In the above program, float and return are keywords. The float is used to declare variables,
and return is used to return an integer type value in this program.
5
C CONSTANTS
Constants are like a variable, except that their value never changes during execution once
defined.
C Constants is the most fundamental and essential part of the C programming language.
Constants in C are the fixed values that are used in a program, and its value remains the same
during the entire execution of the program.
SYNTAX
#include<stdio.h>
void main()
{
const int SIDE = 10;
int area;
area = SIDE*SIDE;
printf("The area of the square with side: %d is: %d sq. units"
, SIDE, area);
}
or
6
C OPERATORS
C operators are symbols that are used to perform mathematical or logical manipulations. The
C programming language is rich with built-in operators. Operators take part in a program for
manipulating data and variables and form a part of the mathematical or logical expressions.
TYPES OF OPERATORS IN C
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operator
7. Bitwise Operators
8. Special Operators
ARITHMETIC OPERATORS
Arithmetic Operators are used to performing mathematical calculations like addition (+),
subtraction (-), multiplication (*), division (/) and modulus (%).
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
#include <stdio.h>
void main()
{
int i=3,j=7,k; /* Variables Defining and Assign values */
k=i+j;
printf("sum of two numbers is %d\n", k);
}
7
INCREMENT AND DECREMENT OPERATORS
Increment and Decrement Operators are useful operators generally used to minimize the
calculation, i.e. ++x and x++ means x=x+1 or -x and x−−means x=x-1. But there is a slight
difference between ++ or −− written before or after the operand. Applying the pre-increment
first add one to the operand and then the result is assigned to the variable on the left whereas
post-increment first assigns the value to the variable on the left and then increment the
operand.
Operator Description
++ Increment
−− Decrement
#include <stdio.h>
//stdio.h is a header file used for input.output purpose.
void main()
{
//set a and b both equal to 5.
int a=5, b=5;
PROGRAM OUTPUT
5 4
4 3
3 2
2 1
1 0
RELATIONAL OPERATORS
Operator Description
== Is equal to
!= Is not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
8
LOGICAL OPERATORS
C provides three logical operators when we test more than one condition to make decisions.
These are: && (meaning logical AND), || (meaning logical OR) and ! (meaning logical
NOT).
Operator Description
And operator. It performs logical conjunction of two expressions. (if both
&& expressions evaluate to True, result is True. If either expression evaluates to
False, the result is False)
Or operator. It performs a logical disjunction on two expressions. (if either or
||
both expressions evaluate to True, the result is True)
! Not operator. It performs logical negation on an expression.
ASSIGNMENT OPERATORS
Operator Description
= Assign
+= Increments then assign
-= Decrements then assign
*= Multiplies then assign
/= Divides then assign
%= Modulus then assign
<<= Left shift and assign
>>= Right shift and assign
&= Bitwise AND assign
^= Bitwise exclusive OR and assign
|= Bitwise inclusive OR and assign
CONDITIONAL OPERATOR
C offers a ternary operator which is the conditional operator (?: in combination) to construct
conditional expressions.
Operator Description
?: Conditional Expression
9
C DATA TYPES
The data-type in a programming language is the collection of data with values having fixed
meaning as well as characteristics. Some of them are an integer, floating point, character, etc.
Usually, programming languages specify the range values for given data-type.
• _Bool
• _Complex
• _Imaginary
After taking suitable variable names, they need to be assigned with a data type. This is how
the data types are used along with variables:
10
int age;
char letter;
float height, width;
C allows the feature called type definition which allows programmers to define their identifier
that would represent an existing data type. There are three such types:
11
int f = -185; // -ve integer data type
short g = 130; // short +ve integer data type
short h = -130; // short -ve integer data type
double i = 4.1234567890; // double float data type
float j = -3.55; // float data type
}
The storage representation and machine instructions differ from machine to machine. sizeof
operator can use to get the exact size of a type or a variable on a particular platform.
#include <stdio.h>
#include <limits.h>
int main()
{
printf("Storage size for int is: %d \n", sizeof(int));
printf("Storage size for char is: %d \n", sizeof(char));
return 0;
}
12
C VARIABLES
The primary purpose of variables is to store data in memory for later use. Unlike constants
which do not change during the program execution, variables value may change during
execution. If you declare a variable in C, that means you are asking the operating system to
reserve a piece of memory with that variable name.
VARIABLE DECLARATION IN C
Syntax
type variable_name;
or
VARIABLE ASSIGNMENT
EXAMPLE
• A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9,
and the underscore character.
• The first character must be a letter or underscore.
• Blank spaces cannot be used in variable names.
• Special characters like #, $ are not allowed.
• C keywords cannot be used as variable names.
• Variable names are case sensitive.
• Values of the variables can be numeric or alphabetic.
• Variable type can be char, int, float, double, or void.
13
C PROGRAM TO PRINT VALUE OF A VARIABLE
EXAMPLE
#include<stdio.h>
void main()
{
int age = 33; // c program to print value of a variable
printf("I am %d years old.\n", age);
}
OUTPUT
I am 33 years old.
C TYPE CASTING
Type Casting in C is used to convert a variable from one data type to another data type, and
after type casting compiler treats the variable as of the new data type.
SYNTAX
(type_name) expression
#include <stdio.h>
void main ()
{
int a;
a = 15/6;
printf("%d",a);
}
RESULT
#include <stdio.h>
void main ()
{
float a;
a = (float) 15/6;
printf("%f",a);
}
After type cast is done before division to retain float value 2.500000.
14