0% found this document useful (0 votes)
57 views

Language C

Uploaded by

Purushotham
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Language C

Uploaded by

Purushotham
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Language C

Introduction to C

Internal Use - Confidential


Introduction to C

C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972 which is developed by Dennis Ritchie.

It is a high level, general purpose programming language which was initially used for system development work. It was
accepted as a system development language because it generates the code that runs almost as fast as the code written in
assembly language. Also called as ELL (English like language).

C is a:

 Procedural language
 Structured Language
 High level Language
 Easy to learn
 It can be compiled on a variety of computer platforms.

Internal Use - Confidential


Language C

Basics

Basic Syntax, Tokens, Semicolons, Comments, Identifiers, Keywords, Whitespace

Internal Use - Confidential


BASIC SYNTAX

Syntax basically refers to the protocols to be followed while writing a program. It is very necessary to follow
proper syntax while coding to get the desired set of output.
The C basic syntax consists of header files, main function, and program code. This is the most fundamental
structure in the C program.

#include<stdio.h>
int main() // main function with integer return type
{
printf ("Welcome to programming!\n"); // print statement to display output
return 0; // Indicates that the main function returns null value
}

Internal Use - Confidential


TOKENS in C
A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal,
or a symbol. The following C statement consists of five tokens −

printf(“Hello, World! \n”);

SEMICOLONS
The semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It
indicates the end of one logical entity. Given below are two different statements −
printf(“Hello, World! \n”);
return 0;
Internal Use - Confidential
COMMENTS
C comments are helping texts in your C program and are ignored by the compiler. They start with /* and
terminate with the characters */.

/* my first program in C */

You cannot have comments within comments and they do not occur within a string or character literals.

IDENTIFIERS
C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts
with a letter A to Z, a to z, or an underscore '_' followed by zero or more letters, underscores, and digits (0 to
9). C does not allow punctuation characters such as @, $, and % within identifiers.

C is a case-sensitive programming language. So, Manpower and manpower are two different identifiers.
Examples of acceptable identifiers −
mohd zara abc move_name
a_123
myname50 _temp j a23b9
retVal
Internal Use - Confidential
KEYWORDS

The following list shows the reserved words in C. These reserved words may not be used as constants or variables or
any other identifier names.
auto else long switch

break enum register typedef

case extern return union

char float short unsigned

const for signed void

continue goto sizeof volatile

default if static while

do double struct Packed

WHITESPACES in C

A line containing only whitespace, possibly with a comment, is known as a blank line, and a C compiler totally ignores it.
Whitespace is the term used in C to describe blanks, tabs, newline characters and comments. Whitespace separates
one part of a statement from another and enables the compiler to identify where one element in a statement, such as
int, ends and the next element begins.
int age;
Internal Use - Confidential
Language C

Datatypes
Basic Types, Enumerated Types, The Type Void, Derived Types, Integer Types
Floating - Point Types, The Void Type

Internal Use - Confidential


DATATYPES:
Basic Types, Enumerated Types, Type Void, Derived Types, Integer Types, Floating - Point Types, Void Type

A datatype determines the type and the operations that can be performed on the data. C provides various data
types and each data type is represented differently within the computer’s memory.
Type-name Type Range
int Numeric – Integer -32 768 to 32 767
short Numeric – Integer -32 768 to 32 767
long Numeric – Integer -2 147 483 648 to 2 147 483 647
float Numeric – Real 1.2 X 10-38 to 3.4 X 1038
double Numeric – Real 2.2 X 10-308 to 1.8 X 10308
char Character All ASCII characters

Internal Use - Confidential


VARIABLES and CONSTANT
Variables – If we declare a variable in C, we ask the operating system for a piece of memory where we can give a name and
store something in that piece of memory. There are two basic kinds of variables in C which are numeric and character.
Numeric variables Character variables
Numeric variables can either be of the type integer (int) or of the Character variables are letters of the alphabet, ASCII characters
type real (float). Integer (int) values are whole numbers (like 10 or - or numbers 0-9. If you declare a character variable, the character
10). Real (float) values can have a decimal point (Like 1.23 or - must be between single quotes (like so ‘A’).
20.123).

Constants – The difference between variables and constants is that variables can change their value at any time but
constants can never change their value. (The constants value is locked for the duration of the program). Constants can be
very useful, Pi for instance is a good example to declare as a constant.
Language C

Storage Classes

Register Storage Class, The Static Storage Class, The extern Storage Class

Internal Use - Confidential


STORAGE CLASSES
Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time
which help us to trace the existence of a particular variable during the runtime of a program.

C uses 4 storage classes:

