0% found this document useful (0 votes)
32 views24 pages

Lecture CSP02

Uploaded by

physics lover
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)
32 views24 pages

Lecture CSP02

Uploaded by

physics lover
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/ 24

Introduction to C Programming language

• Structure of a C program
Character Set
Key words and Identifiers
Constants
Variables
Data Types
• Sample Example Programs

1
Structure of a C Language Program

/* Program to compute …………………………. */


#include<stdio.h> /*Library files inclusion */

/* Global variable declaration …………………. */


int main()
{
/* local variable declarations */
/* initialization */
/* statements */

return(0);
}

2
Structure of a C Program

•Every C program consists of one or more modules called functions.


• One of the functions must be called main.
Execution will always begin by main.
• Each compound statement is enclosed within a pair of braces
{
Statement1;
Statement2;
………
}
• Each statement must end with a semicolon (;)
• Anything within /* */ treated as comments
• Comments may appear anywhere within program

3
Example: A simple C program
#include<stdio.h> /*Library files inclusion */
/* Global variable declaration …………………. */

int main()
{ int num1,num2,sum; /* Variable declaration*/
num1 = 5; /* Initialization */
num2 = 10;
sum = num1 + num2; /* Expression */
printf(“Sum of numbers = %d”,sum); /*output*/

return(0);
}
4
About C
• General purpose structured programming language
• Characterized by the ability to write very concise source
program
• Comparatively small instruction set
• Programs are highly portable

5
C Fundamentals
Token in C
 TOKEN is the smallest unit in a 'C' program.
 It is each and every word and punctuation that you come across in your C
program.
 compiler breaks a program into the smallest possible units (tokens) and
proceeds to the various stages of the compilation.
 A token is divided into six different types, ie.,
Keywords, Operators, Strings, Constants, Special Characters, and
Identifiers.
C Fundamentals
• The C Character Set
 Uppercase letters A to Z , lower case letters a to z, digits 0 to 9
 Special characters.
e.g. + , - , *, &, ?, /, \, #, (, ), {, },[, ], <, >, ==, =, ;, :, ‘, etc
 Escape sequences
• Identifiers And Keywords
 Names given for various program elements
e.g. variables, function name, array name and user-define data.
 Identifiers must be unique. Identifier are defined against a set of rules.
 They are created to give a unique name to an entity to identify it
during the execution of the program.
 - , “ , and space is not allowed
 Reserve words are called keywords
– keywords have special meaning in C language
 User can’t use them for their intended purpose
