C library = Header files + Reserved words + main method

Similar documents
M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

First of all, it is a variable, just like other variables you studied

Unit 3 Decision making, Looping and Arrays

Introduction to C. Systems Programming Concepts

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Decision Making -Branching. Class Incharge: S. Sasirekha

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

IV Unit Second Part STRUCTURES

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Basics of Programming

DEPARTMENT OF MATHS, MJ COLLEGE

C: How to Program. Week /Mar/05

BLM2031 Structured Programming. Zeyneb KURT

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

6.096 Introduction to C++ January (IAP) 2009

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath


Chapter 2 - Introduction to C Programming

C Syntax Out: 15 September, 1995

Q1. Multiple Choice Questions

Chapter 1 & 2 Introduction to C Language

Questions Bank. 14) State any four advantages of using flow-chart

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Principles of Programming. Chapter 6: Arrays

Fundamental of Programming (C)

C Programming Review CSC 4320/6320

Fundamentals of Programming

A Fast Review of C Essentials Part I

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Introduction to the C Programming Language

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Introduction to C Language (M3-R )

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Principles of C and Memory Management

Introduction. C provides two styles of flow control:

Programming, numerics and optimization

C Language, Token, Keywords, Constant, variable

Government Polytechnic Muzaffarpur.

Procedures, Parameters, Values and Variables. Steven R. Bagley

MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

M1-R4: Programing and Problem Solving using C (JAN 2019)

Flow Chart. The diagrammatic representation shows a solution to a given problem.

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Computers Programming Course 6. Iulian Năstac

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C - Basic Introduction

CS 61C: Great Ideas in Computer Architecture Introduction to C

Presented By : Gaurav Juneja

Lecture 02 C FUNDAMENTALS

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Q 1. Attempt any TEN of the following:

Fundamental of C programming. - Ompal Singh

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

C Programming Class I

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Data Types and Variables in C language

Creating a C++ Program

ARRAYS(II Unit Part II)

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

A flow chart is a graphical or symbolic representation of a process.

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

Operators and Expressions:

Dynamic Memory Allocation

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Technical Questions. Q 1) What are the key features in C programming language?

Review of the C Programming Language for Principles of Operating Systems

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CSCI 171 Chapter Outlines

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

Variation of Pointers

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

COMPUTER APPLICATION

'C' Programming Language

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

C Programming Multiple. Choice

Multiple Choice Questions ( 1 mark)

Arrays and Pointers in C. Alan L. Cox

Data Type Fall 2014 Jinkyu Jeong

ET156 Introduction to C Programming

Content. In this chapter, you will learn:

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Transcription:

DAY 1: What are Libraries and Header files in C. Suppose you need to see an Atlas of a country in your college. What do you need to do? You will first go to the Library of your college and then to the Geography shelf and then take out the Atlas of the country you need. Same happens in C language: You first go to the C library use a Header file which you need and then use the functions that are defined in that header file. C library = Header files + Reserved words + main method C library thus contains reserved words, a main method and set of predefined header files that contains predefined functions. You can also create your own header file which will be told later to you. Header files contain various functions or methods. However, Main() method doesn t have any header file. Starting with the C programming: DAY 1: Program #1: A HELLO WORLD PROGRAM..!! The concept of writing the first program as hello world program in every language is that the language evolves in this world and says hello to this new world just like a new born baby appears in a new world. Leaving these weightless talks let s gets started: #include<stdio.> // #include is a pre-processor directive. * void main() // execution of a program starts from main. ** printf( Hello World..!! ); // Used to print Hello World on the console - output window * Thus #include is used to include the header files stored in the include folder of turboc. < > These are used to search the file in the include directory skipping the current one. Thus it is used mainly for system headers. These are used to search the header file in the current directory where the source file is located and then in the include directory. These are used mainly for user-defined header files. Datatypes in C 1. Built-In datatypes a. Fundamental Datatype i. Integer Type: char, int, short, long ii. Floating Type: float, double, long double b. Derived Datatype : Array, Pointers, Structures, Unions 2. User Defined Datatypes: typedef, enum

