You are on page 1of 25

Computer Programming

Introduction
• Computer programming is the act of writing computer programs, which are a
sequence of instructions written using a Computer Programming Language to
perform a specified task by the computer.
• There are hundreds of programming languages, which can be used to write
computer programs and following are a few of them −
❑ Java
❑C
❑ C++
❑ Python
❑ PHP
❑ Perl
❑ Ruby
Introduction
• C programming is a general-purpose, procedural, imperative
computer programming language developed in 1972 by Dennis Ritchie
at the Bell Telephone Laboratories to develop the UNIX operating
system.
• C is the most widely used computer language.
• It keeps fluctuating at number one scale of popularity along with Java
programming language, which is also equally popular and most widely
used among modern software programmers.
Why to Learn C Programming?

• C programming language is a MUST for students and working


professional specially when they are working in Software
Development Domain. Some of the key advantages of learning C
Programming:
• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms
Facts about C

• C was invented to write an operating system called UNIX.


• C is a successor of B language which was introduced around the early
1970s.
• The language was formalized in 1988 by the American National
Standard Institute (ANSI).
• The UNIX OS was totally written in C.
• Most of the state-of-the-art software have been implemented using C.
Features of C Programming Language
1. Procedural Language
2. Fast and Efficient
3. Modularity
4. Statically Type
5. General-Purpose Language
6. Rich set of built-in Operators
7. Libraries with rich Functions
8. Middle-Level Language
9. Portability
10. Easy to Extend
Features of C Programming Language
Features of C Programming Language
1. Procedural Language:
• In a procedural language like C step by step predefined instructions are carried
out.
• C program may contain more than one function to perform a particular task.
• a set of functions, instructions, and statements that must be executed in a certain
order to accomplish a job is program.
• In general, procedural language is used to specify the steps that the computer takes
to solve a problem.
• Computer procedural languages include BASIC, C, FORTRAN, Java, and Pascal
Features of C Programming Language
1. Fast and Efficient
• Newer languages like java, python offer more features than c programming
language but due to additional processing in these languages, their performance
rate gets down effectively.
• C programming language being middle-level language provides programmers
access to direct manipulation with the computer hardware but higher-level
languages do not allow this.
• That’s one of the reasons C language is considered the first choice to start learning
programming languages.
• It’s fast because statically typed languages are faster than dynamically typed
languages.
Features of C Programming Language
3. Modularity
• The concept of storing C programming language code in the form of libraries for
further future uses is known as modularity.
• This programming language does very little on its own most of its power is held
by its libraries.
• C language has its own library to solve common problems, we can use a
particular function by using a header file stored in its library.
Features of C Programming Language
4. Statically Type
• C programming language is a statically typed language.
• Meaning the type of variable is checked at the time of compilation but not at run
time.
• This means each time a programmer type a program they have to mention the
type of variables used.

5.Rich set of built-in Operators: It is a diversified language with a rich set of


built-in operators which are used in writing complex or simplified C programs.
Features of C Programming Language
6.General Purpose Language
• From system programming to photo editing software, the C programming
language is used in various applications.
• Some of the common applications where it’s used are as follows:
1. Operating systems: Windows, Linux, iOS, Android, OXS
2. Databases: PostgreSQL, Oracle, MySQL, MS SQL Server etc.

7.Libraries with rich Functions:


Robust libraries and functions in C help even a beginner coder to code with ease
Features of C Programming Language
8.Middle-Level Language: As it is a middle-level language so it has the combined
form of both capabilities of assembly language and features of the high-level
language.

9.Portability: C language is lavishly portable as programs that are written in C


language can run and compile on any system with either none or small changes.

10.Easy to Extend: Programs written in C language can be extended means when a


