STRUCTURAL
PROGRAMMING
printf and scanf Functions
■ printf() and scanf() functions are inbuilt library functions in C programming language
which are available in C library by default. These functions are declared and related
macros are defined in “stdio.h” which is a header file in C language.
■ Macros using #define pi 3.14
■ You can define a macro in C using the
■ #define preprocessor directive
■ #define sol 299792458 //speed of light
■ We have to include “stdio.h” file as shown in below C program to make use of these
printf() and scanf() library functions in C language.
print function in C
■ In C programming language, printf() function is used to print the (“character, string,
float, integer, octal and hexadecimal values”) onto the output screen.
■ We use printf() function with %d format specifier to display the value of an integer
variable.
■ Similarly, %c is used to display character, %f for float variable, %s for string variable,
%lf for double (We will discuss more about this in data types and format specifier).
■ C language is case sensitive. For example, printf() and scanf() are different from Printf()
and Scanf(). All characters in printf() and scanf() functions must be in lower case.
Example
Example :
#include <stdio.h>
int main()
{
printf("Name : Mohammad Masudur Rahman\n");
printf(“Address: Baridhara DOHS, Dhaka \n");
printf("Mobile : 99-9999999999\n");
return 0;
}
Define Macro
#include <stdio.h>
// Macro definition
#define LIMIT 10
// Driver Code
int main()
{
// Print the value of macro defined
printf("The value of LIMIT is %d",LIMIT);
return 0;
}
■ Example
#include <stdio.h>
int main()
{
char ch = 'X';
char str[20] = "www.ewubd.edu";
float pi = 3.14159;
int no = 220;
double dbl = 60.1234567;
printf("Character is %c \n", ch);
printf("String is %s \n" , str);
printf("Float value is %f \n", pi);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("Octal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
return 0;
}
scanf FUNCTION IN C LANGUAGE:
■ In C programming language, scanf() function is used to read character, string, numeric
data from keyboard
■ Consider below example program where user enters a character. This value is assigned
to the variable “ch” and then displayed.
■ Then, user enters a string and this value is assigned to the variable “str” and then
displayed.
■ Example
#include <stdio.h>
int main()
{
char ch;
int a;
char str[100];
printf("Enter any character \n");
scanf("%c", &ch);
printf("Entered integer value of A is: \n");
scanf("%d", &a);
printf("Entered character is %c \n", ch);
printf("Enter any string ( upto 100 character ) \n");
scanf("%s", &str);
printf("Entered string is %s \n", str);
}
Variables in C
■ A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times.
■ In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier).
Let's see the syntax to declare a variable:
int a; // int age, score;
float b;
char c;
Here, a,b,c are variables. The int, float and char are the data types.
Variables
■ A programmer can choose a meaningful variable name. Example : average, height, age,
total etc.
■ We can also provide values while declaring the variables as given below:
int a=10,b=20;//declaring 2 variables of integer type
float f=20.8;
char c='A';
Rules for defining variables
■ A variable can have alphabets, digits, and underscore.
■ A variable name can start with the alphabet, and underscore only. It can't start with a
digit.
■ No whitespace/blank is allowed within the variable name.
■ Upper and lower case names are treated as different, as C is case-sensitive, so it is
suggested to keep the variable names in lower case.
■ A variable name must not be any reserved word or keyword, e.g. int, float, etc.
Valid Variables or Invalid variables
Valid variable names:
■ int a;
■ int _ab;
int number =10; //integer variable
■ int a_30;
Number = 5.5; //error
Invalid variable names:
■ int 2_total;
Double number; //error
■ int a b;
■ int long;
DECLARING & INITIALIZING C VARIABLE:
Declaration of variables must be done before they are used in the
program. Declaration does the following things.
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.
3. Until the variable is defined the compiler doesn't have to worry about
allocating memory space to the variable.
4. Declaration is more like informing the compiler that there exist a variable
with following datatype which is used in the program.
5. A variable is declared using the extern keyword, outside
the main() function.
Variable initialization
■ Variable initialization means assigning a value to the variable.
Type Syntax
data_type variable_name;
Variable declaration Example: int x, y, z; char flag,
ch;
data_type variable_name =
value;
Variable initialization
Example: int x = 50, y = 30;
char flag = ‘x’, ch=’l’;
TYPES OF VARIABLE
There are many types of Variable
■ Local variable
■ Global variable
■ Environment variable
Local Variable
EXAMPLE PROGRAM FOR LOCAL VARIABLE IN C:
■ The Scope Of Local Variables Will Be Within The Function Only.
■ These variables are declared within the function and can’t be accessed outside the function.
■ In the below example, a and b variables are having scope within the main function
#include <stdio.h>
int main () //main function
{
int a=10, b=20; //Local variables
printf(“The value of A =%d and B=%d”, a, b);
return 0;
}
Global Variable
■ The scope of global variables will be throughout the program. These variables can be accessed
from anywhere in the program.
■ This variable is defined outside the main function. So that, this variable is visible to main
function and all other sub functions.
#include <stdio.h>
int main ()
int a=10, b=20; //Global variables
{
printf(“The value of A =%d and B=%d”, a, b); //print function
return 0;
}
Environment variable
■ Environment variable is a variable that will be available for all C applications and C
programs.
■ We can access these variables from anywhere in a C program without declaring and
initializing in an application or C program.
■ The inbuilt functions which are used to access, modify and set these environment
variables are called environment functions.
C Keywords
■ Keywords are predefined, reserved words used in programming that have special meanings
to the compiler. Keywords are part of the syntax and they cannot be used as an identifier.
For example:
auto, double, int, struct, break, else,
long, switch, case, extern, return, union,
enum, register, typedef, char, continue, for,
signed, void, do, if, static, while,
default, goto, sizeof, volatile, const, float,
short, unsigned.
C Identifiers
■ Identifier refers to name given to entities such as variables, functions, structures etc.
■ Identifiers must be unique. They are created to give a unique name to an entity to
identify it during the execution of the program. For example:
int money;
double accountBalance;
■ Here, money and accountBalance are identifiers.
■ Identifier names must be different from keywords. You cannot use int as an identifier
because int is a keyword.
Rules for naming identifiers
1.A valid identifier can have letters (both uppercase and lowercase letters),
digits and underscores.
2.The first letter of an identifier should be either a letter or an underscore.
3.You cannot use keywords like int, while etc. as identifiers.
4.There is no rule on how long an identifier can be. However, you may run
into problems in some compilers if the identifier is longer than 31 characters.
You can choose any name as an identifier if you follow the above rule,
however, give meaningful names to identifiers that make sense.