Uploaded by sasheena Mohomed

CLes01 C Programming basic Concepts

advertisement
Basic Programming Concepts
Unit 01 - Programming
Variables
• A variable is a name of the memory location.
• It is used to store data.
• Its value can be changed, and it can be reused many
times.
• It is a way to represent memory location through
symbol so that it can be easily identified.
Unit 01 - Programming
Variable Declaration
<data_type> <variable_name>;
ex:
int age;
float marks;
char grade;
Unit 01 - Programming
Variable Initialization
<variable_name > = value;
ex:
age = 25;
marks = 85.5;
grade = ‘A’;
Unit 01 - Programming
Declaration & Initialization
• We can do declaration and initialization both in one
single line
ex:
int age = 25;
float marks = 85.5;
char grade = ‘A’;
Unit 01 - Programming
Rules for defining variables
• A variable can have alphabets, digits, and
underscore.
• A variable name can start with the alphabet, and
underscore only. It can't start with a digit.
• No whitespace is allowed within the variable
name.
• A variable name must not be any reserved word or
keyword, e.g. int, float, etc.
Unit 01 - Programming
Keywords
auto
break
case
char
const
double
int
struct
else
long
switch
enum
register
typedef
extern
return
union
float
for
short
signed
unsigned void
Unit 01 - Programming
continue default
goto
sizeof
volatile
do
if
static
while
What Happens?
• What happens when the declaration is executed?
• it reserves a memory location from computer memory to
our program
• Name that memory location with the name we given to the
variable
Unit 01 - Programming
What Happens?
Physical Memory
Addresses
total
AA0010
AA0011
no1
AA0100
AA0101
AA0110
no2
AA0111
Computer Memory
Unit 01 - Programming
int
no1;
int
no2;
int
total;
What Happens?
total
AA0010
no1 = 50;
AA0011
AA0100
50
no1
no2 = 30;
AA0101
AA0110
AA0111
30
no2
Unit 01 - Programming
What Happens?
AA0010
80
total
AA0011
AA0100
50
no1
Total = no1 + no2 ;
50 +
AA0101
AA0110
AA0111
30
no2
80
Unit 01 - Programming
30
Printf() and Scanf()
• The printf() and scanf() functions are used for output
and input in C language.
• Both functions are inbuilt library functions, defined
in stdio.h (header file).
Unit 01 - Programming
Printf()
• prints the given statement to the console.
• Syntax:
printf( “format string“ ,argument_list);
• Ex:
printf(“Hello World”);
Unit 01 - Programming
printf()
• If you want to display a value of a variable
int age = 25;
float marks = 85.5;
char grade = ‘a’;
printf(“my age is %d years“, age)
printf(“I scored %f marks “, marks)
printf(“I obtained %c pass for the exam, grade);
Unit 01 - Programming
Scanf()
• The scanf() function is used for input.
• It reads the input data from the console.
Unit 01 - Programming
int studentNo ;
float marks;
char grade ;
printf("Enter student no \n");
scanf("%d", &studentNo);
printf("Enter Marks \n");
scanf("%f", &marks);
printf("Enter grade \n");
scanf("%c", &grade);
printf("Your student Number is %d \n", studentNo);
printf("You scored %f marks \n", marks);
printf("I obtained %c pass for the exam \n", grade);
Unit 01 - Programming
Download