CSO101: Computer Programming
O.S.L. Bhavana
Structure of a C Porgram
Documentation: Comments in C
// This program prints the message Namaste Varanasi
// This is a basic Calculator program
/* first line
Second line */
Structure of a C Porgram
Preprocessor Directives (Link)
#include <headerfile_name.h> or
#include “headerfile_name.h” is used
Preprocessor directives are generally placed in the beginning of the file
Preprocessor Directives: Symbolic Constants
Structure of a C Porgram
C Tokens
int - keyword; a - identifier; = , + - operators; 10, 4 - numeric constants
Keywords in C
Keywords in C
Variables in C
Ex: int a;
char c;
Declaration of Variables
char c; assigns 1 byte of memory to store the value of c
Declaration of Variables
Initialization of Variables
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
Initialization of Variables
b = a + 10; // may re-assign some garbage value to b as the value of a itself is garbage
Variable Names in C
x = 20; // re-writes the same memory locations assigned to x with the new value 20
Variable Names in C
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
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
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!
scanf on variables
More arguments than format specifiers -> Extra arguments are ignored
Lesser arguments than format specifiers -> Undefined behaviour
Qualifiers
Const: Read-only Variables in C