Datatypes in c Explained 1.a Fundamental Datatypes: (Very Important) 1. Char: 1 Byte = 8 Bits So Range = 2 8 = 256 Values a. Signed Char: -2 7 to 2 7-1 (which also makes 256 values) b. Unsigned Char: 0 to 255 (which also makes 256 values) 2. Int: 2 Bytes = 8 *2=16 bits So range = 2 16 = 65536 a. Signed int:- -2 15 to 2 15-1 (which also makes total of 2 16 or 65536 values) b. Unsigned int: 0 to 2 16-1 (which also makes total of 2 16 or 65536 values) 3. Short: 2 Bytes = 16 Bits = 2 16 Values a. short (default) : -2 15 to 2 15-1 or -32,768 to 32,767 b. Unsigned Short : 0 to 2 16-1 or 0 to 65,535 4. Long: 4 bytes = 32 Bits a. Long(default) : : -2 31 to 2 31-1 or -2,147,483,648 to 2,147,483,647 b. Unsigned Long: 0 to 2 32-1 or 0 to 4,294,967,295 Day 1: Program #2: C program to check the size of datatypes using sizeof operator. #include<stdio.h> void main() printf( Storage Size of char is = %d, sizeof(char)); printf( \n Storage size of int is= %d, sizeof(short)); printf( \n Storage size of short is= %d, sizeof(int)); printf( \n Storage size of long is= %d, sizeof(long)); printf( \n Storage size of long long is= %d, sizeof(long long)); Output: Storage Size of char is = 1 Storage Size of short is = 2 Storage Size of int is = 2 Storage Size of long is = 4 Storage Size of long long is = 8 However the size of datatype also depends on the compiler you are using. It may happen that if you are using old compiler turbo c the output may be same but if you are using IDE like codeblocks on a 64 bit architecture then you can get different output. *THIS SECTION IS NOT FOR BEGINNERS For every compiler:- Size of char < size of short < size of int < size of long < size of long long

Storage method- Char 8 bits 1 Byte Short 16 bits 2 Bytes Int 32 bits 4 Bytes Long 32 bits for32 bit-environment, 64 bit for 64 bit environment. 4 Bytes 8 Bytes Long Long 64 bit 8 Bytes So OUTPUT FOR PROGRAM #1 Result on 32 Bit-environment OR On 32 Bit TURBO C Storage Size of char is = 1 Storage Size of short is = 2 Storage Size of int is = 2 Storage Size of long is = 4 Storage Size of long long is = 8 Result on 64 Bit-environment OR On IDE- Codeblocks/Visual Studio Storage Size of char is = 1 Storage Size of short is = 2 Storage Size of int is = 4 Storage Size of long is = 8 Storage Size of long long is = 8 DAY 1: Program #3: Write a program to know the range of Datatypes Int, char, short, long. #include<limits.h> #include<stdio.h> void main() printf( Range of short int is:%d to %d,shrt_min,shrt_max); printf( \nsize of Unsigned Short int is:%d,ushrt_max); printf( \nrange of int is : %d to %d,int_min,int_max); printf( \nsize of Unsigned int is:%d,uint_max); printf( \nrange of Long is : %d to %d,long_min,long_max); printf( \nsize of Unsigned Long is:%d,ulong_max); Derived Datatypes: Array, Pointers, Structures, Unions 1. Array: A sequence or collection of same type of variables is an array. 2. Pointers: A variable that stores address to other variable or points to some memory address. 3. Structures: A collection of related variables of same or different Datatypes. 4. Unions: Almost same as Structures but the memory allocation and syntax is different. 5. String: Collection of characters is called a string. There is no Datatype named String in C.

