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

Fundamentals of ‘C’

Uploaded by

manavjpatel2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Fundamentals of ‘C’

Uploaded by

manavjpatel2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Fundamentals of ‘C’

Features of ‘C’
• Important feature of ‘C’ language is
• It is portable, by portability we mean that the program can
be run on any hardware machine.
• It supports modular programming.
• It supports bit-wise operations.
• It is known as structured programming language.
Character Set
• ‘C’ program basically consists of keywords, identifiers, constants,
operators and some special symbols.
• The characters that can be used in a ‘C’ program are Alphabets (A-Z
and a-z), Digits (0-9), Special Characters and White Space
Characters.

C Tokens
• Keywords
• Identifiers
• Constants
• Strings
• Special symbols {}
• Operators
Keywords
• Keywords are words whose meaning is fixed, as a programmer
we can not change its meaning.
• Keywords are also known as reserved words. The keywords
must be in second alphabet.
• Some of the keywords are: int, void, double, for, while, switch,
goto, if etc.
Identifiers
• Identifiers are user defined.
• Identifiers can be the variables, symbolic constants, functions,
procedures.
• The name of identifier should be meaningful so that the
maintenance of the program becomes easy.
• The rules for naming an identifier are
 The first character must be an alphabet.
 Underscore ( ‘_’) is valid letter.
 Reserved words i.e. keywords can not be used as
identifiers.
 Identifier names are case sensitive.
Write a program to print the message Hello !

#include <stdio.h>
void main()
{
printf(“Hello !”);
}
Write a program to print the message
Hello
World !

/* this program developed by XYZ */


#include <stdio.h>

void main()
{ /* start of main() */
printf(“Hello\n World !”); /* use of ‘\n’ character */
} /* end of main() */
Constants
• The ‘C’ language supports following types of constants:
• Numeric constants
• Integer (5, 15)
• Real (3.6, -5.4)
• Non-numeric constants
• Character constants (‘B’ , ‘a’ , ‘?’ , ‘5’ , ‘+’ etc)
• String Constants (“Computer”, “XYZ”, “-5.4” etc)
Variables
• Variables are the identifiers whose value changes as opposite
to constants.
• As variable is an identifier, all the rules for naming an identifier
applies to variables also.
Fundamental data types
Variable declaration
• The syntax for variable declaration is :
datatype var1,var2,….,varn;
• Following are valid declaration of variables.
int sum, count;
float weight, average;
double rho;
char c;
Variable declaration (Cont)
• Assigning value to a variable
weight = 55.5;
sum = 0;

• Assigning value to a variable at the time of a declaration


int sum =0;
int length, count=0;
Input with scanf() function
• The scanf() function is used to get formatted input from standard input
device which is keyboard. The syntax of scanf() function is:
scanf(“control string”, list of addresses of variables);
• For example,
#include <stdio.h>
void main()
{
float b; char c;
printf("Enter value of b: ");
scanf("%f", &b);
printf("Enter single character: ");
scanf("%c", &c);
printf("The value of b='%f'\n", b);
printf("The value of c='%c'\n", c);
}
Input with scanf() function
• in above scanf() function, “%c %d %f” is control string, where
format specifies are preceded by % symbol.
• The first input will be treated as character, second as integer,
and third as float, and will be stored in x, y and z respectively.
• The variables x, y and z are all preceded by & symbol,
because scanf() function requires the address of the variable
in which the value is to be stored.
Output with printf() function
• The syntax of printf() function is:
printf(“Control string”, list of variables);
• For example,
float avg=13.7;
printf(“ Average = %f\n”, avg);
printf(“Over”);
Symbolic Constants
The constant is given a symbolic name and instead of constant
value, symbolic name is used in the program.
 Symbolic constant is defined as
#define symbolic_name value
Example:
#define PI 3.1415
Note that there is no semicolon ‘;’ at the end.
Program with symbolic constant
/* Program to calculate area and circumference of a circle */
#include <stdio.h>
#define PI 3.1415
void main()
{
float rad = 5;
float area, circum;
area = PI * rad *rad;
circum = 2 * PI *rad;
printf("Area of circle = %f\n",area);
printf("Circumference of circle =%f\n",circum);
}
Output
Area of circle = 78.537498
Circumference of circle =31.415001
#include<math.h>

