Professional Documents
Culture Documents
Programming in C
History of C Programming Language
C language is the middle – level programming language. The C language was developed at AT&T Bell
Laboratories, USA by Denis Ritchie in 1972. This is general-purpose language.
Before the development of C language there were many languages that were developed, but these
languages served some specific purpose. For example, for commercial applications, where lot of data are
needed to be stored and retrieved, COBOL is used; for Engineering and Scientific applications, which
needs more calculating power than data storage and retrieval facilities, FORTRAN is used, etc. There was
a need for a common language for all these purposes. “C” came into existence, after the development of
many languages like ALGOL 60, CPL, BCPL, B, and many others, which didn’t actually serve the purpose
and were a specific purpose language.
C as a programming language is sometimes referred to as a middle-level language. This is because it
provides much flexibility and ease for the programmer as provided by any high-level language and the
same time it provides and supports certain features and concepts that are necessary for good system (low-
level) programming.
printf Function – The printf function is used to print out a message either on screen or paper. The syntax
of printf function is as follows:
printf(“text to be printed”);
When printf is used, it means that printing function is to be called for action. The statement or texts used in
quotes are called the control strings.
newline Character \n – The newline symbol instructs the computer to advance to the next new line before
printing subsequent information. The printf function does not do this automatically.
Semicolon ; - The end of any statement in C Programming is denoted by a semicolon. In C, all statements
are terminated with a semicolon.
Braces – Braces are used at the start as well as at the end of the program. The open brace { is used to begin
a program, just below main() function. The alignment of the braces on either side is not strictly
necessary but it makes the program more readable. The closed brace } is used to denoted end of a
program.
Comments – The comment demonstrates the method used in C to add a comment or remark to a program.
This is done by beginning the comment with the two characters /* and ending it with the characters
*/. No space can be included between the two characters. Between these pairs of characters
(delimiters), any characters may be included either in upper or lowercase. In other words, a
comment appears as follows:
/* Whatever you want using any characters at all */
The only restriction is that a comment nay not contain the closing delimiter */ as a part of the comment’s
text. All such comments are ignored by the compiler ad have no effect on the way the program runs. They
included because they serve as an excellent way of internally documenting the programs. When a program
is printed out, those comments appear along with the instructions. Comments may start on the same line as
another statement or on a line of its own. It may also extend over any number of lines if the terminating
delimiter is placed at the appropriate point.
What are Constants? Give types and rules for each one of them.
Constant is a quantity that doesn’t change and can be stored at any location in the memory of the computer.
These quantities may be combinations of alphabets, numbers or special symbols in C.
eg: 2y=30, where 2 and 30 are constants i.e., their value cannot be changed.
C Constants can be divided into two categories:
1. Primary constants
2
2. Secondary constants
Primary constants are as follows:
i. Integer Constants – An integer constant refers only to numbers. It may be a single digit or combination
of digits. Generally, integer constants are written in decimal number system, which consist of
combination of digits from 0-9.
Rules for constructing Integer constants.
a) An integer constant must contain of at least one digit.
b) It should not contain decimal values.
c) The numbers could either be positive or negative.
d) It does not allow commas or blank space.
e) The range of integer constant is –32768 to +32767
ii. Float Constants – A float constant refers to numbers that contains a decimal point or an exponent or
both. The float constants are often known as real constants that could be written in two forms: fractional
or exponential.
Rules for constructing Float constants
a) A float constant must consist of at least one digit.
b) It should have decimal values
c) The numbers could either be positive or negative
d) It does not allow commas or blank spaces
e) A letter ‘e’ should separate the mantissa part and the exponent part. For example, 301224x10-17 would
be represented as 301224 e –17 in float constants.
f) The range of real constants is much greater than integer constants, which is expressed as –3.4e38 to
3.4e38.
iii. Character Constants – A character constant is defined as a single alphabet, a single digit or a special
symbol enclosed within single quotes (‘r’).
Rules for constructing Character constants
a) The maximum length of character constant can be 1 character.
b) The quotes within the character is enclosed must point to the left as ‘a’.
c) The characters constant have integer values determined by computer’s ASCII character set.
d) The range of character constant is –128 to +127.
(b) Formatted I/O functions – These functions are used to format the input or output of data as
per the user requirements. The major difference between unformatted and the formatted I/O
functions is that in the latter user can format the input or the output displayed according to
his own requirement. For example, to display the output on which part of screen, how many
spaces to be present between two values or any other thing that you require to be displayed
can be controlled by using the formatted I/O functions.
(1) The input function - scanf( )
This function allows you to read data from keyboard that can be formatted accordingly. This input data
can be arranged in a particular format. Each scanf( ) statement is ended with a semicolon. This function
generally is written as:
scanf(“format string”, &a1, &a2, &a3, ………………&an);
where control string refers to the field format in which data is to be entered and a1, a2, ……….,an are
the arguments that represent the individual input data items.
scanf( ) function is used for input of the data.
(2) The output function – printf( )
This function is used to display plain messages to the user. The same function can be used to display
the value of the variables you are using in your program. The syntax of printf( ) function is:
printf(“format string”, variable list);
If you want to display a simple message to the use, you can use the function like:
printf(“your message”);
Everything that is enclosed in the double quotes will be displayed to the user on the screen as it is.
The if Statement – The instructions in a program are executed sequentially. If we want to alter the
sequence we can do it with the use of decision control statement in C. The if statement allows decision to
be made by evaluating a given condition is true or false. These conditions involve both the comparison and
operators. The keyword if is followed with the involve both the comparison and logical operators. The
keyword if is followed with the conditions and statement next to it are executed only and only if the
condition is true. If the condition is not true, the
statement next to it are skipped. The syntax of if statement is:
if (condition)
statement;
If there is more than one statement to be executed in case the condition is true, than
You would be required to include all such statement in a pair of curly braces as
Shown below:
If (condition)
{
Statement
}
Failure of doing so will result in the execution of just the immediate statement after the if condition.
Rest of the statement will become ineffective form the if claus.
For example, to check the identity of a person named Mahesh and display an
Appropriate message, we use the if condition as follow:
If (identity = = 1024)
{
printf(“Found Mahesh”);
}
The if-else statement – The statement enclosed within the curly after the condition of the if clause
would be executed only if the condition of is true. If you want to execute some statement if the condition is
false, you can do this using else i.e., if condition is false the control moves to statement following else. The
if-else statement helps reduce the complexity of a program to some extent and also improves its
readability. The syntax of if-else statement is:
if (condition)
{
statement;
}
else
{
statement;
}
For example, to find whether the number is odd or even
if (number%2 = = 0)
{
printf(“the number is even);
}
else
{
printf(“the number is odd”);
8
}
The if-else statements can also use the following syntax:
(condition)? Statement1: Statement2
in this declaration, if the condition is true statement1 will be performed and if it is false the else part which
is separated with a colon : will be performed.
Thus the above example will be done in this way:
(number % 2 = = 0)? printf(“the number is even “): printf(“the number is odd”);
? and : are together known as conditional operator. They are also referred to as ternary operator.
Nested if - When we combine several if statements, it is referred to as nested if statement. This improves
the flexibility in programming. In these statements the control is moved to the next if statement only if the
condition is true and after performing the first statement the next if condition is checked and if this is also
true, then the second statement is performed. In case, the first condition is false, the control skips the
following statements of both if conditions and moves to the else part, if specified. The syntax of Nested-if
statement is:
if (condition 1)
{
statement 1;
if (condition 2)
{
statement 2;
}
}
For example, to check if the identity of the person is 1024, display an appropriate message and then check
if this age is greater than 25 and then display another message. If you make the program as shown below:
if (identity = = 1024)
{
printf(“found match”);
}
if (age> 25)
{
printf (“age matched”);
}
here condition for age is checked separately from that of identity. In this case, if the age greater than 25
and the identity is not equal to 1024, then also the message is displayed. But actually it is required that the
age verification should be checked only if the identity verification is true. The solution to this problem can
be done through nested if as shown below:
The syntax of the nested if-else statement is:
If (condition 1)
{
statement 1;
if (condition 2)
{
statement 2;
else
statement 3;
}
else
statement 4;
}
Input- John
Memory representation of the above example is
Initializing a String
char name[] = “John”;
‘\0’ is not input by the user as C inserts the null character automatically. This indicates one thing that a
string always takes one character more and hence one more byte in the memory. The null character helps
in this way:
main ( )
{
15
char name[] = “John”;
for (i = 0; i <= 3; i + +)
{
printf(“%c”, name[i]);
}
String library functions are the functions which are used regularly and stored in a library file and whenever
these functions are needed, you need to include the required header file (string.h) in your program to be
able to use these functions. Some of the string library functions are:
Strlen( ) function – This function is used to count the number of characters present in a string. It calculates
the length of the string. The syntax of this function is:
strlen(array-name)
This function does not perform when the parenthesis are enclosed with some string but calculates the
length if the base address is passed within it.
Strcpy( ) function – This function copies the contents of one string into another. The syntax of this function
is:
strcpy(target, source)
This function performs only when it supplies the base address of the source and target string.
Strcat( ) function – This function concatenates (to link together) the source string at the end of the target
string. It appends one string to another. The syntax of this function is
strcat(destination, source)
Strcmp( ) function – This function compares two strings to find out whether they are same or not. The two
strings are compared character by character until there is a mismatch or end of the string, which indicates
similar strings. This function returns a value zero if both strings are identical. The syntax of this function is
:
strcmp( string 1, string 2)
Arguments represent information being passed between the functions. Any number of arguments can
be passed to a function. Arguments passed to function from the called function are just name of the
variable that the calling function have. These are called actual arguments .To receive the values passed
by the caller function, the called function must be declare same types of variable in the brackets just
after the function name. In this context the variables are known as formals arguments. The variable
names between the actual and the formal arguments may differ, but the type and /or order in which
they are displayed and the number of actual and formal arguments must always be the same. If they
differ, the program will display an error while compiled.
For e.g. , to create an add function, that performs the addition operation the function could be written as
below:
void main (void)
{
int add(int a, int b);
int a,b,res;
printf(“enter two integers:”);
scanf(“%d %d”,&a,&b);
res=add(a,b);
printf(“the addition of %d and %d is %d”,a,b,res);
}
add(int x,int y)
{
int sum;
sum= x+y;
18
return(sum);
}
In the above example, you can see that function has declared as to return an integer value by
mentioning its data type before its name while declaring. To parameters or arguments of data type
integer with variable names a & b are passed.
While defining the functions add, again the same data types as parameter are passed. In this a & b are
actual arguments where x & y are formal arguments. The value of a gets assigned to x and the value of
b to y. This also makes clears that the order of parameters does matter i.e., first variable while
declaring, assigns the value to the first variable while defining. Thus, if we have written add (int y, int
x) while defining, the value of a would had been assigned to y and not to x. It is also necessary that
value being passed back must be of the same type as specified in the declaration.
Struct employee
{
char name[10];
char address[25];
float salary;
};
main( )
{
void emp(struct employee);
struct employee e1 = {“Tom”, “15/2 New York”, 500215};
emp(e1);
}
void emp(struct employee e1)
{
printf(“%s%s%f”, e1.name, e1.address, e1.salary);
}
In other words, structure is declared global so that it becomes known to all functions in the program.
22
Variable name a b
Values 10 4002
Memory location 4002 6008
Thus, you can see that the address of ‘a’ is stored in ‘b’ which itself is stored at another location i.e. 6008.
You need to declare the variable ‘b’ before you use it. But, as this would not be an ordinary variable but ‘a’
variable containing the address of some other variable so this would be declared as a pointer, which points
to some other’s variable address. As, int *b;
Thus, this expression means that if printed, it would display the value of the address of the variable it
points i.e., 4002 because it points to location 4002 which consists of value 10.