[FURTHER DESCRIPTION OF DERIVED DATATYPES WILL BE GIVEN LATER] Operators in C 1. Arithmetic Operators ( +, -, *, /, %, ++, - -) 2. Logical Operators (&&,,!) 3. Relational Operators (==,!=, >, <, >=, <=) 4. Bitwise Operators ( &,, ^, ~, <<, >> ) 5. Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, = ) 6. Unary Operators ( ~,!, ++, --, sizeof(), &,*) Here; & address of operator * pointer to a variable Control Statements 1. Decision Control Statements (if, if-else, switch, Ternary) 2. Looping Statements (while, do while, for) 3. Loop Control Statements (Break, continue, goto) 1. Condition Control Statements a. If b. If-else c. Switch d. Ternary IF STATEMENT IN C Suppose you want to find whether a number is even or odd. All you need to do is put a condition to check for even or odd number and print The message Number is Even if condition is true, else print message- Number is odd For a number to be even it is checked if it is divisible by 2 or not

Let the number be int a; then (a%2==0) means that number is divisible by 2 i.e. it is even Day 1: Program #4: Program for finding whether a number is even or odd in c will be: #include<stdio.h> int a; // Declaration of a variable. printf( enter a number ); scanf( %d,&a) // Scanf is used to input a number from user and this number is stored in variable a if(a%2==0) printf( Number is even ); else printf( number is odd ); More examples of if: Question: If users favourite color is Blue then Print You are cool #include<stdio.h> char *color; // pointer is used for string type of input. printf( Enter your favourite colour ); scanf( %s,color); // in pointer &(address of) operator is not used; if(color== Blue ) printf( You are cool ); Note: Now A condition above can arise that input (user s favourite colour is neither blue or Black then comes the use of if-else. If user s choice is Blue,then print You are cool OTHERWISE print You have a bad choice. #include<stdio.h> char *color; printf( Enter a color of you choice ); scanf( %s,color); //%s format specifier for string. if(color== Blue ) printf( You are cool ); else printf( You have bad choice );

Nested if-else Syntax: if (condition1) Statement 1; if(condition2) Statement2; else if(condition3) Statement3; Switch Statement in C Switch Statement is almost same as nested if and also provides better readability and handling. Switch statement is used with the break statement to break the continuity of the cases. Syntax: switch( case ) case 1: Statement 1; break; case 2: Statement 2; break; case 3: Statement 3; break; default: Statement 4; When none of the case is matched then the statements written in default are executed. If the case 2 should not get executed after case 1 then Break should be used between them. Example: Write a program to accept the name of a user. If the user name starts with a vowel then print You are Unique else print Welcome. #include<stdio.h> char *name, ch; // ch is a variable that will be used to store the first character in the username printf( Enter You name ); scanf( %s,name); ch=name[0]; //this stores the first character(index zero ) of name to variable ch switch( ch )

case a : case e : case i : case o;: case u : printf( You are Unique ); break; default: printf( Welcome ); If name = aryan. Then ch will store a (name[0]). Switch will jump to case a and then go through all cases e, i, o, u and print the message You are unique. However you can print the name in case a but since you have to print the same Message for all the 5 cases thus you can avoid break statement and write the message in the last case as eventually the last case will be reached and statements residing in that will be executed. Ternary operator in C Again a condition control statement and an alternate to if else this is used for mainly single if else however ternary can also be nested. Syntax: (condition)? (St1 if true) : (St2 If false) Example: Lets consider the same example of odd or even number. Let a be a number. Then, to check and print the number what we do is: 2. Looping Statements: (a%2==0)? printf( Even ) : printf( Odd ) ; (a%2==0) condition printf( Even ) true printf( Odd ) false FOR LOOP This is the most commonly used looping statement in any language as it is used when number of iterations is known to us. Syntax: for (initialisation; condition; updation) Statement 1; Statement 2; Example: Write a program for find the sum of first n natural numbers. (level: beginners) Solution: Now since the iteration is known : initialise i=-1; condition: uptill n i <= n (because we need n numbers) updation: i=i+1; we will have 1,2,3,4,5,6...n

