Professional Documents
Culture Documents
Basics of C
Basics of C
Lecture 3
General Aspect of ‘C’
C was originally developed in the 1970s, by Dennis Ritchie at Bell
Telephone Laboratories, Inc.
C is a High level , general –purpose structured programming
language. Instructions of C consists of terms that are very closely
same to algebraic expressions, consisting of certain English
keywords such as if, else, for ,do and while
C contains certain additional features that allows it to be used at
a lower level , acting as bridge between machine language and
the high level languages.
This allows C to be used for system programming as well as for
applications programming
The Character set of ‘C’
C language consist of some characters set, numbers and
some special symbols. The character set of C consist of all the
alphabets of English language. C consist of
Alphabets a to z, A to Z
Numeric 0,1 to 9
Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
The words formed from the character set are building
blocks of C and are sometimes known as tokens. These
tokens represent the individual entity of language. The
following different types of token are used in C
1) Identifiers 2)Keywords 3)Constants
4) Operators 5)Punctuation Symbols
#include <stdio.h>
int main() {
printf("Hello World!");
return 0; #include <stdio.h>
}
int main() {
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}
Try it Yourself
#include <stdio.h>
int main() {
printf("Hello World!");
printf("I am learning C.");
return 0;
}
Basic Data Types
The data type specifies the size and type of information the variable will store.
In this tutorial, we will focus on the most basic ones:
There are different format specifiers for each data type. Here are
some of them
\\ casting
int num1 = 5;
int num2 = 2;
float sum = (float) num1 / num2;
== Equal to x == y
!= Not equal x != y
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables
or values:
Example
int myInt;
float myFloat;
double myDouble;
char myChar;
printf("%lu\n", sizeof(myInt));
printf("%lu\n", sizeof(myFloat));
printf("%lu\n", sizeof(myDouble));
printf("%lu\n", sizeof(myChar));
int x = 10;
int y = 9;
printf("%d", x > y);
do if static while
Variables
• A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in C has a specific type, which determines the size and
layout of the variable's memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the variable.
• The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and lowercase
letters are distinct because C is case-sensitive. There are following basic variable
types −
Type Description
• char Typically a single octet(one byte). This is an integer type.
• int The most natural size of integer for the machine.
• float A single-precision floating point value.
• double A double-precision floating point value.
• void Represents the absence of type.
Constants
Integer constants
• A integer constant is a numeric constant (associated with number)
without any fractional or exponential part. There are three types of
integer constants in C programming:
Floating-point constants
• A floating point constant is a numeric constant that has either a
fractional form or an exponent form. For example:
2.0,0.0000234,-0.22E-5
Character constants
• A character constant is a constant which uses single quotation
around characters. For example: 'a', 'l', 'm', 'F'
String constants
• String constants are the constants which are enclosed in a pair
of double-quote marks. For example: "good" ,"x","Earth is
round\n"
Escape Sequences
Hello C Programming 24
printf() functio
n
• The printf() function is used for output. It
prints the given statement to the console, defined
in stdio.h (header file).. The syntax of printf()
function is given below:
printf("format string", argument_list);
• The format string can be
%d for integer, %f for floating point number,
%c for single character, %s for a string etc.
25
scanf() functio
n
• The scanf() function is used for input ,
defined in stdio.h (header file). It reads the input
data from the console.
scanf("format string", &argument_list);
• The format string can b
%d for integer, %f for floating point number,
%c for single character, %s for a string etc.
26
/* Program to print cube of given number
*/
#include<stdio.h> main()
{
int number, cube; printf("enter a number:");
scanf("%d",&number);
cube=number*number*number; printf(“ cube
of number is:%d “ cube,);
}
Output
enter a number:5 cube of number is:125
27
• The scanf("%d",&number) statement reads integer
number from the console and stores the given value in
number variable.
28
/* Program to print sum of 2 integer numbers
*/
#include<stdio.h> main()
{
int x=0,y=0,result;
printf("enter first number:");
scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);
}
29
Output
enter first number:19 enter second number:11 sum of 2 numbers:30
If-else Statemen
t
32
if-else Statemen
• t is used to perform two
The if-else statement operations for a
single condition. The if-else statement is an extension to the if
statement using which, we can perform two different
operations, i.e., one is for the correctness of that condition, and
the other is for the incorrectness of the condition. The syntax
of the if-else statement is given below.
• if(expression)
{ executable statement 1;
}
Else
{ executable statement 2;
}
23
#include<stdio.h>
main()
{ int a, b;
printf("enter a:"); scanf("%d",&a);
printf("enter b:"); scanf("%d",&b);
if(a>b)
{
printf(“a is larg
e”);
}
else
{
printf(“b is larg
e”);
}
}
enter a:14 enter b: 1 a is large
--------- 2
34
---- enter a:19 enter b: 2 b is large
Outpu 5
t
#include <stdio.h> main()
{ int age;
printf("Enter your age?");
if(age>=18) scanf("%d",&ag
e);
{ printf("You are eligible to vot
e..."); }
else
{ printf(“You are not eligible t
o vote..."); }
}
-----
Output 35
36
Syntax of if-else-if statemen
t
if(condition1)
{
executable statements 1;
}
else if(condition2)
{
executable statements 2;
}
else if(condition3)
{
executable statements 3 ;
}
...
else
{
executable statements last;
37
}
Program to prepare the result of the stude
nt
#include <stdio.h>
main()
{ int marks;
printf("Enter your marks?"); scanf("%d",&marks);
if(marks >=600)
{ printf("Congrats ! you are place in the FIRST CLASS"); }
else if (marks >=500)
{ printf("You are placed in the SECOND CLASS"); }
else if (marks >= 400 )
{ printf("You are placed in the THIRD CLASS"); else }
• switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
int day = 4;
switch (day) {
case 6:
printf("Today is Saturday");
break;
case 7:
printf("Today is Sunday");
break;
default:
printf("Looking forward to the Weekend");
}
• while (condition) {
// code block to be executed
}
• int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
Do- while
• do {
// code block to be executed
}
while (condition);
• int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
For loop
• int i;
// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
Break& continue
• int i; • int i;
Example Explained
•myFunction() is the name of the function
•void means that the function does not have a return value. You will learn
more about return values later in the next chapter
•Inside the function (the body), add code that defines what the function
should do
• void myFunction() {
// code to be executed
}
• // Create a function
void myFunction() {
printf("I just got executed!");
}
int main() {
myFunction(); // call the function
return 0;
}
void myFunction() {
printf("I just got executed!");
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
int main() {
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;
}
// Hello Liam
// Hello Jenny
// Hello Anja
void myFunction(char name[], int age) {
printf("Hello %s. You are %d years old.\n", name, age);
}
int main() {
myFunction("Liam", 3);
myFunction("Jenny", 14);
myFunction("Anja", 30);
return 0;
}
// Function definition
void myFunction() {
printf("I just got executed!");
}
int main() {
int result = myFunction(5, 3);
printf("Result is = %d", result);
return 0;
}
// Outputs 8 (5 + 3)