C - Fundamentals
Pratik Shah
II – Sem CSE (DIV B)
24/01/23 to 12/05/23
Student will be able to :
Importance of C
• It is a robust language whose rich set of built-in functions and operators can be used to
write any complex program.
• Programs written in C are efficient and fast.
• There are only 32 keywords in ANSI C and its strength lies in its built-in functions.
• C is highly portable.(This means that C programs written for one computer can be run on
another with little or no modification. Portability is important if we plan to use a new
computer with a different operating system)
• C language is well suited for structured programming
• Another important feature of C is its ability to extend itself. A C program is basically a
collection of functions that are supported by the C library. We can continuously add our
own functions to C library.
• With the availability of a large number of functions, the programming task becomes
simple.
Basic structure of C programs
The documentation section consists of a set of comment lines giving the
name of the program, the author and other details, which the programmer
would like to use later.
The link section provides instructions to the compiler to link functions from
the system library.
The definition section defines all symbolic constants.
There are some variables that are used in more than one function. Such
variables are called global variables and are declared in the global
declaration section that is outside of all the functions.
Every C program must have one main() function section. This section
contains two parts, declaration part and executable part.
The declaration part declares all the variables used in the executable part.
There is at least one statement in the executable part. These two parts
must appear between the opening and the closing braces. The program
The subprogram section contains all the user- execution begins at the opening brace and ends at the closing brace. The
defined functions that are called in the main closing brace of the main function section is the logical end of the program.
function. User-defined functions are generally All statements in the declaration and executable parts end with a
placed immediately after the main function, semicolon(;).
although they may appear in any order.
Executing a ‘C’ program
Executing a program written in C involves a series of steps.
1. Creating the program
2. Compiling the program
3. Linking the program with functions that are needed from the
C library
4. Executing the program
Character Set
• The characters that can be used to form words, numbers and
expressions depend upon the computer on which the program is run.
• The characters in C are grouped into the following categories:
• Letters
• Digits
• Special characters
• White spaces
Tokens in C
• In a passage of text, individual words and punctuation marks are called tokens.
• A C program consists of various tokens and a token is either a keyword, an
identifier, a constant, a string literal, or a symbol. For example, the following C
statement consists of five tokens −
• printf("Hello, World! \n");
The individual tokens are −
printf
(
"Hello, World! \n"
)
;
Semicolons
• In a C program, 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");
• c=a+b;
Comments
• Comments are like helping text in your C program and they are
ignored by the compiler. They start with /* and terminate with the
characters */ as shown below −
/* my first program in C */
• Single line comment : they start with //
• //my first program
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 int struct double
Identifiers
• A 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.
Thus, Manpower and manpower are two different identifiers in C.
Here are some examples of acceptable identifiers −
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
Rules for Identifiers
• First character must be an alphabet (or underscore).
• Must consist of only letters, digits or underscore.
• Only first 31 characters are significant.
• Cannot use a keyword.
• Must not contain white space
Constants
• Constants in C refer to fixed values that do not change during the
execution of a program.
• C supports several types of constants as illustrated in diagram
Backslash character constants
• C supports some special backslash character constants that are used
in output functions.
C - Variables
• A variable is a data name that may be used to store a data value.
• A variable name can be chosen by the programmer in a meaningful
way so as to reflect its function or nature in the program.
• Some examples of such names are:
• Average
• height
• Total
• Counter_1
• class_strength
• variable names may consist of letters, digits, and the underscore(_) character,
subject to the following conditions:
• They must begin with a letter.
• Some systems permit underscore as the first character.
• ANSI standard recognizes a length of 31 characters. However, length should not be
normally more than eight characters, since only the first eight characters are treated as
significant by many compilers. (In C99, at least 63 characters are significant.)
• Uppercase and lowercase are significant. That is, the variable Total is not the same as
total or TOTAL.
• It should not be a keyword.
• White space is not allowed.
Some valid declarations are shown here
• int i, j, k;
• char c, ch;
• float f, salary;
• double d;
• The line int i, j, k; declares and defines the variables i, j, and k; which
instruct the compiler to create variables named i, j and k of type int.
• Variables can be initialized (assigned an initial value) in their
declaration. The initializer consists of an equal sign followed by a
constant expression as follows −
• type variable_name = value;
• Some examples are −
• int d = 3, f = 5; // definition and initializing d and f.
• char x = 'x'; // the variable x has the value 'x'.
Whitespace 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.
• Therefore, in the following statement −
• int age;
there must be at least one whitespace character (usually a space) between int
and age for the compiler to be able to distinguish them.
On the other hand, in the following statement −
• fruit = apples + oranges; // get the total fruit
no whitespace characters are necessary between fruit and =, or between = and
apples, although you are free to include some if you wish to increase
readability.
C - Data Types
• Data types in c refer to an extensive system used for declaring
variables or functions of different types.
• The type of a variable determines how much space it occupies in
storage and how the bit pattern stored is interpreted.
• ANSI C supports three classes of data types:
• Primary (or fundamental) data types
• Derived data types
• User-defined data types
Primary Datatypes
• All C compilers support five fundamental data types, namely integer
(int), character (char), floating point (float), double-precision floating
point (double) and void.
Derived Datatypes
• Derived Datatypes are composed of fundamental datatypes;
• They are derived from the fundamental data types.
• Therefore, they have some additional characteristics and properties
other than that of fundamental data types.
• Programmers can modify or redefine the derived datatypes.
• Some common examples of derived datatypes include Arrays,
Functions, Pointers, etc.
User-defined datatypes
• User-defined data types are created by the user using a combination of
fundamental and derived data types in the C programming language.
• Some common examples of User-defined datatypes include Structure,
Union, Typedef, enum
Declaration of Storage Class
• Variables in C can have not only data type but also storage class that
provides information about their location and visibility.
• The storage class decides the portion of the program within which the
variables are recognized.
• Consider the following example:
The variable m which has been declared before the main is
called global variable
It can be used in all the functions in the program.
It need not be declared in other functions.
A global variable is also known as an external variable.
The variables i, balance and sum are called local variables
because they are declared inside a function.
Local variables are visible and meaningful only inside the
functions in which they are declared. They are not known to
other functions.
Note that the variable i has been declared in both the
functions. Any change in the value of i in one function does not
affect its value in the other.
• C provides a variety of storage class specifiers that can be used to
declare explicitly the scope and lifetime of variables.
• The concepts of scope and lifetime are important only in
multifunction and multiple file programs and therefore the storage
classes are considered in detail later when functions are discussed.
• For now, remember that there are four storage class specifiers
(auto, register, static, and extern)
Automatic
• Automatic variables are allocated memory automatically at runtime.
• The visibility of the automatic variables is limited to the block in
which they are defined.
• The scope of the automatic variables is limited to the block in which
they are defined. The automatic variables are initialized to garbage by
default.
• The memory assigned to automatic variables gets freed upon exiting
from the block.
• The keyword used for defining automatic variables is auto.
• Every local variable is automatic in C by default.
Static
• The variables defined as static specifier can hold their value between
the multiple function calls.
• Static local variables are visible only to the function or the block in
which they are defined.
• A same static variable can be declared many times but can be
assigned at only one time.
• Default initial value of the static integral variable is 0 otherwise null.
• The visibility of the static global variable is limited to the file in which
it has declared.
• The keyword used to define static variable is static.
Output:
0 0 0.000000 (null)
Register
• The variables defined as the register is allocated the memory into the CPU
registers depending upon the size of the memory remaining in the CPU.
• We can not dereference the register variables, i.e., we can not use &operator for
the register variable.
• The access time of the register variables is faster than the automatic variables.
• The initial default value of the register local variables is 0.
• The register keyword is used for the variable which should be stored in the CPU
register. However, it is compilers choice whether or not; the variables can be
stored in the register.
• We can store pointers into the register, i.e., a register can store the address of a
variable.
• Static variables can not be stored into the register since we can not use more
than one storage specifier for the same variable.
External
• The external storage class is used to tell the compiler that the variable defined as
extern is declared with an external linkage elsewhere in the program.
• The variables declared as extern are not allocated any memory. It is only
declaration and intended to specify that the variable is declared elsewhere in the
program.
• The default initial value of external integral type is 0 otherwise null.
• We can only initialize the extern variable globally, i.e., we can not initialize the
external variable within any block or method.
• An external variable can be declared many times but can be initialized at only
once.
• If a variable is declared as external then the compiler searches for that variable to
be initialized somewhere in the program which may be extern or static. If it is not,
then the compiler will show an error.
C library functions
• Library functions are built-in functions that are grouped together and
placed in a common location called library.
• Each function here performs a specific operation. We can use this
library functions to get the pre-defined output.
• All C standard library functions are declared by using many header
files.
• These library functions are created at the time of designing the
compilers.
• We include the header files in our C program by
using #include<filename.h>. Whenever the program is run and
executed, the related files are included in the C program.
Header File Functions
• Some of the header file functions are as follows −
• stdio.h − It is a standard i/o header file in which Input/output
functions are declared
• conio.h − This is a console input/output header file.
• string.h − All string related functions are in this header file.
• stdlib.h − This file contains common functions which are used in the C
programs.
• math.h − All functions related to mathematics are in this header file.
• time.h − This file contains time and clock related functions.Built
functions in stdio.h
fopen()
Built functions in stdio.h 8 All file handling functions are defined in stdio.h header file.
fclose()
printf() 9 Closes an opened file.
This function is used to print the all char, int, float, string etc., values onto the
1 output screen. getw()
10 Reads an integer from file.
scanf() putw()
2 This function is used to read data from keyboard. 11 Writes an integer to file.
getc() fgetc()
3 It reads character from file. 12 Reads a character from file.
putc()
gets() 13 Writes a character to file.
4 It reads line from keyboard.
fputc()
getchar() 14 Writes a character to file.
5 It reads character from keyboard.
fgets()
15 Reads string from a file, one line at a time.
puts()
6 It writes line to o/p screen.
f puts()
16 Writes string to a file.
putchar()
7 It writes a character to screen. feof()
17 Finds end of file.
18 fgetchar
Reads a character from keyboard. 28 rewind()
Moves file pointer to the beginning of the file.
19 fgetc()
Reads a character from file. 29 putc()
Writes a character to file.
20 fprintf()
Writes formatted data to a file.
30 sprint()
21 fscanf() Writes formatted output to string.
Reads formatted data from a file.
31 sscanf()
22 fputchar Reads formatted input from a string.
Writes a character from keyboard.
32 remove()
23 fseek()
Deletes a file.
Moves file pointer to given location.
24 SEEK_SET 33 flush()
Moves file pointer at the beginning of the file. Flushes a file.
25 SEEK_CUR
Moves file pointer at given location.
26 SEEK_END
Moves file pointer at the end of file.
27 ftell()
Gives current position of file pointer. https://www.tutorialspoint.com/what-are-the-c-library-
functions
Data Output Formatting
• To display any message or value on output screen, then we use printf()
• printf and PRINTF are not the same.
• In C, everything is written in lowercase letters.
• Syntax : printf(“Your message here”);
• Example : printf(“Hello World”);
• To print the value of variable : printf(“Control string”, name of variable);
• Example : printf(“%d”,a);
Data Input Formatting
• Another way of giving values to variables is to input data through
keyboard using the scanf function.
• The general format of scanf is as follows:
• The control string contains the format of data being received.
• The ampersand symbol & before each variable name is an operator
that specifies the variable name’s address.
• We must always use this operator, otherwise unexpected results may
occur.
Example
scanf(“%d”, &number);
• When this statement is encountered by the computer, the execution
stops and waits for the value of the variable number to be typed in.
Since the control string “%d” specifies that an integer value is to be
read from the terminal, we have to type in the value in integer form.
• Once the number is typed in and the ‘Enter’ Key is pressed, the
computer then proceeds to the next statement.
• Thus, the use of scanf provides an interactive feature and makes the
program ‘user friendly’.
• The value is assigned to the variable number.
References
• Programming in ANSI C – Balaguruswamy
• https://www.tutorialspoint.com/what-are-the-c-library-functions
Thank You !!!!