C fundamentals
Basic Programming (CS 1000)
Sumanta Pyne
Assistant Professor
Computer Science and Engineering Department
National Institute of Technology Rourkela
[email protected]
August 25, 2023
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 1 / 26
First C Program - hello world.c
/* hello world.c written by Sumanta Pyne on 17/12/2021.
Prints ”Hello World!” on the screen.*/
#include<stdio.h> // header file for library function printf
int main() // main function begins
{
printf(”Hello World!”); // print ”Hello World!”
return 0; // returns integer value 0
} // end of main function
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 2 / 26
Comments in a C program
Texts which do not take part in program execution
Ignored by compiler
Increases readability
Single line comment - //
describes a single line
inactivates a program line
Multi line comment - /* */
large text descriptions of code
inactivate portions of a program
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 3 / 26
Header Files
Have .h extension
Contain library functions
Request use of a header file in your program by including it
Using C preprocessing directive #include
Provided by vendor
Created by user
Example - stdio.h
for standard input/output
printf - for output to be printed on screen
scanf - for input from keyboard
fprintf - for output to be written to a file
fscanf - for input to be read a file
Reduces programming/coding burden
All programmers do not have knowledge of computer system
Code reusabilty - write once use multiple times
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 4 / 26
The main function
First function of every C program
Responsible for starting execution and termination of program
Called by operating system when user runs the program
Has int or void as return data type
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 5 / 26
Punctuations
A code block is written within { }
Semicolon (;) at end of each line/statement
() after a function name
Preprocessing macros begin with #
A header file name within <>
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 6 / 26
Adding two numbers - sum.c
/* sum.c written by Sumanta Pyne on 17/12/2021.
Input: read two numbers (integers) from keyboard.
Computation: add two numbers
Output: print sum on screen.*/
#include<stdio.h> // header file for library function printf
int main() // main function begins
{
int a; // stores first number
int b; // stores second number
int c; // stores sum
printf(”Enter the first number:”); // entry of first number
scanf(”%d”,&a); // read first number from keyboard
printf(”First number = %d\n”,a); // print first number
printf(”Enter the second number:”); // entry of second number
scanf(”%d”,&b); // read second number from keyboard
printf(”Second number = %d\n”,b); // print second number
c = a + b; // add two numbers and store sum in c
printf(”Sum: %d + %d = %d”,a,b,c); // print sum of two numbers
return 0; // returns integer value 0
} // end of main function
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 7 / 26
Assignment 1
1 Write a program that will print your name, roll number, department, favourite subjects,
favourite sports and hobbies. All to be printed in separate. Usage of scanf not required.
Follow the “hello world” program.
2 Write a program to subtract two integers given as input. Print the difference.
3 Write a program to multiply two integers given as input. Print the product.
4 Write a program to divide two integers given as input. Print the quotient and remainder.
What will happen if divisor is zero?
5 Write a program to swap (exchange) two integers given as input. Print the swapped
values.
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 8 / 26
Identifiers
Name of variable, function, array, label, structure, union, enum
Format
Begin with a letter (a-z or A-Z) or underscore ( )
Followed by letter, digit (0-9), underscore
No blank spaces
Symbols : - ? . @ + * ; , $ # < = are not allowed
Keyword/reserved word (like int, return) cannot be identifiers
Distinct within a scope
First eight characters are recognized by C compiler
Case sensitive
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 9 / 26
Examples of valid identifiers
record1 - valid
1record - invalid, an identifier must begin with letter or underscore
file 3 - valid
return - invalid, return is a reserved word
$tax - invalid, an identifier must begin with letter or underscore
name and address - invalid, blank spaces not allowed
name and address - valid
name-and-address - invalid, dash (minus sign) not allowed
123 45 6789 - invalid, an identifier must begin with letter or underscore
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 10 / 26
Examples of identical and distinct identifiers
name, names - distinct
address, Address - distinct
identifier 1, identifier 2 - identical (1st 8 chars recognized)
list1, list2 - distinct
answer, Answer - distinct
char1, char 1 - distinct
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 11 / 26
Constants
Values should not be changed by program during normal execution
Types
Numeric
Integer
Real
Character
Symbolic
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 12 / 26
Integer Constants
Decimal representation (base 10)
12345678 - int constant
12345678L - long int constant
12345678U or 12345678u - unsigned int constant
12345678UL or 12345678ul - unsigned long int constant
Hexadecimal representation (base 16)
Begin with 0X or 0x
0X123ABCDEF or 0X123abcdef
Octal representation (base 8)
Begin with 0
0515
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 13 / 26
Real Constants
Float constant (base 10)
0.5f
-1.25f
Double constant (base 10)
0.5
-1.25
Scientific notation (base 10)
9.3 × 1012 – 9.3e12, 9.3e+12, 9.3E12, 9.3E+12
9.3 × 10−12 – 9.3e-12, 9.3E-12
0.8E+0.8 - invalid, exponent must be an integer
0.8E 8 - invalid, illegal character blank space
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 14 / 26
Charater constants
American Standard Code for Information Interchange (ASCII) characters
Size - 1 byte or 8 bits
Within single quotes
Example - ’a’, ’Z’, ’5’, ’+’, ’$’
Cannot contain multiple characters
’xyz’ is invalid
Constants with escape sequence start with backward slash (\)
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 15 / 26
Constants with escape sequence
’\a’ - audible bell
’\b’ - backspace
’\” - single quote
’\“’ - double quote
’\0’ - null character
’\n’ - line feed, new line character
’\f’ - form feed, new page character
’\t’ - horizontal tab space
’\v’ - vertical tab space
’\r’ - carriage return
’\\’ - backslash
’\?’ - question mark
’\052’ - octal value of (42)10
’\xFF’ - hexadecimal value of (255)10
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 16 / 26
String constants
Within double quotes
Examples
”“ - empty or null string
”Basic programming“
”212EE0123“
”TN-05 2155“
”Percentage(%)“
”\“Happy New Year!\“\n *****2022*****“
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 17 / 26
Symbolic constants
#define FACTOR -18
#define ERROR 0.0001
#define BEGIN {
#define END }
#define NAME ”Sharon“
#define EOLN ’\N’
#define COST ”$19.95“
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 18 / 26
Operators and Expressions
Arithmetic operators - +, –, *, %
Logical operators - &&, ||, !
Relational operators - >, <, <=, >=, ==, ! =
Bitwise operations - ˜, |, &,, <<, >>
Assignment operators - =, op=
Ternary operator - ?:
Array operator - []
Function operator - ()
Pointer - *
Address - &
Dot - .
Arrow - − >
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 19 / 26
Arithmetic operators
Plus(+) and minus(–)
unary - +a; -a;
binary - a+b; a–b;
Multiply(*) - a*b;
Divide(/) - a/b;
Modulo (remainder) - a%b;
% operation with floating point not allowed
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 20 / 26
For a op b, where op in {+,-,*,/}
a is char, b is int - a op b is int
a is char, b is short int - a op b is short int
a is int, b is short int - a op b is int
a is long int, b is int - a op b is long int
a is long int, b is char - a op b is long int
a is long int, b is float - a op b is float or double
a is char, b is float - a op b is float or double
a is double, b is float - a op b is double
a is double, b is long int - a op b is double
a is double, b is long int - ((int)a) op b is long int
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 21 / 26
Logical operators
And (&&) - a && b is true, iff both a and b are true
Or (||) - a || b is true, iff either of a or b is true
Not (!)
!a is true if a is false
!a is false if a is true
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 22 / 26
Relational operators
Equality (==) - a == b is true, if a=b
Not equal (!=) - a != b is true, if a and b are not equal
Greater than (>) - a > b is true, if a > b
Less than (<) - a < b is true, if a < b
Greater than or equal (>=) - a >= b is true, if a > b or a = b
Less than or equal (<=) - a <= b is true, if a < b or a = b
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 23 / 26
Bitwise operators
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 24 / 26
Assignment 4
1 Write a program to evaluate the expressions a*(c%b) and a*c%b.
2 Write a program to find area and circumference of a circle.
3 Write a program to convert temperature in degree Celcius to degree Fahrenheit.
4 Write a program to convert speed in km/h to mi/h.
5 Write a program to convert mass in lbs to kg.
6 Write a program to convert feet to cm.
7 Write a program to convert yard to m.
8 Write a program to find simple interest.
9 Write a program to evaluate the expression (a>b)&&((c<d)||(e==f)).
10 Write a program to perform following bitwise operation on two bit strings.
OR, AND, XOR, NOR, NAND, XNOR
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 25 / 26
Thank you
Sumanta Pyne (NITRKL) Spring 2022, Introduction August 25, 2023 26 / 26