3
Logical Building
Logicbuilding skills are essential for programs.
Logic building is the process of developing logical thinking and problem-solving skills.
If one wishes to be a programmer, he or she needs to keep getting better at developing logic.
Complex and sophisticated algorithms require advanced logic in order for programmers to work with
them.
Developers must also learn about data structures, algorithms and programming paradigms.
Another requisite for acquiring logic building skills is being comfortable with solving problems daily.
6
Enhancing Programming Logic
Practice writing a lot of code
Check solutions by other people
Work out solutions
Keep learning new things
Be consistent
Face problems head-on
12
Commonly used PredefinedFunctions
main()
printf()
scanf()
Note:
1. Every C statement ends with a semicolon ;
2. The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
3. The compiler ignores white spaces. However, multiple lines makes the code more readable.
14
C Comments
Single-line Comments
comments start with two forward slashes (//).
C Multi-line Comments
comments start with /* and ends with */.
14
15.
15
DataTypes
Data typesrefer to the type of data that we are using in a C program.
Whenever we utilize a data type in a C program, we define the variables or functions
used in it.
the type of data that is in use, so that the compiler knows exactly what type of data
it must expect from the given program.
must use a format specifier
15
16.
16
Data typesused in C language refer to an extensive system that we use to declare
various types of functions or variables in a program.
the space that it occupies in storage, along with the way in which the stored bit
pattern will be interpreted
Purpose of DataTypes
16
17.
17
Types of DataTypes
17
DataTypeExample of DataType
Primary Data types Floating-point, integer, double, character.
Derived DataTypes Union, structure, array, etc.
Enumerated DataTypes Enum
Void DataType EmptyValue
Bool Type True or False
18.
18
Enumeration (or enum)
Enumeration (or enum) is a user defined data type in C.
It is mainly used to assign names to integral constants, the names make a program
easy to read and maintain
18
18
DataType Modifiers
datatypes much more specific
Here are a few modifiers:
short
long
unsigned
signed
25.
Range of Valuesof C DataType
25
DataType Format Specifier Minimal Range Typical Bit Size
unsigned char %c 0 to 255 8
char %c -127 to 127 8
signed char %c -127 to 127 8
int %d, %i -32,767 to 32,767 16 or 32
unsigned int %u 0 to 65,535 16 or 32
signed int %d, %i Same as int Same as int
16 or 32
short int %hd -32,767 to 32,767 16
26.
Range of Valuesof C DataType
26
DataType Format Specifier Minimal Range Typical Bit Size
unsigned short int %hu 0 to 65,535 16
signed short int %hd Same as short int 16
long int %ld, %li -2,147,483,647 to
2,147,483,647
32
long long int %lld, %lli -(263 – 1) to 263 – 1 (It
will be added by the C99
standard)
64
signed long int %ld, %li Same as long int 32
unsigned long int %lu 0 to 4,294,967,295 32
unsigned long
long int
%llu 264 – 1 (It will be added
by the C99 standard)
64
27.
Range of Valuesof C DataType
27
float %f 1E-37 to 1E+37 along with six digits of the
precisions here
32
double %lf 1E-37 to 1E+37 along with six digits of the
precisions here
64
long double %Lf 1E-37 to 1E+37 along with six digits of the
precisions here
80
28.
Out of Range
#include <stdio.h>
int main()
{
// the maximum value allowed in the signed short int is 32767
signed short int x = 34767;
return 0;
}
The generated output for this program would be:
warning: very large integer value implicitly truncated to signed type [-Woverflow]
signed short int x = 34767;
28
Variables
A variableis a name of the memory location.
It is used to store data. Its value can be changed, and it can be reused many times.
Syntax:
type variable_list;
30
Valid variable names: Invalid variable names:
int a;
int _ab;
int a30;
int 2;
int a b;
int long;
31.
Types of Variablesin C
Local Variable
GlobalVariable
Static Variable
AutomaticVariable
External Variable
31
32.
32
LocalVariable
A variablethat is declared inside the function or block is called a local variable.
It must be declared at the start of the block.
Example:
void function1()
{
int x=10;//local variable
}
34
GlobalVariable
A variablethat is declared outside the function or block is called a global variable.
Any function can change the value of the global variable. It is available to all the functions.
It must be declared at the start of the block.
Example:
int value=20;//global variable
void function1()
{
int x=10;//local variable
}
35.
35
GlobalVariable
#include <stdio.h>
int count= 0;
void my_function()
{
count++;
printf("Function called %d timesn", count);
}
int main()
{
my_function();
my_function();
my_function();
return 0;
}
36.
36
Static Variable
Avariable that is declared with the static keyword is called static variable.
It retains its value between multiple function calls.
#include <stdio.h>
int main()
{
printf("%d",func());
printf("n%d",func());
return 0;
}
int func()
{
static int count=0;
count++;
return count;
}
37.
37
Automatic Variable
Allvariables in C that are declared inside the block, are automatic variables by
default.We can explicitly declare an automatic variable using auto keyword.
Example:
void main()
{
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
38.
38
External Variable
avariable in multiple C source files by using an external variable.To declare an external variable,
you need to use extern keyword.
extern int x=10;//external variable (also global)
Note: extern only declares the variable but it doesn't allocate any memory for this variable.
program1.c
#include "myfile.h"
#include <stdio.h>
void printValue()
{
printf("Global variable: %d", global_variable);
}