2. MCA 101
Programming in with Data Structure
UNIT I
Fundamentals of C Programming :
Structure of a C Program, Data types, Identifiers and keywords,
Operators & expressions, Preprocessor directive, Input output,
Casting, Precedence, Scope of variables Control Constructs and
Iteration Constructs Functions: Defining and accessing: passing
arguments, Function prototypes, Recursion, Storage classes
3. Structure of a C Program
To find the sum of two numbers given by the user
It is given by:
/* Sum of two numbers */
#include<stdio.h>
int main()
{
int a, b, sum;
printf("Enter two numbers to be added ");
scanf("%d %d", &a, &b);
// calculating sum
sum = a + b;
printf("%d + %d = %d", a, b, sum);
return 0; // return the integer value in the sum
}
* conio.h is a C header file used mostly by MS-DOS
compilers to provide console input/output. It is not part
of the C standard library or ISO C, nor is it defined by
POSIX. This header declares several useful library
functions for performing "istream input and output" from
a program.
6. Data Types in C
• A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
7. Basic Data Types
• The basic data types are integer-based and floating-point based. C
language supports both signed and unsigned literals.
• The memory size of the basic data types may change according to 32
or 64-bit operating system.
• Let's see the basic data types. Its size is given according to 32-bit
architecture.
8.
9.
10. C Identifiers
• C identifiers represent the name in the C program, for example,
variables, functions, arrays, structures, unions, labels, etc. An
identifier can be composed of letters such as uppercase, lowercase
letters, underscore, digits, but the starting letter should be either an
alphabet or an underscore. If the identifier is not used in the external
linkage, then it is called as an internal identifier. If the identifier is
used in the external linkage, then it is called as an external identifier.
11.
12. • We can say that an identifier is a collection of alphanumeric
characters that begins either with an alphabetical character or an
underscore, which are used to represent various programming
elements such as variables, functions, arrays, structures, unions,
labels, etc. There are 52 alphabetical characters (uppercase and
lowercase), underscore character, and ten numerical digits (0-9) that
represent the identifiers. There is a total of 63 alphanumerical
characters that represent the identifiers.
13. Rules for constructing C identifiers
• The first character of an identifier should be either an alphabet or an
underscore, and then it can be followed by any of the character, digit, or
underscore.
• It should not begin with any numerical digit.
• In identifiers, both uppercase and lowercase letters are distinct. Therefore,
we can say that identifiers are case sensitive.
• Commas or blank spaces cannot be specified within an identifier.
• Keywords cannot be represented as an identifier.
• The length of the identifiers should not be more than 31 characters.
• Identifiers should be written in such a way that it is meaningful, short, and
easy to read.
14. Example of valid identifiers
1.total, sum, average, _m _, sum_1, etc.
Example of invalid identifiers
1.2sum (starts with a numerical digit)
2.int (reserved word)
3.char (reserved word)
4.m+n (special character, i.e., '+')
15. Types of identifiers
• Internal Identifier
• If the identifier is not used in the external linkage, then it is known as
an internal identifier. The internal identifiers can be local variables.
• External Identifier
• If the identifier is used in the external linkage, then it is known as an
external identifier. The external identifiers can be function names,
global variables.
16.
17. Let's understand through an example.
int main()
{
int a=10;
int A=20;
printf("Value of a is : %d",a);
printf("nValue of A is :%d",A);
return 0;
}
18. Output:
• Value of a is : 10
• Value of A is :20
• The above output shows that the values of both the variables, 'a' and
'A' are different. Therefore, we conclude that the identifiers are case
sensitive.
19. Keywords in C
• A keyword is a reserved word. You cannot use it as a variable name,
constant name, etc. There are only 32 reserved words (keywords) in
the C language.
• A list of 32 keywords in the c language is given below:
21. // C program to demonstrate
// auto keyword
#include <stdio.h>
int printvalue()
{
auto int a = 10;
printf("%d", a);
}
// Driver code
int main()
{
printvalue();
return 0;
}
22. C Operators:
An operator is simply a symbol that is used to perform operations. There
can be many types of operations like arithmetic, logical, bitwise, etc.
• There are following types of operators to perform different types of
operations in C language.
• Arithmetic Operators
• Relational Operators
• Shift Operators
23. C Operators:
• Logical Operators
• Bitwise Operators
• Ternary or Conditional Operators
• Assignment Operator
• Misc Operator
24. Precedence of Operators in C
• The precedence of operator species that which operator will be
evaluated first and next. The associativity specifies the operator
direction to be evaluated; it may be left to right or right to left.
• Let's understand the precedence by the example given below:
int value=10+20*10;
• The value variable will contain 210 because * (multiplicative operator)
is evaluated before + (additive operator).
• The precedence and associativity of C operators is given below:
25.
26.
27.
28.
29. C - Preprocessors
• The C Preprocessor is not a part of the compiler, but is a separate step
in the compilation process. In simple terms, a C Preprocessor is just a
text substitution tool and it instructs the compiler to do required pre-
processing before the actual compilation. We'll refer to the C
Preprocessor as CPP.
• All preprocessor commands begin with a hash symbol (#). It
must be the first nonblank character, and for readability, a
preprocessor directive should begin in the first column. The
following section lists down all the important preprocessor
directives −
30.
31.
32.
33.
34. C Input Output (I/O)
• In C programming, printf() is one of the main output function. The
function sends formatted output to the screen. For example,
35. How does this program work?
• All valid C programs must contain the main() function. The code
execution begins from the start of the main() function.
• The printf() is a library function to send formatted output to the
screen. The function prints the string inside quotations.
• To use printf() in our program, we need to include stdio.h header file
using the #include <stdio.h> statement.
• The return 0; statement inside the main() function is the "Exit status"
of the program. It's optional.
36.
37.
38. C Input
• In C programming, scanf() is one of the commonly used function to
take input from the user. The scanf() function reads formatted input
from the standard input such as keyboards.
39.
40.
41. C - Type Casting
• Converting one datatype into another is known as type casting
or, type-conversion. For example, if you want to store a 'long'
value into a simple integer then you can type cast 'long' to 'int'.
You can convert the values from one type to another explicitly
using the cast operator as follows −
Without Type Casting:
int f= 9/4;
printf("f : %dn", f );//Output: 2
With Type Casting:
float f=(float) 9/4;
printf("f : %fn", f );//Output: 2.250000
42. Type Casting example
Let's see a simple example to cast int value into the float.
#include<stdio.h>
int main(){
float f= (float)9/4;
printf("f : %fn", f );
return 0;
}
Output:
f : 2.250000
43.
44. Operator precedence
• Operator precedence determines the grouping of terms in an
expression and decides how an expression is evaluated.
Certain operators have higher precedence than others; for
example, the multiplication operator has a higher precedence
than the addition operator.
• For example, x = 7 + 3 * 2; here, x is assigned 13, not 20
because operator * has a higher precedence than +, so it first
gets multiplied with 3*2 and then adds into 7.
45. Operator precedence
• For example, x = 7 + 3 * 2; here, x is assigned 13, not 20
because operator * has a higher precedence than +, so it first
gets multiplied with 3*2 and then adds into 7.
• Here, operators with the highest precedence appear at the top
of the table, those with the lowest appear at the bottom.
Within an expression, higher precedence operators will be
evaluated first.
46.
47. • When you compile and execute the above program, it produces the
following result −
• Value of (a + b) * c / d is : 90
• Value of ((a + b) * c) / d is : 90
• Value of (a + b) * (c / d) is : 90
• Value of a + (b * c) / d is : 50
48. C variable scope
• In C programming, every variable is defined in scope. You can define scope as the
section or region of a program where a variable has its existence; moreover, that
variable cannot be used or accessed beyond that region. In C programming, a
variable declared within a function differs from a variable declared outside of a
function. The variable can be declared in three places. These are:
49. Local Variables
• Variables that are declared within the function block and can be used
only within the function are called local variables.
• All these locally scoped statements are written and enclosed
within the left ({) and right (}) curly braces. C also has a provision
for nested blocks, which means that a block or function can
occur within another block or function. So it means that
variables declared within a block can be accessed within that
specific block and all other internal blocks of that block but
cannot be accessed outside the block.
50.
51. Global Variables
• In most cases, global variables are defined outside a function or any
specific block on top of the C program. These variables hold their
values all through the end of the program and are accessible within
any of the functions defined in your program. Any function can access
variables defined within the global scope, i.e., its availability stays for
the entire program after being declared.
52.
53. Function prototype
• A function prototype is simply the declaration of a function that
specifies function's name, parameters and return type. It doesn't
contain function body.
• A function prototype gives information to the compiler that the
function may later be used in the program.
56. • Passing arguments to a function
• In programming, argument refers to the variable passed to the
function. In the above example, two variables n1 and n2 are passed
during the function call.
• The parameters a and b accepts the passed arguments in the function
definition. These arguments are called formal parameters of the
function.
57. The type of arguments passed to a
function and the formal parameters
must match, otherwise, the compiler
will throw an error.
If n1 is of char type, a also should be
of char type. If n2 is of float type,
variable b also should be of float type.
A function can also be called without
passing an argument.
58. Return Statement
• Return Statement
• The return statement terminates the execution of a function and
returns a value to the calling function. The program control is
transferred to the calling function after the return statement.
• In the above example, the value of the result variable is returned to
the main function. The sum variable in the main() function is assigned
this value.
59.
60. Parameter Passing Techniques in C/C++
• There are different ways in which parameter data can be passed into
and out of methods and functions. Let us assume that a function B()
is called from another function A(). In this case A is called the “caller
function” and B is called the “called function or callee function”. Also,
the arguments which A sends to B are called actual arguments and
the parameters of B are called formal arguments.
61. Terminology
• Formal Parameter : A variable and its type as they appear in the prototype
of the function or method.
• Actual Parameter : The variable or expression corresponding to a formal
parameter that appears in the function or method call in the calling
environment.
• Modes:
• IN: Passes info from caller to callee.
• OUT: Callee writes values in caller.
• IN/OUT: Caller tells callee value of variable, which may be updated by
callee.
62. Important methods of Parameter Passing
• Pass By Value: This method uses in-mode semantics. Changes made
to formal parameter do not get transmitted back to the caller. Any
modifications to the formal parameter variable inside the called
function or method affect only the separate storage location and will
not be reflected in the actual parameter in the calling environment.
This method is also called as call by value.
63.
64.
65.
66. • Pass by reference(aliasing): This technique uses in/out-mode
semantics. Changes made to formal parameter do get transmitted
back to the caller through parameter passing. Any changes to the
formal parameter are reflected in the actual parameter in the calling
environment as formal parameter receives a reference (or pointer) to
the actual data. This method is also called as call by reference. This
method is efficient in both time and space.
67.
68.
69.
70. Recursion
• Recursion is the technique of making a function call
itself. This technique provides a way to break
complicated problems down into simple problems which
are easier to solve.
• Recursion may be a bit difficult to understand. The best
way to figure out how it works is to experiment with it.
72. Example Explained
• When the sum() function is called, it adds parameter k to the sum of
all numbers smaller than k and returns the result. When k becomes 0,
the function just returns 0. When running, the program follows these
steps
• Since the function does not call itself when k is 0, the program
stops there and returns the result.
73.
74. C - Storage Classes
• A storage class defines the scope (visibility) and life-time of
variables and/or functions within a C Program. They precede
the type that they modify. We have four different storage
classes in a C program −
• auto
• register
• static
• extern
75. The auto Storage Class
The auto storage class is the default storage class for all local variables.
{
int mount;
auto int month;
}
The example above defines two variables with in the same storage
class. 'auto' can only be used within functions, i.e., local variables.
76. The register Storage Class
The register storage class is used to define local variables that should
be stored in a register instead of RAM. This means that the variable has
a maximum size equal to the register size (usually one word) and can't
have the unary '&' operator applied to it (as it does not have a memory
location).
{
register int miles;
}
77. The static Storage Class
The static storage class instructs the compiler to keep a local variable in existence during the
life-time of the program instead of creating and destroying it each time it comes into and goes
out of scope. Therefore, making local variables static allows them to maintain their values
between function calls.
The static modifier may also be applied to global variables. When this is done, it causes that
variable's scope to be restricted to the file in which it is declared.
In C programming, when static is used on a global variable, it causes only one copy of that
member to be shared by all the objects of its class.
78.
79.
80. The extern Storage Class
• The extern storage class is used to give a reference of a global
variable that is visible to ALL the program files. When you use
'extern', the variable cannot be initialized however, it points the
variable name at a storage location that has been previously
defined.
• When you have multiple files and you define a global variable
or function, which will also be used in other files,
then extern will be used in another file to provide the reference
of defined variable or function. Just for understanding, extern is
used to declare a global variable or function in another file.
81. • The extern modifier is most commonly used when there are two or
more files sharing the same global variables or functions as
explained below.