program is already written in it then some more features and operations can be
added to it.
C Tokens
• A token is the smallest element of a program that is meaningful to the compiler.
Tokens can be classified as follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
Keywords
• Keywords are pre-defined or reserved words in a programming language.
• Each keyword is meant to perform a specific function in a program.
• Since keywords are referred names for a compiler, they can’t be used as variable
names because by doing so, we are trying to assign a new meaning to the
keyword which is not allowed.
• You cannot redefine keywords. However, you can specify the text to be
substituted for keywords before compilation by using C/C++ preprocessor
directives.
• C language supports 32 keywords.
Keywords
Identifiers
• Identifiers: Identifiers are used as the general terminology for the naming of
variables, functions and arrays.
• These are user-defined names consisting of an arbitrarily long sequence of letters
and digits with either a letter or the underscore(_) as a first character.
• Identifier names must differ in spelling and case from any keywords.
• You cannot use keywords as identifiers; they are reserved for special use.
• Once declared, you can use the identifier in later program statements to refer to
the associated value.
• A special kind of identifier, called a statement label, can be used in goto
statements.
Identifiers
• There are certain rules that should be followed while naming c identifiers:
• They must begin with a letter or underscore(_).
• They must consist of only letters, digits, or underscore.
• No other special character is allowed.
• It should not be a keyword.
• It must not contain white space.
• It should be up to 31 characters long as only the first 31 characters are significant.
• E.g identifier1,finding_sum()
Constants
• Constants are also like normal variables.
• But, the only difference is, their values can not be modified by the program once
they are defined.
• Constants refer to fixed values.
• They are also called literals.
• Constants may belong to any of the data type
• Syntax: const data_type variable_name; (or) const data_type
*variable_name;
Types of Constants

1. Integer constants – Example: 0, 1, 1218, 12482


2. Real or Floating-point constants – Example: 0.0, 1203.03, 30486.184
3. Octal & Hexadecimal constants – Example: octal: (013 )8 =
(11)10, Hexadecimal: (013)16 = (19)10
4. Character constants -Example: ‘a’, ‘A’, ‘z’
5. String constants -Example: “Hello”
Strings
• Strings are nothing but an array of characters ended with a null character (‘\0’).
• This null character indicates the end of the string.
• Strings are always enclosed in double-quotes. Whereas, a character is enclosed in single
quotes in C
• Declarations for String:

• char string[20] = {‘g’, ’o’, ‘i’, ‘n’, ‘g’, ‘f’, ‘o’, ‘r’, ‘b’, ’r’, ‘e’, ‘a’, ‘k’, ‘\0’};
• char string[20] = “goingforbreak”;
• char string [] = “goingforbreak”;
• when we declare char as “string[20]”, 20 bytes of memory space is allocated for holding
the string value.
• When we declare char as “string[]”, memory space will be allocated as per the
requirement during the execution of the program.
Special Symbols
The following special symbols are used in C having some special meaning and thus, cannot be used for
some other purpose.[] () {}, ; * = #
❑ Brackets[]: Opening and closing brackets are used as array element reference. These indicate single and
multidimensional subscripts.
❑ Parentheses(): These special symbols are used to indicate function calls and function parameters.
❑ Braces{}: These opening and ending curly braces mark the start and end of a block of code containing
more than one executable statement.
❑ Comma (, ): It is used to separate more than one statements like for separating parameters in function
calls.
❑ Colon(:): It is an operator that essentially invokes something called an initialization list.
❑ Semicolon(;): It is known as a statement terminator. It indicates the end of one logical entity. That’s why
each individual statement must be ended with a semicolon.
❑ Asterisk (*): It is used to create a pointer variable and for the multiplication of variables.
❑ Assignment operator(=): It is used to assign values and for the logical operation validation.
❑ Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to
transform your program before actual compilation.
Operators
• Operators are symbols that trigger an action when applied to C variables and
other objects
• The data items on which operators act upon are called operands.
• Depending on the number of operands that an operator can act upon, operators
can be classified as follows:
• Unary Operators: Those operators that require only a single operand to act
upon are known as unary operators. For Example increment and decrement
operators
• Binary Operators: Those operators that require two operands to act upon are
called binary operators. Binary operators are classified into :
• Arithmetic operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Bitwise Operator
• Ternary Operator: The operator that require three operands to act upon are
called ternary operator. Conditional Operator(?) is also called ternary operator.
Syntax: (Expression1)? expression2: expression3;
Identifiers
• Identifiers are used for the naming of variables, functions, and arrays.
• It is a string of alphanumeric characters that begins with an alphabet, or an
underscore( _ ) that are used for variables, functions, arrays, structures, unions,
and so on.
• It is also known as the user-defined word.
• Identifier names must differ in spelling and case from any keywords. We cannot
use keywords as identifiers; they are reserved for special use. Once an identifier is
declared, we can use the identifier anywhere in the program to refer to the
associated value.
• In programming languages, identifiers are used for identification purposes. Or in
other words, identifiers are the user-defined name of the program components. In
C, an identifier can be a class name, method name, variable name, or label.
Example
int sum()
{
int a,b,s=0;
s=a+b;
printf(“sum is %d”, s);
return(s);
}
No. of identifiers= 4

You might also like