1 of 23

CSO101: Computer Programming

O.S.L. Bhavana

2 of 23

Structure of a C Porgram

3 of 23

Documentation: Comments in C

  • Text written in a program that is ignored by the compiler and is used to explain or document the code for humans.
  • Explains logic of the Code, improves readability, facilitates easier maintenance of code
  • Single Line Comments:

// This program prints the message Namaste Varanasi

// This is a basic Calculator program

  • Multi-line Comments: Should be enclosed within /* and */

/* first line

Second line */

  • Comments can appear anywhere in the program

4 of 23

Structure of a C Porgram

5 of 23

Preprocessor Directives (Link)

  • #include
  • C Programs are divided into modules and functions
  • Commonly used functions are grouped and stored in separate files called header files (end with .h)
  • Ex: printf is stored in stdio.h
  • To access such functions -

#include <headerfile_name.h> or

#include “headerfile_name.h” is used

Preprocessor directives are generally placed in the beginning of the file

6 of 23

Preprocessor Directives: Symbolic Constants

  • #define
  • It is a preprocessor directive and not a C statement
  • #define PI 3.14
  • Replaces the individual occurrence of PI in the program with 3.14
  • Should not end with a semicolon
  • float a = PI * 2; (will be preprocessed as float a = 3.14*2;)
  • PI is a symbolic constant but not a variable. Therefore no memory will be allocated
  • The constant cannot be reassigned (PI= 3.413 is invalid)
  • Symbolic constant names should follow same rules as variable names
  • No type checking & error detection

7 of 23

Structure of a C Porgram

8 of 23

C Tokens

  • Smallest individual units of a program
  • int a = 10+4;

int - keyword; a - identifier; = , + - operators; 10, 4 - numeric constants

  • Types of C Tokens
    1. Keywords
    2. Identifiers (names of variables, symbolic constants, functions(ex.printf) etc)
    3. Strings - sequence of characters in double quotes - Ex. “ABC123”
    4. Constants (literals) - Ex: 10, ‘Z’, 4.5 etc
    5. Operators - Ex: +, -, ^ etc
    6. Special symbols - Ex: ( ), [ ]

9 of 23

Keywords in C

  • What are keywords in a language?
  • Keywords in C are words that have special meaning to the C compiler
  • Reserved words used in programming. Ex: for
  • Each keyword is associated with a specific feature
  • Ex: int – the integer datatype

10 of 23

Keywords in C

  • Some keywords in C

11 of 23

Variables in C

  • A variable is an entity whose value may change during the runtime of the program
  • Declaration of variables:
    • Variables are declared by specifying the data type of the variable followed by its name chosen by the programmer.

Ex: int a;

char c;

12 of 23

Declaration of Variables

    • When a variable is declared a set of memory locations are assigned to store the value of the variable (during the execution of the program)
    • Ex: int a; specifies that a is an integer and 4 bytes of memory is assigned to store the value of a.

char c; assigns 1 byte of memory to store the value of c

    • The amount of memory assigned to store a variable depends on its data type

13 of 23

Declaration of Variables

    • All variables must be declared before their use in the program

    • Multiple variables of the same data type can be declared in a single statement
    • Ex: int a,b,c; // declares three variable a,b and c as integers

14 of 23

Initialization of Variables

  • Assigns a value to the variable

  • Ex: int a; // declares a as an integer

a=10; // assigns the value 10 to a and the value 10 will be stored (in binary format) in the memory locations assigned to a.

int a=10; //assigns and declares a variable in one statement

char a=‘A’, b=‘B’; //assigns and declares the variables a and b variable in one statement

  • Uninitialized variables may take the value 0 or some garbage values

15 of 23

Initialization of Variables

  • Uninitialized variables may take the value 0 or some garbage values

  • Ex: int a, b=10; // declares a,b as integers and assigns value 10 to b

b = a + 10; // may re-assign some garbage value to b as the value of a itself is garbage

16 of 23

Variable Names in C

  • A variable name is a human-readable name for the locations in memory assigned to the variable
  • Ex: int x=10; // declares a variable with name x and assigns value 10 to it. Internally, 4 bytes of memory is allocated for the x.

x = 20; // re-writes the same memory locations assigned to x with the new value 20

17 of 23

Variable Names in C

  • A variable name
    • May consist of alphabets (upper and lower case), digits and underscore ‘_’
    • Must begin with alphabets or ‘_’
    • No whitespaces or special characters are allowed
    • Cannot be same as keyword

18 of 23

printf on variables

int a = 10;

printf(“The value of variable is %d”, a);

// prints “The value of variable is 10”

Here %d is the format specifier for an integer variable

The input to printf within “ “ is called as the format string.

It specifies the format in which the output is printed

The format string “The value of variable is %d” specifies to print the text The value of variable is followed by an integer whose variable name is given next argument/input

More arguments than format specifiers -> Extra arguments are ignored

Lesser arguments than format specifiers -> Undefined behaviour /Prints garbage data

19 of 23

Data types

The size of each data size is specific to the system configuration. Here are some typically used values

int – to declare integers - %d

long - to declare large integers - %ld

float - declare numbers with fractional parts - %f

double - declares large floating point numbers - %lf

char - declares characters - %c

20 of 23

scanf on variables

scanf is used to read user-input data into variables

scanf takes multiple inputs - a format string, and addresses of the variables where the input data has to be stored

int a; char b;

Ex. scanf(“%d %c”, &a, &b);

Here %d is the format specifier for the integer variable a

%c is the format specifier for the character variable b

The input to printf within “ ” is called as the format string.

It specifies the format in which the input should be read

&a and &b specify the addresses of the variables a and b respectively. We will addresses in more detail later!

21 of 23

scanf on variables

More arguments than format specifiers -> Extra arguments are ignored

Lesser arguments than format specifiers -> Undefined behaviour

22 of 23

Qualifiers

  • “type qualifiers are” keywords that give extra properties to a type without changing its size or basic representation.
  • const is a “qualifier” in C that is used to define read only variables in C.
  • The remaining qualifiers volatile, restrict, _Atomic are out of the scope of this course.

23 of 23

Const: Read-only Variables in C

  • An entity whose value cannot be modified once they are defined

  • They are fixed values in a program

  • It is useful to define fixed values as constants to avoid programming errors

  • Read-only variables can be defined using keyword const
  • const int a =10; // declares a as an integer constant
  • const char d =’Z’; // declares and initializes d as a character constant
  • Initialization of a constant should happen along with declaration
  • const int a=10; // compiles successfully
  • const int a; a=10; // results in compilation error