• pow(x,y);
• sqrt(x);
• log(x); // base e
• log10(x);
• sin(x);
• cos (x);
Qualifiers
• In addition to fundamental 4 types of data, ‘C’ language
supports different qualifiers such as signed, unsigned, short
and long.
• These qualifiers can be put before the fundamental data types
to change the range of values supported.
Qualifiers (Cont)
Operators and Expression
Introduction
• The operators are used inside expressions.
• Expression is a formula having one or more operands and may
have zero or more operands.
• The operand can be a variable, constant or a name of a
function. For example, following is an expression involving two
operands a and b with ‘+’ operator.
a+b
Arithmetic Operators
 Arithmetic operators are used to perform arithmetic
operations. ‘C’ language supports following arithmetic
operators.
+, -, *, / and %(modulo division)
 The precedence of arithmetic operators is
* / % first priority
+ - second priority
 If in an expression, the operators having same priority occur,
then the evaluation is from left to right. Sub expressions
written in brackets are evaluated first.
Program explaining Arithmetic operators
#include <stdio.h>
void main()
{
int x=25,y=4;
printf("%d + %d = %d\n",x,y,x+y);
printf("%d - %d = %d\n",x,y,x-y);
printf("%d * %d = %d\n",x,y,x*y);
printf("%d / %d = %d\n",x,y,x/y);
printf("%d %% %d = %d\n",x,y,x%y);
}
Output
25 + 4 = 29
25 - 4 = 21
25 * 4 = 100
25 / 4 = 6
25 % 4 = 1
Program – floating point arithmetic
/* Write a program to convert Fahrenheit temperature to centigrade. This
program explains floating-point arithmetic. The formula is c = 5/9*(f-32). */
#include <stdio.h>
void main()
{
float fahr,cent;
printf("Give the value of temperature in Fahrenheit\n");
scanf("%f",&fahr);
cent = 5*(fahr-32)/9;
printf("Fahrenheit temperature = %f\n",fahr);
printf("Centigrade temperature = %f\n",cent);
}
Output
Give the value of temperature in fahrenhit 100
Fahrenhit temperature = 100.000000
Centigrade temperature = 37.777779
Relational Operators
• In programming, relational operators are used to compare
whether some variable value is higher, equal or lower with
other variable. ‘C’ language provides following relational
operators.
• == (Equals), != (Not Equal to), < (Less than)
> (Greater than), <= (Less than or equal to),
>= (Greater than or equal to)
• The relational expression will have value either True (1) or
False (0).
Logical operators
 We need to take certain action based on outcome of some
conditions. Logical operators help us to combine more than
one condition, and based on the outcome certain steps are
taken.
 ‘C’ language supports following logical operators.
&& (Logical AND) , | | (Logical OR),
! (Logical NOT i.e Negation)
 If more than one operator is used, ! (NOT) is evaluated first,
then && (AND) and then || (OR). We can use parentheses to
change the order of evaluation.
Logical operators (Cont)
• For example, if we have a=2, b=3 and c=5 then
a<b && c==5 is true
a< b || c>10 is true
(b<c || b>a) && (c ==5) is true
! (5>3) is false
Assignment operator
 ‘C’ language supports = assignment operator. It is used to
assign a value to a variable. The syntax is: variablename =
expression;
 In an expression involving arithmetic operators, assignment
operator has the least precedence
 ‘C’ language also supports the use of shorthand notation also.
For example, the statement a=a+5 can be written using
shorthand notation as a += 5. So, shorthand notation can be
used for any statement whose form is
varname = varname operator expression;
into
varname operator= expression;
Program using shorthand notations
#include <stdio.h>
void main()
{
int a;
printf("Give the value of a\n");
scanf("%d",&a);
a += 5; /* a= a +5 */
printf("a= %d\n",a);
a -= 5; /* a = a-5 */
printf("a= %d\n",a);
a *=5; /* a = a* 5 */
printf("a= %d\n",a);
a /=5; /* a= a /5 */
printf("a= %d\n",a);
a %= 5; /* a= a %5 */
printf("a= %d\n",a);
}
Program using shorthand notations
(Cont)
Output
-----------------------------------------------------------------------------------
Give the value of a
4
a= 9
a= 4
a= 20
a= 4
a= 4
-----------------------------------------------------------------------------------
Increment/Decrement operators
• ‘C’ language provides special operators
• ++ (increment operator) and -- (decrement operator)
• If a is a variable, then
++a is called prefix increment
a++ is called postfix increment.
Similarly, --a is called prefix decrement
a-- is called postfix decrement.
Conditional Operator (Ternary operator)

 ‘C’ language provides ? operator as a conditional operator. It


