Dr A GNANABASKARAN Content by C Programming- Basics of C
Software Development Life Cycle 2
Logical Building Logic building 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. 3
Logical Building 4
Logical Building 5
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 6
Commonly used Predefined Functions main() printf () scanf () Note: Every C statement ends with a semicolon ; The body of int main() could also been written as: int main(){ printf ("Hello World!");return 0;} The compiler ignores white spaces. However, multiple lines makes the code more readable. 12
Escape Sequences 13 Escape Sequence Description \t Creates a horizontal tab \n Creates a new line \v Creates a vertical tab
C Comments Single-line Comments comments start with two forward slashes (//). C Multi-line Comments comments start with /* and ends with */. 14 14
Data Types Data types refer 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 15
Data types used 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 16 Purpose of Data Types 16
Types of Data Types 17 17 Data Type Example of Data Type Primary Data types Floating-point, integer, double, character. Derived Data Types Union, structure, array, etc. Enumerated Data Types Enum Void Data Type Empty Value Bool Type True or False
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 18
Primary Data Types Keyword Used Data Type Memory Allocation int Integer 2 bytes/4 bytes float Floating-point 4 bytes void Void 0 byte char Character 1 byte double Double 8 bytes
Data Type Modifiers data types much more specific Here are a few modifiers: short long unsigned signed
Range of Values of C Data Type 25 Data Type 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
Range of Values of C Data Type 26 Data Type 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
Range of Values of C Data Type 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
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
Keywords / Reserved Words 29
Variables 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. Syntax: type variable_list ; 30 Valid variable names: Invalid variable names: int a; int _ ab ; int a30; int 2; int a b; int long ;
Types of Variables in C Local Variable Global Variable Static Variable Automatic Variable External Variable 31
Local Variable A variable that 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 } 32
Local Variable int function1() { int x=10;//local variable return x; } main() { printf (“%d”,function1()); return 0; } 33
Global Variable A variable that 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 } 34
Global Variable #include < stdio.h > int count = 0; void my_function () { count++; printf ("Function called %d times\n", count); } int main() { my_function (); my_function (); my_function (); return 0; } 35
Static Variable A variable 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; } 36
Automatic Variable All variables 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 } 37
External Variable a variable 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 ); } 38