auto: This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while
writing programs in C language. Auto variables can be only accessed within the block/function they have been declared and not outside
them (which defines their scope). Of course, these can be accessed within nested blocks within the parent block/function in which the auto
variable was declared. However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to
the very exact memory location where the variables resides. They are assigned a garbage value by default whenever they are declared.
Internal Use - Confidential
extern: Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically,
the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is
nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any
function/block. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition
in any function/block. It basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable
only. Main purpose of using extern variables is that it can be accessed between two different files which are part of a large program.

static: This storage class is used to declare static variables which are popularly used while writing programs in C language. Static variables
have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use
in their scope. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is
allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be
accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.

register: This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is
that the compiler tries to store these variables in the register of the microprocessor if a free register is available. This makes the use of
register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register is
not available, these are then stored in the memory only. Usually few variables which are to be accessed very frequently in a program are
declared with the register keyword which improves the running time of the program. An important and interesting point to be noted here
is that we cannot obtain the address of a register variable using pointers.

To specify the storage class for a variable, the following syntax is to be followed:

Syntax: storage_class var_data_type var_name;


Internal Use - Confidential
Language C

Operators
Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment
Operators, Miscellaneous Operators, Operators precedence in C

Internal Use - Confidential


OPERATORS
C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform a certain mathematical or
logical manipulation. Operators are used in programs to manipulate data and variables.

Internal Use - Confidential


Language C

Decision Making in C

Internal Use - Confidential


DECISION MAKING in C
Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until
certain specified conditions are met. C language handles decision-making by supporting the following statements.

1. If statement
2. Switch statement
3. Conditional operator statement (? : operator)
4. GOTO statement

Internal Use - Confidential


Sr.No. Specifier & Output

1 c
Character
2 d or i
Signed decimal integer
3 e
Scientific notation (mantissa/exponent) using e character

4 E
Scientific notation (mantissa/exponent) using E character

5 f
Decimal floating point
6 g
Uses the shorter of %e or %f
7 G
Uses the shorter of %E or %f
8 o
Signed octal
9 s
String of characters
10 u
Unsigned decimal integer
11 x
Unsigned hexadecimal integer
12 X
Unsigned hexadecimal integer (capital letters)
13 p
Pointer address
14 n
Nothing printed
15 %
Character

Internal Use - Confidential


 #include<stdio.h>
 void main()
 {
 int a, b, c, big ;
 printf("Enter three numbers : ") ;
 scanf("%d %d %d", &a, &b, &c) ;
 big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
 printf("\nThe biggest number is : %d", big) ;
 }

Internal Use - Confidential


Language C

Loops in C

Internal Use - Confidential


LOOPS

Internal Use - Confidential


Language C

Functions in C

Internal Use - Confidential


FUNCTIONS
The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and
modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known
as procedure or subroutine in other programming languages.

Syntax: return_type function_name(data_type parameter...)


{
//code to be executed
}
Advantage of functions in C:
 By using functions, we can avoid rewriting same logic/code again and again in a program.
 We can call C functions any number of times in a program and from any place in a program.
 We can track a large C program easily when it is divided into multiple functions.
 Reusability is the main achievement of C functions.
 However, Function calling is always a overhead in a C program.

Internal Use - Confidential


Different aspects of calling Function

A function may or may not accept any argument. It may or may not return any value. Based on these facts, There are four different aspects of
function calls.

 Function without arguments and without return value


 Function without arguments and with return value
 Function with arguments and without return value
 Function with arguments and with return value

Return Value

C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return
type.
Example without return value: void hello()

Internal Use - Confidential printf("hello c");


Language C

Arrays in C

Internal Use - Confidential


ARRAYS
An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of
an array. They are used to store similar type of elements as in the data type must be the same for all elements.

Syntax: int a[ ];

Why do we need arrays?


We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it
becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable.

Advantages of an Array in C/C++:

 Random access of elements using array index.


 Use of less line of code as it creates a single array of multiple elements.
 Easy access to all the elements.
 Traversal through the array becomes easy using a single loop.
 Sorting becomes easy as it can be accomplished by writing less line of code.

Disadvantages of an Array in C/C++:


 Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not
dynamic.
 Insertion and deletion of elements can be costly since the elements are needed to be managed with new memory allocation.
Internal Use - Confidential
Declaring Arrays

There are various ways in which we can declare an array. It can be done by specifying its type and size, by initializing it or both.

 Array declaration by specifying size


Syntax: int arr1[10];

 Array declaration by initializing elements.


Syntax: int arr[ ]={10. 20, ,30, 40}

 Array declaration by specifying size and initializing elements.


Syntax: int arr[6]={10,20,30,40}

How to access array element?

Internal Use - Confidential


End of Session 1

Internal Use - Confidential

You might also like