Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views12 pages

Unit 1

Uploaded by

priyavenkat171
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)
7 views12 pages

Unit 1

Uploaded by

priyavenkat171
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/ 12

UNIT - I:

Basic of C: History of C and its importance – Structure of a C program –


Data Types – Constants and Variables – Operators and Expressions – Order of
Precedence, Evaluating of Arithmetic Expressions – Type
HISTORY OF C

 C programming language can be used to design the system


softwarelike operating system and Compiler.
IMPORTANCE OF ‘C’ LANGUAGE  To develop application software like database and spread sheets.
1. It is robust language whose rich setup of built in functions and operator  For Develop Graphical related application like computer and
can be used to write any complex program. mobilegames.
2. Program written in C are efficient due to several variety of data types  To evaluate any kind of mathematical equation use c language.
and powerful operators.  C programming language can be used to design the compilers.
3. The C compiler combines the capabilities of an assembly language with  UNIX Kernal is completely developed in C Language.
the feature of high level language.  For Creating Compilers of different Languages which can take input
4. There are only 32 keywords from other language and convert it into lower level machine
5. C is portable language -C programs written for one computer system can dependent language.
be run on another system, with little or no modification.  C programming language can be used to design Operating System.
6. C language is well suited for structured programming.  C programming language can be used to design Network Devices.
7. C language has its ability to extend itself.  To design GUI Applications. Adobe Photoshop, one of the most
Applications of C language popularly used photo editors since olden times, was created with
the help of C.
BASICSTRUCTURE OFC PROGRAM  In this section, we define different constants.
 The components of the basic structure of a C program consists of 6 parts  The keyword define is used in this part.
Global declaration section
 The entire global variable used is declared in this part.
 The user-defined functions are also declared in this part of the code.
MAIN FUNCTION
 Every C-programs need to have the main function.
 Each main function contains 2 parts.
o Declaration part - The declaration part is the part where all the
variables are declared
o Execution part - The execution part begins with the curly brackets
and ends with the curly close bracket.
 Both the declaration and execution part are inside the curly braces.
User-defined function section
 All the user-defined functions are defined in this section of the program.
CHARACTER SET
 C language contains the following set of characters.
o Letters
DOCUMENT SECTION o Digits
 Programmer gives the details associated with the program. o Special characters
 It usually gives o White space
o name of the program, Letters
o details of the author and other details like the time of coding o C language supports all the alphabets from the English language.
and description. o Lower and upper case letterstogether support 52 alphabets.
o anyone reading the code the overview of the code. LOWER CASE LETTERS a to z
PREPROCESSOR/LINK SECTION UPPER CASE LETTERS A to Z
 It is used to declare all the header files that will be used in the program. Digits
DEFINITION SECTION o C language supports 10 digits which are used to construct numerical
values in C language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special characters
o C language supports a rich set of special characters
o comma, period semicolon colan, questionmark ect( ~ @ # $ % ^ &
* ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ |) white spaces,
backspaces,harizantal tab,carriage return,newline,form feed.
C TOKENS
o The smallest individual units are known as tokens.
o C has six types of tokens as shown in fig.
Keywords
Decimal Constant 10, 20, 450 etc.
o All keywords have fixed meanings and cannot be changed.
o Keywords are pre-defined or reserved words in c. there are total 32 Real or Floating-point 10.3, 20.2, 450.6 etc.
keywords in C. Constant

Octal Constant 021, 033, 046 etc.

Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.

Character Constant 'a', 'b', 'x' etc.

String Constant "c", "c program", "c in


javatpoint" etc.
VARIABLES
Identifiers o A variable is a data name that may be used to store a data value.
o Identifiers refer to the name of a variable, function and arrays. o The values can be changed during the execution time.
Rules for identifiers: Rules for variable names
 First Character must be an alphabet (or underscore).  The first letter must be a letters (or underscore).
 Consist of only letters, digits, or underscore.  Variable name has length of 31 characters.
 It should be up to 31 characters long.  Uppercase and lowercase are significant
 It should not be a keyword
 Cannot use a keyword.  White space is not allowed
 Must not contain white space. DATA TYPES
CONSTANTS Data types are the keywords, which are used for assigning a type to a variable.
 A constant is a value or variable that can't be changed in the program\  Primary (Fundamental)data types
 For example: 10, 20, 'a', 3.4, "c programming" etc  Derived data types
 Several types constants as shown in the following figure  User defined data types

