0% found this document useful (0 votes)
2 views14 pages

c Language Tutorial Keypoint-Version 1

This document is a comprehensive tutorial on the C programming language, covering its history, features, advantages, disadvantages, and installation process. It explains key concepts such as identifiers, keywords, constants, operators, data types, and variable declarations, providing examples for clarity. The tutorial aims to help beginners understand the fundamentals of C programming and its applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

c Language Tutorial Keypoint-Version 1

This document is a comprehensive tutorial on the C programming language, covering its history, features, advantages, disadvantages, and installation process. It explains key concepts such as identifiers, keywords, constants, operators, data types, and variable declarations, providing examples for clarity. The tutorial aims to help beginners understand the fundamentals of C programming and its applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

C LANGUAGE TUTORIAL KEYPOINT

We recommend reading this tutorial, in the sequence listed in the left


menu.

This C tutorial series will help you to get started in the C programming
language. By learning C, you will understand basic programming
concepts.

And I Hope you enjoy it all Thanks....

This tutorial supplements all explanations with clarifying examples.

WHAT IS C

C is a mid-level structured oriented programming language, used in general-purpose


programming, developed by Dennis Ritchie at AT&T Bell Labs, the USA, between 1969 and
1973.

SOME FACT ABOUT C PROGRAMMING LANGUAGE

• 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.

USES OF C PROGRAMMING LANGUAGE

In the beginning, C was used for developing system applications, e.g. :

• Database Systems
• Language Interpreters
• Compilers and Assemblers
• Operating Systems
• Network Drivers
• Word Processors

1
C HAS BECOME VERY POPULAR FOR VARIOUS REASON

• One of the early programming languages.


• Still, the best programming language to learn quickly.
• C language is reliable, simple, and easy to use.
• C language is a structured language.
• Modern programming concepts are based on C.
• It can be compiled on a variety of computer platforms.
• Universities preferred to add C programming in their courseware.

FEATURES OF C PROGRAMMING LANGUAGE

• C is a robust language with a rich set of built-in functions and operators.


• Programs written in C are efficient and fast.
• C is highly portable; programs once written in C can be run on other machines with
minor or no modification.
• C is a collection of C library functions; we can also create our function and add it to
the C library.
• C is easily extensible.

ADVANTAGES OF C

• C is the building block for many other programming languages.


• Programs written in C are highly portable.
• Several standard functions are there (like in-built) that can be used to develop
programs.
• C programs are collections of C library functions, and it's also easy to add functions to
the C library.
• The modular structure makes code debugging, maintenance, and testing easier.

DISADVANTAGES OF C

• C does not provide Object Oriented Programming (OOP) concepts.


• There are no concepts of Namespace in C.
• C does not provide binding or wrapping up of data in a single unit.
• C does not provide Constructor and Destructor.

The limitations of C programming languages are as follows:

• 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 ?

A compiler is a computer program that transforms human-readable (programming language)


source code into another computer language (binary) code.
In simple terms, Compiler takes the code that you wrote and turned in to the binary code that
the computer can understand.

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.

• You can download a 90-day trial version of Visual Studio


• You can download Dev-C++ IDE to develop C and C++ application.
• You can install MinGW

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.

RULES FOR NAMING IDENTIFIERS

• 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.

A list of 32 reserved keywords in c language is given below:

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

#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.

• Constants are also called literals.


• Constants can be any of the data types.
• It is considered best practice to define constants using only upper-case names.

SYNTAX

const type constant_name;

const keyword defines a constant in C.

#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);
}

Putting const either before or after the type is possible.

int const SIDE = 10;

or

const int SIDE = 10;

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

C programming language offers various types of operators having different functioning


capabilities.

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;

//Print them and decrementing each time.


//Use postfix mode for a and prefix mode for b.
printf("\n%d %d",a--,--b);
printf("\n%d %d",a--,--b);
printf("\n%d %d",a--,--b);
printf("\n%d %d",a--,--b);
printf("\n%d %d",a--,--b);
}

PROGRAM OUTPUT

5 4
4 3
3 2
2 1
1 0

RELATIONAL OPERATORS

Relational operators are used to comparing two quantities or values.

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

Assignment operators applied to assign the result of an expression to a variable. C has a


collection of shorthand 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

A data-type in C programming is a set of values and is determined to act on those values. C


provides various types of data-types which allow the programmer to select the appropriate
type for the variable to set its value.

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.

C Data Types are used to:

• Identify the type of a variable when it declared.


• Identify the type of the return value of a function.
• Identify the type of a parameter expected by a function.

ANSI C provides three types of data types:

1. Primary(Built-in) Data Types:


void, int, char, double and float.
2. Derived Data Types:
Array, References, and Pointers.
3. User Defined Data Types:
Structure, Union, and Enumeration.

PRIMARY DATA TYPES

Every C compiler supports five primary data types:

As the name suggests, it holds no value and is generally used for


void specifying the type of function or what it returns. If the function has a
void type, it means that the function will not return any value.
int Used to denote an integer type.
char Used to denote a character type.
float, double Used to denote a floating point type.
int *, float *, char * Used to denote a pointer type.

Three more data types have been added in C99:

• _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;

DERIVED DATE TYPES

C supports three derived data types:

Data Types Description


Arrays are sequences of data items having homogeneous values.
Arrays
They have adjacent memory locations to store values.
Function pointers allow referencing functions with a particular
References
signature.
These are powerful C features which are used to access the
Pointers
memory and deal with their addresses.

USER DEFINED DATA TYPES

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:

Data Types Description


It is a package of variables of different types under a single name.
Structure This is done to handle data efficiently. "struct" keyword is used to
define a structure.
These allow storing various data types in the same memory
location. Programmers can define a union with different members,
Union
but only a single member can contain a value at a given time. It is
used for
Enumeration is a special data type that consists of integral
Enum constants, and each of them is assigned with a specific name.
"enum" keyword is used to define the enumerated data type.

DATA TYPES AND VARIABLE DECLARATIONS IN C


#include <stdio.h>
int main()
{
int a = 4000; // positive integer data type
float b = 5.2324; // float data type
char c = 'Z'; // char data type
long d = 41657; // long positive integer data type
long e = -21556; // long -ve integer data type

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

Variables are memory locations(storage area) in the C programming language.

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

type variable_name, variable_name, variable_name;

VARIABLE DECLARATION AND INITIALIZATION


int width, height=5;
char letter='A';
float age, area;
double d;

/* actual initialization */width = 10;


age = 26.5;

VARIABLE ASSIGNMENT

A variable assignment is a process of assigning a value to a variable.

EXAMPLE

int width = 60;


int age = 31;

There are some rules on choosing variable names

• 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

WITHOUT TYPE CASTING

#include <stdio.h>
void main ()
{
int a;
a = 15/6;
printf("%d",a);
}

RESULT

In the above C program, 15/6 alone will produce integer value as 2.

AFTER TYPE CASTING

#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

You might also like