is also known as a ternary operator. It is used as shown
below.
expr1 ? expr2 :expr3;
where expr1 is a logical expression, logical expression can be
TRUE or FALSE. expr2 and expr3 are expressions.
 expr1 is evaluated first, and depending on its value, either
expr2 or expr3 is evaluated. If expr1 is TRUE, then expr2 is
executed, otherwise expr3 is executed.
Program –Ternary operator
/* Write a program to find maximum and minimum of two numbers using ternary ? operator */
#include <stdio.h>
void main()
{
int x,y,max,min;
printf("Give two integer numbers\n");
scanf("%d%d",&x,&y);
max = (x>y) ? x: y;
min = (x<y) ? x: y;
printf("maximum of %d and %d is = %d\n",x,y,max);
printf("minimum of %d and %d is = %d\n",x,y,min);
}
Output
Give two integer numbers 3 6
maximum of 3 and 6 is = 6
minimum of 3 and 6 is = 3
Bit-wise operators
 ‘C’ language supports some operators which can perform at
the bit level. These type of operations are normally done in
assembly or machine level programming. But, ‘C’ language
supports bit level manipulation also, that is why ‘C’ language
is also known as middle-level programming language.
 Bit-wise operators are:
& (Bit-wise AND) , | (Bit-wise OR),
^ ( Bit-wise Exclusive OR (XOR)), << (Left Shift) , >> (Right
Shift), ~ (Bit-wise 1’s complement)
Program –Bit wise operators
/* Write a program to multiply and divide the given number by 2 using bit-wise operators << and >> */
#include <stdio.h>
Void main()
{
int x;
int mul,div;
printf("Give one integer number\n");
scanf("%d",&x);
mul = x << 1; /* left shift */
div = x >> 1; /* right shift */
printf("multiplication of %d by 2 = %d\n",x,mul);
printf("division of %d by 2 = %d\n",x,div);

}
Output
---------------------------------------------------------------------------------------------------------
Give one integer number 8
multiplication of 8 by 2 = 16
division of 8 by 2 = 4
---------------------------------------------------------------------------------------------------------
Program –Bit wise operators
/* Write a program to multiply and divide the given number by 2 using bit-wise operators << and >> */
#include <stdio.h>
main()
{
int x;
int mul,div;
printf("Give one integer number\n");
scanf("%d",&x);
mul = x << 2; /* left shift */
div = x >> 2; /* right shift */
printf("%d",mul);
printf("%d",div);

}
Output
---------------------------------------------------------------------------------------------------------
Give one integer number
8
32
2
---------------------------------------------------------------------------------------------------------
Other opeartors
• sizeof operator is used to find out the storage requirement of
an operand in memory. It is an unary operator, which returns
the size in bytes.
Type casting
• When user needs the type conversion explicitly, then type
casting is used.
• Type conversion is automatic while type casting is explicitly
specified by the programmer in the program.
• The type casting can be specified in following form
(typename) expression;
Program - Type casting
/* Program demonstrating type casting */
#include <stdio.h>
void main()
{
int sum =47;
int n = 10;
float avg;
avg = sum/n; /*avg without type cast */
printf("avg=sum/n = %f\n",avg);
avg = (float)sum/n; /*avg with type cast on sum */
printf("avg=(float)sum/n = %f\n", avg);
avg = (float)(sum/n); /*avg without type cast on (sum/n) */
printf("avg=(float)(sum/n) = %f\n", avg);
}
Output
---------------------------------------------------------------------------------------------------------
avg=sum/n = 4.000000
avg=(float)sum/n = 4.700000
avg=(float)(sum/n) = 4.000000
---------------------------------------------------------------------------------------------------------

You might also like