Constant Example Primary / Fundamental Data types


1. Integer
 Integer is whole numbers with a range.  Arithmetic operators are used to perform mathematical calculations like
 Integer occupies one word of storage. addition, subtraction, multiplication, division and modulus.
 C has 3class of integer storage - short int, int, long int all in signed and
unsigned forms.
2. Character (char)
 Character data type allows a variable to store only one character.
 Storage size of character data type is 1.
3. Floating point (float)
 Float data type allows a variable to store decimal values.
 Storage size of float data type is 4.
 .For example, 10.456789 can be stored in a variable using float data type Relational Operators
4. void Relational operators are used to compare the value of two variables.
 The void has no values.
 This is usually used to specify the type of functions.
 The type of function is said to be void when it does not return any value
to the calling function.
Derived data types
 The derived data type consists of Arrays, functions, Pointers (represents
the address and type of a variable or afunction).
User defined data types
 Structures (Different data items that make up a logical unit are grouped
Logical Operators
together as a structure data type)
 Logical operators are used when more than one conditions are to be
 Unions (A union permits references to the same location in memory to have
tested and based on that result, decisions have to be made.
different types) and
 Enumeration (used to define variables that can only be assigned certain
possible values throughout the program).
OPERATORS
 An operator is a symbol that tells the computer to perform certain
mathematical or logical manipulations.
Assignment Operators
 Operators require some data to operate on and such data is called
 Assignment operators are used to assign the result of an expression to a
operands.
variable.
 Operators in C can be classified into following categories:
 The most useful assignment operator in C is ‘=’.
o Arithmetic Operators , Relational Operators, Logical Operators
o Assignment Operators, Increment and Decrement Operators
o Conditional Operators, Bitwise Operators and
o Special Operators
Arithmetic Operators
member selection operators (. and ->).
1. Comma Operator
 The comma operator can be used to link the related expressions together.
 A comma linked expression is evaluated from left to right and the value of the
right most expression is the value of the combined expression.
Example: x = (a = 2, b = 4, a+b)
2. Sizeof( ) operator
 Sizeof operator is a compile time operator.
 The sizeof operator is usually used to determine the length of arrays and
structures when their sizes are not known.
Increment and Decrement Operators  It is also used in dynamic memory allocation.
 The increment operator ++ adds 1 to the operand x = sizeof (a);
 The decrement operator – subtracts 1 from the operand. y = sizeof(float);
EXPRESSIONS
 An expression is a combination of one or more operands, zero or more
operators, and zero or more pairs of parentheses.
 There are three kinds of expressions:
 An arithmetic expression evaluates to a single arithmetic value.
 A character expression evaluates to a single value of type character.
Conditional Operators  A logical or relational expression evaluates to a single logical value.
 Conditional operators return one value if condition is true and returns Arithmetic Expressions
another value is condition is false.  An arithmetic expression evaluates to a single arithmetic value, and its
 The operator pair “?” and “:” is known as conditional operator. operands have the following types.
 These pair of operators is ternary operators.  The operators for an arithmetic expression are any of the following:
 The syntax is: TABLE 3-1 Arithmetic Operators
expression1 ? expression2 : expression3 ;
Example: x = (a > b) ? a : b ; Operator Meaning
Bitwise Operators
** Exponentiation
 Bitwise operators are used for testing the bits or shifting them left or
* Multiplication
right.
/ Division
- Subtraction or Unary Minus
+ Addition or Unary Plus
The rules for evaluation of expression are as follows:
1. First, parenthesized sub expressions from left to right are evaluated.
2. If parentheses are nested, the evaluation begins with the innermost sub-
Special Operators expression.
 Comma operator, sizeof operator, pointer operators (& and *) and 3. The precedence rule is applied in determining the order of application of
operators inevaluating sub-expressions.
4. The associativity rule is applied when two or more operators of the same
precedence appearin a sub expression.
5. Arithmetic expressions are evaluated from left to right using the rules of
precedence.
6. When parentheses are used, the expressions within parentheses assume
highest priority.
OPERATOR PRECEDENCE
Operator precedence determines which operation is performed first in an
expression with more than one operator with different precedence.

 C automatically converts any intermediate values to the proper type so that


expression can be evaluated without losing any significance.
 This automatic conversion is known as implicit type conversion.
 During evaluation, if the operands are of different types, the lower type is