e.g. auto, break, for, void, return, enum if, for
7
Rules for naming identifiers
 A valid identifier can have letters (both uppercase and lowercase
letters (a..z, A..Z) , digits (0..9) and underscores ( _ ).
 The first letter of an identifier should be either a letter or an
underscore.
 You cannot use keywords like int, printf, main etc. as identifiers.
 Identifiers are also case sensitive in C.
eg. name and Name are two different identifiers in C.
 No special characters, such as semicolon, period, whitespaces, slash or
comma are permitted to be used in or as Identifier.
 There is no rule on how long an identifier can be.
 but, you may run into problems in some compilers if the identifier is longer than
31 characters
 ANSI C allows 31 significant characters, which is a far more practical
limit (ANSIC -(American National Standards Institute –standard for C) ) .
 You can choose any name as an identifier if you follow the above rule,
 however, give meaningful names to identifiers that make sense.
Examples:

Invalid Remark Valid


5thplayer First char is number player5
“x” First char is not letter x
Order-no - is not allowed Order_no
auto Auto is a key word autotype
count value Blank is not permitted Count_value
Correct and Valid _PositveNum
Correct and Valid Game20

9
Data Types
Three classes of data types
1. Primary or primitive or fundamental
2. Derived (Discuss later)
3. User defined ( Discuss later)

char-

int-
float-

10
Primary Data Types
Data Description Memory storage FORMAT
SPECIFIER
type (Range of values) size Requirement
int Integer quantity 2 bytes %d
(-32768 to 32767)
char Single character 1 byte %c
(-128 to 127)
float Floating point 4 bytes %f
number
(3.4e-38 to 3.4e+38)
double Double precision 8 bytes %lf
(1.7e-308 to 1.7e+308)

Note: Qualifiers can be used to control over the range of the numbers
signed, unsigned, short, long

11
Data Types

12
Constants
• Integer constants
• Integer valued number consists sequence of digits
e.g const int a=10;

• Floating Point Constants


• It is a base 10 number that contains either a decimal point or an
exponent or both
e.g. 1. , 0.2, 2E-8, 1.75E+6

• Character Constants
• Single character, enclosed in apostrophes
• They have integer values that are determined by the computer’s
particular character set
• Mainly ASCII is used for it
e.g. const char a = ‘A’;

13
ASCII stands for American
Standard Code for Information
Interchange.

 ASCII character table,


including descriptions of
first 32 characters shown 

 ASCII was originally designed


for use with teletypes
Constants
• String Constants
 Consists any number of consecutive characters enclosed in
double quotation marks
 Special characters and non printing characters can be
included in a string constants
 Last character of a string constant is always a null char

??Q ? : Is ‘B’ and “B” are equivalent ?

• Escape Sequences
 Non printing characters and special characters can be
expressed in terms of it
 e.g. \n, \r, \t, \b

15
Variables And Their Declarations

• Variables
 It is an identifier that is used to represent a single
data item
 The Data item must be assigned to the variable
 Its value can be changed anywhere in the program
but its type can’t be

• Declarations
 All variables must be declared before they can appear
in executable statements
 It consists of a data type, followed by one or more
variables.

16
Variable Declarations

 Variables are used as names for data items.


 Each variable has a type, which tells the compiler how the data is to
be interpreted (and how much space it needs, etc.).

int counter;
int startPoint;

 int is a predefined integer type in C.

17
Assign Statement ( ie. assignment Operator)
Values can be assigned to variables using assignment
operator ( = )

Syntax:
variable_name = constant;

Examples:
name = “mohan”;
Example
amount = 100.50;
value = 25; a=5; b=5;
symbol = ‘X’; or
a=b=5;

18
Reading Data from Keyboard
Syntax
scanf(“control string”, &var1, &var2,…);
Control String:
• Contains the format of data being received
• & specifies the variable name’s address
• var1 and var2,…. are variable names

Example:
Scanf(“%d”, &count);

Here,%d specifies that integer value is to be


read
19
Display Output to Console

Syntax
printf(“control string”,var1,var2,…);
Control String:
•Contains the format of data being displayed
• var1 and var2,…. are variable names

Example:
printf(“The value of count is = %d”,count);

Here, %d indicates that integer value is to be


displayed

20
Sample Program
#include<stdio.h>
/* program to display use of I/O and data types */
int main() Input
{ int marks;
Enter your marks:
float average;
70
char grade;
Enter average marks:
printf(“Enter your marks:\n”);
50
scanf(“%d”, &marks);
Enter your grade:
printf(“Enter average marks:\n”);
A
scanf(“%f”, &average);
printf(“Enter your grade:\n”);
scanf(“%c”, &grade); Output
printf(“Marks = %d\n”,marks); Marks = 70
printf(“Average = %f\n”,average); Average = 50
printf(“Grade = %c\n”,grade);
Grade = A
return 0;
}

21
Try Sample C Program to execute
//Write the Name of program as Display.c
#include <stdio.h>
//My First Program
int main()
{
printf(" My Name is <type your name here>
\n");
printf(" My ID is < write ID no> \n");
printf( " My Branch is < type bracnh>
\n");

printf( " My hobby is <---write---> \n");


printf(" I am happy to Run my First C
program ");

return 0;
}
Try Sample C Program to execute

//write Name of program Multiply.c


//Program for multiplying two integer numbers
#include<stdio.h>
int main()
{ // product of two numbers
int x=200 ;
int y= 20;
int z;
printf(" Program written by _____, Id : 2020______ \n \n");

printf("Enter the value of x \n");


scanf("%d", &x);
printf(“Input the value of Y");
scanf("%d", &y);
z = x*y;
printf(" Product of x=%d and y= %d is =%d \n ", x, y, z );
printf(" X * Y= %d ",z);
return(0);
}
Any Queries ?

You might also like