IMP: Now decide what do you want to do with this iteration: (1,2,3,4,5...n) 1. You can print the values generated by i ( will result in printing the series 1,2,3...) 2. You can sum up the values of i (will result in Sum=sum+i; 1+2+3+4+5+6...) 3. Anything else which can be done by this type of iteration with your logics. 4. Here i is called loop variable. So, int i, n; // You need two variables one for running the loop and other // Accept the value of n from user... scanf( %d,&n); for(i=0;i<n;i++) printf( %d, i ); // this will print the series- 1 2 3 4 5 6 7... n return 0; Day 1: Program #5: Write a program to print the sum of n odd numbers, where n is given by user. Important: Observe the logic implemented here carefully. There are 2 solutions provided. Solution 1: int i,sum=0,n; scanf( %d,&n); for(i=1;i<2*n;i=i+2) // This is your logic, you can have you own logic. sum=sum+i; printf( sum of first n odd numbers is= %d,sum); return 0; Solution 2: return 0; // In this the loop variables i goes 1,2,3,4,5... int i, sum=0,n,k=1; thus you have to use different variable for scanf( %d,&n); finding odd number series and its sum for(i=1;i<=n;i++) sum=sum+k; k=k+2; //Thus k goes 1,3,5,7,9... which was done by variable i in Sol. 1

Array in C Array in c is a collection of same type of variables. Suppose you have to store 10 integer values then you need 10 variables, to overcome this situation arrays are used in which you can store 10 different values and denote them with a single variable name. A = 10 13 14 15 8 3 A[0] A[1] A[2] A[3] A[4] A[5] Here, A[] is an array that contains 6 elements and the first element A[0] is 10. The indexing can start from 0, 1 or any number upto depending upon the size of array. SYNTAX: Datatype ArrayName[size]; Example: int A[10]; Thus A[] array can have maximum of 10 elements. However this method of declaration wastes a lot of memory as it allocates the space of 10 elements here- 20 Bytes (as each element is of 2 bytes:int), Thus Dynamic memory allocation is used which is much efficient as it allocates that memory which is required and the memory unused is freed. Also the memory can be increased or decreased later. You will study more about Dynamic memory allocation in c later. Day1: Program #4: Write a program to print the numbers stored in an array given by a user. Directions: 1. So, first you will store the numbers given by user in an array. 2. Then you will print them using a looping statement- for or while Solution: #include <stdio.h> int array[10],n,i; //where array[10] can store 10 elements and n is used for No. of elements. printf( enter the number of values you want to store: ); scanf( %d,&n); for(i=0;i<n;i++) scanf( %d,&array[i]); //this will store the user given element at i th position of the array. //loop for printing the elements for(i=0;i<n;i++) //there is no need to use brackets since loop contains only 1 statement printf( %d,array[i]); return 0;

Types of Array: 1. One Dimensional Array 2. Double Dimension Array 3. Multi Dimensional Array Multi dimensional array are those with multiple rows and multiple column. Ex. Two dimensional arrays, three dimensional arrays, matrices Single Dimension Array 1 3 4 7 11 9 12 20 24 30 Array having one row and multiple column or assume vice-versa; int arr[10]; for(i=0;i<10;i++) scanf( %d,&arr[i]); //thus number given by user as input will be saved at i th index of array arr[]; Double Dimension Array A double dimensional array is a m x n array where: m number of rows n number of columns 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Thus, m x n array can be stored using a nested for loop. (While loop can also be used but it is inconvenient since number of iterations is known to us so it s better to use for loop. Read: Difference between while and for loop). The first for loop is for rows and the second for loop is for columns. Here We will often use the word MATRIX instead of double dimension array as its one and the same thing. //Program to accept a double dimensional array int arr[20][20], i, j, m, n; // Ask the user for the size of MATRIX or Double Dimension array printf( Enter the number of rows in a matrix ); scanf( %d,&m); printf( Enter the number of columns in matrix ); scanf( %d,&n); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf( %d,&arr[i][j]); Same way you can print the double dimension array // program to print the double dimensional array

for(i=0;i<m;i++) for(j=0;j<n;j++) printf( %d,arr[i][j]); printf( \n ); The above \n escape sequence is used to change the line as soon as all the n elements of a row are printed to come to next row. This is a very common mistake not using \n during printing. -------------------------------------------FURTHER TUTORIALS COMING SOON ----------------------------------------