automatically converted to the higher type before the operation proceeds.
And the result is always of the higher type.
Example:
5/2 = 2
5/2.0 = 2.5 (Implicit type conversion)
5.0/2 = 2.5 (Implicit type conversion)
2. Explicit Type Conversion
 This process is also called type casting and it is user defined.
 Here the user can type cast the result to make it of a particular data type.
Syntax
(type) expression

Type conversion in expressions


1. Implicit Type Conversion

Example:
#include<stdio.h>
void main() Format String Meaning
{
double x = 1.2; %d Scan or print an integer assigned decimal number
// Explicit conversion from double to int
int sum = (int)x + 1; %f Scan or print a floating point number
printf("sum = %d", sum); %c scan or print a character
} %s scan or print a character string. The scanning ends at
Output: whitespace.
sum = 2
MANAGING INPUT AND OUTPUT OPERATIONS
Example: program to print cube of given number
 C programming language provides many built-in functions to read any #include<stdio.h>void main()
given inputand to display data on screen as output, the result. {
 Input means to provide the program with some data to be used in the int number;
program printf("Enter a number:");scanf("%d",&number);
 Output means to display data on screen or write the data to a printer or a printf("Cube of number is:%d ",number*number*number);
file. }
 All the built-in functions are present in C header files Output:
INPUT AND OUTPUT FUNCTIONS IN C LANGUAGE Enter a number 5
1. scanf( ) Cube of number is : 125
2. printf( )
3. getchar( ) getchar( ) & putchar( ) functions
4. putchar( )  The getchar( ) function reads a single character from the terminal and
5. gets( ) returns it asan integer.
 The putchar( ) function displays the single character passed to it on the
6. puts( )
screenand returns the same character.
scanf( ) and printf( ) functions
Example : program for getchar( ) and putchar( ) function
 The standard input-output header file, named stdio.h contains the #include <stdio.h>
definition of thefunctions scanf( ) and printf( ) . void main( )
 Input function takes input from user and displays output on screen {
respectively. int c;
 They are known as inbuilt library functions. printf("Enter a character :");
c = getchar( ); /* Take a character as input and store it
The syntax of scanf( ) function is in variable c*/
putchar(c); /*display the character stored in variable
scanf("format string",argument_list); c */
}
The syntax of printf( ) function is gets( ) function

printf("format string",argument_list);
 The gets( ) function enables the user to enter some characters  Simple if statement
followed by theenter key.  if..else statements
 nested if statements
 All the characters entered by the user get stored in a character array.  else-if ladder
 The gets() allows the user to enter the space-separated strings. It Simple if statement
returns the stringentered by the user.  If statement is always used with a condition.
 The condition is evaluated first before executing any statement inside the
Syntax : gets(char[]); body of it.
Syntax :
puts( ) function if (test expression)
{
 The puts() function is used to print the string on the console which is statement block;
previously readby using gets() or scanf() function.
}
Program to read a string using gets( ) and print it on the console using puts( ) statement n;
#include<stdio.h> #include<string.h> Example:
void main() #include <stdio.h> void main()
{ {
char name[50]; printf("Enter your name: "); int x = 20, y = 22;
gets(name); //reads string from userprintf("Your name is: "); if (x<y)
puts(name); //displays string {
} printf("Variable x is less than y");
Output: }
Enter your name : S. Hariharsudhan }
Your name is S. Hariharsudhan
DECISION MAKING AND BRANCHING Output: Variable x is less than y
if …else statement
 It supports sequential program statements which execute one  The if-else is statement is an extended version of if statement.
statement immediately after another.  It is used to execute the code if the condition is true or false.
 C language handles decision-making by supporting the following  It is also called a two-way selection statement.
