Introduction to Computing and
Programming
Introduction to Programming, Identifiers and
Constants, Number System
Content
➢Quick Recap
➢Expressions
➢Comments
➢Conditional Statements
➢Number System
Recap
➢Structure of C program
➢Character Set
➢Delimiters
➢Keywords
➢Identifiers
➢Variables
Introduction to C
C is a general purpose C can be used for system
and structured programming and for
programming language. application programming.
It can be used to write very
concise source program
It is highly portable.
due to the availability of
extensive libraries.
History of C
Language
• Developed in 1970’s by Dennis
Ritchie at AT & T Bell Laboratories.
• It became popular in mid 1980’s
with the availability of compilers for
various platforms.
• Some standardization has been
made for C implementation
• ANSI
• GNU Compiler Collection (GCC)
• C11 → C17/18 → C23 (latest)
Structure of C
Program
C Program Example
int main()
#include <stdio.h>
{
int addition(int num1, int num2) int var1, var2;
{ printf("Enter number 1: ");
int sum; scanf("%d",&var1);
sum = num1+num2; printf("Enter number 2: ");
scanf("%d",&var2);
return sum;
int res = addition(var1, var2);
} printf ("Output: %d", res);
return 0;
}
Example
OUTPUT
Elements of C
Execution
Trigraph
Character Set Characters/Escape Delimiters
Characters
Sequences
Reserved Words/
Identifiers Data types Constants
Keywords
Variables Expressions Statements
ASCII: American Standard Code for Information Interchange
(Letter representation)
Character Set
Execution
Character /
Escape
Sequence
Trigraph Character
Delimiters
Reserved Words / Keywords (Total: 44)
Identifiers: User defined names
1. Consists of letters (a-z or A-Z), and digits (0-9).
2. Exclude special characters except the ‘_’
underscore.
3. Spaces are not allowed while naming an
identifier.
4. Can only begin with an underscore or letters.
5. Cannot name identifiers the same as keywords
6. The identifier must be unique in its namespace.
7. C language is case-sensitive so, ‘name’ and
‘NAME’ are different identifiers.
Data types
• Read-only variables whose values cannot be
modified once they are declared in the C program.
• The const keyword is used to define the constants.
• We can only initialize the constant variable in C at
the time of its declaration. Otherwise, it will store
the garbage value.
• The constant variables are immutable after its
definition,
Variables
• Variables are used to store different forms of data
like int, float, char, double, etc.
• It acts as a memory card where it saves all the data
and used it during program execution
Naming of a variable
Blank space between Keywords are not
Must not start with the
variables is not allowed to define as a
number
allowed variable
As C is a case Variable names can
sensitive language, be a combination of
upper and lower cases string, digits, and
are considered as a special characters like
different variable. underscores (_).
Variable declaration and initialization
• After variables are declared, the
space for those variables has been
assigned and it is used for the
program.
Types of variables
Types of Variable
• Local variable: Variables declared inside the
functions and only local functions can change
the value of variables.
• Global variable: Variables are declared
outside the functions and any functions can
change the value of variables.
Program on local variable
#include <stdio.h>
#include <stdio.h>
void subfunc() void foo(int a)
{
{
// Changing the value of 'a' in foo but not in main
int a; // since both variables are distinct
a = 10; a = 145;
printf("Foo: Variable a = %d\n", a); // a == 145
printf(“Sub function: Variable a = %d\n", a);
}
}
int main(void) int main(void)
{
{
int a;
subfunc();
printf("Main: Variable a = %d\n", a); a = 10;
return (0);
printf("Main: Variable a = %d\n", a); // a == 10
foo(a);
} printf("Main: Variable a = %d\n", a); // a == 10
return (0);
}
https://www.codequoi.com/en/local-global-static-variables-in-c/
#include <stdio.h>
int a;
void foo(void)
{
a = 42;
printf("Foo: a = %d\n", a); // a == 42
Program on }
global int main(void)
{
variable printf("Main: a = %d\n", a); // a == 0
foo();
printf("Main: a = %d\n", a); // a == 42
a = 200;
printf("Main: a = %d\n", a); // a == 200
return (0);
}
#include <stdio.h>
void foo(void)
{
int a = 100;
static int b = 100;
printf("a = %d, b = %d\n", a, b);
Program on a = a+1;
static b = b+1;
}
variable int main(void)
{
foo();
foo();
foo();
return (0);
}
File1.c File2.c
#include <stdio.h>
Program on extern int a;
void foo(void); // Foo prototype,
#include <stdio.h>
extern defined elsewhere int a = 100;
variable
int main(void)
{ void foo(void)
printf("Main: a = %d\n", a); {
foo(); a = 42;
printf("Main: a = %d\n", a); printf("Foo: a = %d\n", a);
a = 200; }
printf("Main: a = %d\n", a);
return (0);
}
Types of Variable
• Static variable: Declared with the static
keyword
• External variable: Declared using the extern
keyword which can be used in multiple C
source files.
Expressions
An expression is a combination of operators, constants, variables and functions
Program on Expressions
Statements • Instructions are written in the form of statements.
• An executable part of program to carry out some
actions
Compound Statement
A block is a set of
Compound
statements that are
statement in C is
enclosed within the
also called a block.
{ }.
This grouping
allows you to treat
multiple statements
as a single unit.
#include<stdio.h>
int main ()
{
int num1, num2;
num1=12;
Conditional
num2=13;
if (num1 == num2){
Statement printf("both are equal");}
else if (num1 > num2){
printf("%d is greater", num1);}
else{
printf("%d is greater", num2);}
return 0;
}
Comments in C
• Comments can be used to explain code, and to make it more readable.
• It can also be used to prevent execution when testing alternative code.
• Comments can be singled-lined or multi-lined.
Comments in C
Single-line comments: Multi line Comments:
• Start with two forward slashes (//). • Multi-line comments start with /* and
ends with */.
• Any text between // and the end of
the line is ignored by the compiler • Any text between /* and */ will be
(will not be executed). ignored by the compiler