statements: Syntax:
o if statement if (test-expression)
o switch statement {
o conditional operator statement (? : operator)
True block of statements
o goto statement }
if statement else
{  When a series of the decision are involved in a statement, use the if-else
statement in nested form.
False block of statements
Syntax:
} if( expression )
Statements; {
if( expression1 )
Explanation: {
 If the value of test-expression is true, then the true block of statements statement-block1;
will be executed. }
 If the value of test-expression if false, then the false block of statements else
will be executed. {
 In any case, after the execution, the control will be automatically statement-block 2;
transferred to the statements appearing outside the block of If.
}
Example: }
#include<stdio.h> else
void main() {
{ statement-block 3;
int age; }
printf("Enter your age:"); scanf("%d",&age); Example:
if(age >=18)
{ #include<stdio.h>

printf("You are eligible for voting"); #include<conio.h>

} void main( )
else {
int a,b,c;
{
printf("You are not eligible for voting"); clrscr();

} printf("Please Enter 3 number");


} scanf("%d%d%d",&a,&b,&c);
Output:
if(a>b)
Enter your age: 14
You are not eligible for voting {
if(a>c)
Nested If statement {
printf("a is greatest");
 The nested if...else statement is used when a program requires more than
}
one test expression.
else
 It is also called a multi-way selection statement.
{ #include<stdio.h>
printf("c is greatest"); #include<conio.h>
} void main( )
} {
else int a;
{ printf("enter a number");
if(b>c) scanf("%d",&a);
{ if( a%5==0 && a%8==0)
printf("b is greatest"); {
} printf("divisible by both 5 and 8");
else }
{ else if( a%8==0 )
printf("c is greatest"); {
} printf("divisible by 8");
} }
getch(); else if(a%5==0)
} {
Else...if ladder printf("divisible by 5");
 It is used to execute one code from multiple conditions. }
 It is also called a multipath decision statement. else
Syntax: {
if(condition1) printf("divisible by none");
{ }
//statements getch();
} }
else if(condition2)
Switch Statement
{
 Switch statement acts as a substitute for a long if-else-if ladder that is
//statements
used to test a list of cases.
}
 A switch statement contains one or more case labels that are tested
else if(condition3)
against the switch expression.
{
 When the expression match to a case then the associated statements
//statements
with that case would be executed.
}
Syntax:
else
Switch (expression)
{
{
//statements
case value1:
}
//Statements
Example:
break;
case value2: default:
//Statements printf("Error! operator is not correct");
break; }
case value3: }
//Statements Conditional Operator in C
case value n:  The conditional operator is also known as a ternary operator.
//Statements  The conditional statements are the decision-making statements which
break; depends upon the output of the expression.
Default:  It is represented by two symbols, i.e., '?' and ':'
//Statements Syntax: Expression1? expression2: expression3;
}
Example:
// Program to create a simple calculator
#include <stdio.h>
void main()
{
char operation;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operation); Example:
printf("Enter two operands: "); #include <stdio.h>
scanf("%lf %lf",&n1, &n2); void main()
{
switch(operation) int age; // variable declaration
{ printf("Enter your age");
case '+': scanf("%d",&age);
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2); (age>=18)? (printf("eligible for voting")) :
break; (printf("not eligible for voting")); // conditional
case '-': operator
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
}
break;
goto statement
 The goto statement is known as jump statement in C.
case '*':  Goto is used to transfer the program control to a predefined label.
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);  The goto statement can be used to repeat some part of the code for a
break; particular condition.
case '/': It can also be used to break the multiple loops which can't be done by
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2); using a single break statement
break; Syntax:
label:  Development of video games.
//some part of the code;  Applications using graphical user interfaces.
goto label;  Databases and computer operating systems.
Example:  Browsers on the internet.
#include <stdio.h>  Computational and graphical methods.
void main()  Banking.
{
 Cloud computing and distributed systems.
int num,i=1;
 Embedded systems are systems that are integrated into a larger system.
printf("Enter the number whose table you want to print?");
scanf("%d",&num);  Integrated software libraries for enterprises
table:  Server applications on a large scale
printf("%d x %d = %d\n",num,i,num*i);  Compilers of code
i++;
if(i<=10) 2 Mark questions
goto table; 1. Define the term variable and constant.
} 2. What is an expression in C language?
Features of C Language 3. Specify the use of printf( ) and scanf( ) functions.
 Simple 4. What is an unary operator? Give example.
 Machine Independent or Portable
5. What are formal arguments?
 Mid-level programming language
 structured programming language 6. Define the term recursion.
 Rich Library 7. What are automatic variable?
 Memory Management 8. What are bitwise operators?
 Fast Speed
 Pointers 5 Mark and 10 Mark Questions
 Recursion
 Extensible 1. Discuss about History of C language.
2. Write down the applications of C language.
3. List and explain the features of C language.
4. Give a short note on Data types in C language.
5. What is meant by Constant? Explain its types.
6. Give a note on C tokens.
7. Explain in detail about any 3 operators in C.
8. Briefly explain the various forms of if statements with example.
9. Discuss about Switch statement with an example.
Use/ Application of C Language
10. Write short notes on Goto statement with example.

You might also like