SlideShare a Scribd company logo
1 of 48
JustETC Education Inc.
 Structure of C Program
 Data Types, Operators, and Expressions
 Control Flow
 Functions and Program Structure
 Pointers and Arrays
 Structures
 Input and Output
 How does a C program look like?
#include <stdio.h>
main()
{
printf("hello, worldn");
}
 What it does?
◦ Displays/prints/echos ‘hello World’
 What do the different sections indicate?
◦ Header Files
◦ Code Blocks, functions
◦ Statements
 How to compile
◦ Depends on the Operating System and IDE you
use
 How to Run?
◦ Depends on the Operating System and IDE you use
Program Structure
 How to compile
◦ Depends on the Operating System and IDE you use
◦ Save the file as hello.c
◦ Linux/Unix: cc hello.c
 How to Run?
◦ Depends on the Operating System and IDE you use
◦ Linux/Unix: a.out
 Windows:
◦ Usually you will use an IDE
◦ IDE will have menu options to compile and run
◦ Commons are: f9, shift+f9, f5 are used to
compile/run.
◦ Menus may have options such as compile, build,
run, debug, build all, auto build
 A program is nothing but a set of instructions to the
computer hardware to perform some operations.
◦ Each line of instructions is called a statement
◦ A semicolon is placed at the end of each statement
◦ You can group a set of instructions by placing them in between { }
◦ You can assign a name to a code block inside { } – then the code block is called
function/procedure/method
◦ You can define your own functions (code blocks)
◦ C also have many functions/code blocks already defined
◦ Hence, a program will consist some blocks of instructions defined by you, or will use
instruction block as defined by C itself
◦ Remember, one function can call /use another function
 As a statement
◦ While calling another function, some information can be passed to the other function
– called Arguments
 printf(“%st%s”, “Hello”, “World”);
 printf(“%dt%f”, 100, 200.50);
 Variables:
◦ Example: x, y, z
◦ x=10, y=100.5
◦ printf(“%dt%f”,x,y);
◦ word=“Hello World”;
◦ printf(“%s”,word);
 More correctly
◦ int x = 10, float y=100.5;
◦ char x[50] = “Hello World”;
◦ int intArr[5] = {1,2,3,4,5};
 Note:
◦ t, b, n
◦ t: print a tab
◦ n: print a newline
 Understanding by Examples
◦ int x=10, y=20, sum=0;
◦ sum = x + y; sum = (x + y); (you can think (x+y) as an
expression)
◦ (x>y) is an expression
◦ ((x+y) > 20) is an expression
 An expression is a statement that has a value. In C,
C++ and C#, an expression is always inside
brackets like these examples. [about.com]
◦ ( y > 4)
◦ ( x== 8)
◦ ( c = 1)
 Write a program that will print 1 if today is
holiday else it will print 0.
◦ If today is holiday printf(“%d”,1);
◦ else if today is not holiday printf(“0”);
 Controlling what the program will do
◦ Control the flow of the instruction block
 Which instruction will do something
 Which instruction will sit idle
 Decide if the same statements will run 100 times or not
if (expression)
statement1
else
statement2
----
if (expression)
statement1
else if (expression)
statement2
else
statement3
if (a > b)
z = a;
else
z = b;
----
if (a > b)
z = a;
else if (b>a)
z = b;
else if (a==b)
x=y;
else
printf(“nothing”);
 Switch-case can be used instead of if then
else sometimes (not always).
◦ If (1==a)
 statement1
◦ else if (2==a)
 Statement2
◦ else if (3==a)
 Statement3
◦ else
 statement4
switch(a){
◦ Case 1:statement1;break;
◦ Case 2:statement2;break;
◦ Case 3:statement3;break;
◦ default:statement4;break;
}
 Fall through: break is required to stop the flow
otherwise it will keep running until it sees a break.
 Default: When no match is found.
switch (expression) {
case const-expr: statements
case const-expr: statements
default: statements
}
 Execute a set of instructions over and over
◦ For specific number of times
◦ As long as an expression is true/satisfied
 For Loop
◦ Specific number of times
 While, and Do-While
◦ As long as an expression is true/satisfied
while (expression){
Statement
}
for (expr1; expr2; expr3){
Statement
}
expr1;
while (expr2) {
statement
expr3;
}
 For
◦ int n=100,i=0;
◦ for (i = 0; i < n; i++){
 printf(”%dn”,i);
◦ }
 While
◦ int n=100,i=0;
◦ while(i <n){
 printf(”%dn”,i);
 i++;
◦ }
 Do-While
◦ int n=100,i=0;
◦ do {
 printf(”%dn”,i);
 i++;
◦ } while(i <n);
 Break
◦ Get out of the current loop
 Continue
◦ Stop executing current iteration and go to next
iteration
 Syntax
◦ Go to Label
 ……
 ………
◦ Label:
 …….
 Operation
◦ Jump to the labeled place from current place
◦ Not encouraged
◦ Rarely can be used to get out of deeply nested
loops.
 What are Pointers?
◦ Note: pointers usually lead to more compact and
efficient code
◦ A pointer is a variable that contains the address of
another variable
◦ If p is a pointer and c is a variable the statement
◦ p = &c;
◦ assigns the address of c to the variable p. & gives
the address of a variable
 The & operator only applies to objects in
memory: variables and array elements.
 Pointers cannot be applied to expressions,
constants, or register variables
 when * is applied to a pointer, it accesses the
object the pointer points to
 Examples
◦ int x = 1, y = 2, z[10];
◦ int *ip; /* ip is a pointer to int */
◦ ip = &x; /* ip now points to x */
◦ y = *ip; /* y is now 1 */
◦ *ip = 0; /* x is now 0 */
◦ ip = &z[0]; /* ip now points to z[0] */
 Every pointer points to a specific data type
◦ one exception:
 a ``pointer to void'‘ (void *p) is used to hold any type
of pointer
 You cannot use dereference with void *
 If p points to the integer x, then *ip can occur
in any context where x could
◦ *ip = *ip + 10;
◦ y = *ip + 1
◦ ++*ip
◦ (*ip)++
 Call by reference applies here
void swap(int *px, int *py)
/* interchange *px and *py */
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
main()
{
swap(x, y);
//The values of x and y will also get changed here
}
 Any operation that can be achieved by array subscripting can
also be done with pointers
◦ int a[10]; int *pa; pa = &a[0];
◦ x = *pa; is equivalent to x=a[0];
◦ pa+1 points to the next element pointed by pa
◦ pa+i points i elements after pa
◦ and pa-i points i elements before
◦ Thus, if pa points to a[0]
 *(pa+1) refers to the contents of a[1],
 pa+i is the address of a[i], and
 *(pa+i) is the contents of a[i]
 Example:
◦ int a[10][20];
◦ int *a[10];
◦ char name[][50] = { "Illegal month", "Jan", "Feb",
"Mar" };
◦ char *name[] = { "Illegal month", "Jan", "Feb", "Mar"
};
 Applies only to char, short, int, and long whether
signed or unsigned
◦ & bitwise AND
◦ | bitwise inclusive OR
◦ ^ bitwise exclusive OR
◦ << left shift
◦ >> right shift
◦ ~ one's complement (unary) (alternate bits)
 Bitwise Operators
◦ & can be used to turn off some bits
◦ x= x & 031, turn off (0) all bits except last five
◦ | is used to turn bits on (1)
◦ ^ sets
 a one in each bit position where its operands have
different bits, and
 zero where they are the same.
◦ << and >> perform left and right shifts of their left
operand
 x >> 2; y << 2;
 x << 2
◦ shifts the value of x by two positions
◦ fills vacated rightmost bits with zero
◦ One left shift multiplies the number by 2
 Right shifting (Signed Number): The vacated left most bits
◦ will be filled with bit signs on some machines – arithmetic
shift
◦ and with 0-bits on others - logical shift
 int getchar(void)
 int putchar(int)
 int printf(char *format, arg1, arg2, ...);
 printf("%.*s", max, s);
 int scanf(char *format, ...) : Read from user/console
 int sscanf(char *string, char *format, arg1, arg2, ...): from
string
 FILE *fp;
 FILE *fopen(char *name, char *mode);
 int fclose(FILE *fp)
 char *fgets(char *line, int maxline, FILE *fp)
 int fputs(char *line, FILE *fp)
 String related functions as defined in string.h
◦ strcat(s,t) concatenate t to end of s
◦ strncat(s,t,n) concatenate n characters of t to end of s
◦ strcmp(s,t) return negative, zero, or positive for s < t, s == t, s > t
◦ strncmp(s,t,n) same as strcmp but only in first n characters
◦ strcpy(s,t) copy t to s
◦ strncpy(s,t,n) copy at most n characters of t to s
◦ strlen(s) return length of s
◦ strchr(s,c) return pointer to first c in s, or NULL if not present
◦ strrchr(s,c) return pointer to last c in s, or NULL if not present
 What is a structure?
◦ It is just a data type/variable that has multiple variables
under it
 Why do we need it?
◦ For example, you are writing a student record
management system. Now to define a student in your
software, you have to keep many variables. Such as id,
name, address, phone, email, major, minor, and some
more.
◦ Yes, you can deal with all these variables for all students
 Better if you had only one variable for each student but as
we see many variables are required to represent a student
 So why not create one variable for a student and put the
other variables under that [to represent that particular
student)
 The top level variable is the structure type
 struct student {
◦ int id;
◦ char *name;
◦ char * address;
◦ char *cell;
 };
 Struct defines a data type. Hence, student
now is a data type. We can define variables of
student type as follows
◦ student studentVar;
 We can declare and assign at the same time
◦ student studentVar = {100,”smith”
,”winnipeg”,”999-0539”};
 We can assign separately as well
◦ student studentVar;
◦ studentVar.id=100;
◦ studentVar.name=“smith”;
 How to print structure variable data?
◦ printf("%d,%s", studentVar.id, studentVar.name);
 Legal operations with structures
◦ Copy it as a unit
◦ Assign to it as a unit
◦ Take it’s address with &
◦ Access it’s members
 struct student studentVar, *pp;
 pp = &studentVar;
 printf(“student details (%d,%s)n", (*pp).id,
(*pp).name);
 printf(“student details (%d,%s)n", pp->id,
pp->name);
 Notice the above two lines. How the member
variables were accessed.
 (*pp).id or pp->id
 struct student{
◦ int id;
◦ char *name;
◦ char *address;
◦ char *cell;
} studentList = {
{100,”name1”,”address1”},
{101,”name2”,”address2”},
{102,”name3”,”address3”}
 };
 Union declaration, assignment may look like pretty
similar to structures
 Unions are usually used to store data of different data
types for the same concept
◦ All member variables points to the same memory location
◦ Actually, we are trying to store one value
◦ But as the data type of the value can differ, hence, we are
keeping options
◦ Usually, the storage will be of the size of the largest data
type
◦ The programmer is responsible to remember and extract
the same data type he stored
 For example, you want to store tags. However, tags
can be of int, float, or string type based on context.
Then you can declare a union as follows
 union tag {
◦ int ival;
◦ float fval;
◦ char *sval;
 } u;
 enum DAY {
saturday,
sunday ,
monday,
tuesday,
wednesday,
thursday,
friday
} workday;
 enum DAY today = wednesday;
 Misc
◦ int ungetc(int c, FILE *fp)
◦ system("date");
 Storage Management
 void *malloc(size_t n)
 void *calloc(size_t n, size_t size)
 int *ip;
 ip = (int *) calloc(n, sizeof(int));
 Mathematical Functions
◦ sin(x) x in radian
◦ cos(x)
◦ atan2(y,x)
◦ exp(x)
◦ log(x) (x>0) , natural
◦ log10(x) (x>0) , 10 based
◦ pow(x,y) x to the power y
◦ sqrt(x)
◦ fabs(x) absolute value of x
 “The C Programming Language”: Kernighan
and Ritchie
 Internet

More Related Content

What's hot

شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثةجامعة القدس المفتوحة
 
1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object Calisthenics1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object CalisthenicsLucas Arruda
 
Swift for TensorFlow - CoreML Personalization
Swift for TensorFlow - CoreML PersonalizationSwift for TensorFlow - CoreML Personalization
Swift for TensorFlow - CoreML PersonalizationJacopo Mangiavacchi
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++Seok-joon Yun
 
RxSwift 시작하기
RxSwift 시작하기RxSwift 시작하기
RxSwift 시작하기Suyeol Jeon
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptSeok-joon Yun
 
JavaScript Design Patterns
JavaScript Design PatternsJavaScript Design Patterns
JavaScript Design PatternsDerek Brown
 
Алексей Кутумов, Вектор с нуля
Алексей Кутумов, Вектор с нуляАлексей Кутумов, Вектор с нуля
Алексей Кутумов, Вектор с нуляSergey Platonov
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...ssuserd6b1fd
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsAlfonso Peletier
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...ssuserd6b1fd
 
Generics and Inference
Generics and InferenceGenerics and Inference
Generics and InferenceRichard Fox
 
Oxygine 2 d objects,events,debug and resources
Oxygine 2 d objects,events,debug and resourcesOxygine 2 d objects,events,debug and resources
Oxygine 2 d objects,events,debug and resourcescorehard_by
 

What's hot (20)

شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
 
1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object Calisthenics1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object Calisthenics
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
C++ L08-Classes Part1
C++ L08-Classes Part1C++ L08-Classes Part1
C++ L08-Classes Part1
 
Swift for TensorFlow - CoreML Personalization
Swift for TensorFlow - CoreML PersonalizationSwift for TensorFlow - CoreML Personalization
Swift for TensorFlow - CoreML Personalization
 
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
c programming
c programmingc programming
c programming
 
RxSwift 시작하기
RxSwift 시작하기RxSwift 시작하기
RxSwift 시작하기
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 
JavaScript Design Patterns
JavaScript Design PatternsJavaScript Design Patterns
JavaScript Design Patterns
 
Алексей Кутумов, Вектор с нуля
Алексей Кутумов, Вектор с нуляАлексей Кутумов, Вектор с нуля
Алексей Кутумов, Вектор с нуля
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
Generics and Inference
Generics and InferenceGenerics and Inference
Generics and Inference
 
Oxygine 2 d objects,events,debug and resources
Oxygine 2 d objects,events,debug and resourcesOxygine 2 d objects,events,debug and resources
Oxygine 2 d objects,events,debug and resources
 
Java script obfuscation
Java script obfuscationJava script obfuscation
Java script obfuscation
 

Viewers also liked

8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED CAman Sharma
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C LanguageMohamed Elsayed
 
Object Oriented Programing - Intro
Object Oriented Programing - IntroObject Oriented Programing - Intro
Object Oriented Programing - IntroBayu Firmawan Paoh
 
Object Oriented Programing - Generic Programing
Object Oriented Programing - Generic ProgramingObject Oriented Programing - Generic Programing
Object Oriented Programing - Generic ProgramingBayu Firmawan Paoh
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For LoopSukrit Gupta
 
Introduction to c_language
Introduction to c_languageIntroduction to c_language
Introduction to c_languageWay2itech
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C ProgrammingSonya Akter Rupa
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programminggajendra singh
 
C LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONSC LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONSBESTECH SOLUTIONS
 

Viewers also liked (20)

Embedded _c_
Embedded  _c_Embedded  _c_
Embedded _c_
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C
 
Structure in c
Structure in cStructure in c
Structure in c
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C Language
 
Mobile Programing
Mobile ProgramingMobile Programing
Mobile Programing
 
Object Oriented Programing - Intro
Object Oriented Programing - IntroObject Oriented Programing - Intro
Object Oriented Programing - Intro
 
Object Oriented Programing - Generic Programing
Object Oriented Programing - Generic ProgramingObject Oriented Programing - Generic Programing
Object Oriented Programing - Generic Programing
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
Session 3
Session 3Session 3
Session 3
 
Session 4
Session 4Session 4
Session 4
 
Lec 10
Lec 10Lec 10
Lec 10
 
Introduction to c_language
Introduction to c_languageIntroduction to c_language
Introduction to c_language
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONSC LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONS
 
Embedded C - Lecture 4
Embedded C - Lecture 4Embedded C - Lecture 4
Embedded C - Lecture 4
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
Embedded c
Embedded cEmbedded c
Embedded c
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Array in C
Array in CArray in C
Array in C
 

Similar to Introduction to c

4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)yap_raiza
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programmingPrabhu Govind
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingPathomchon Sriwilairit
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupSyedHaroonShah4
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfezonesolutions
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptxvijayapraba1
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionSvetlin Nakov
 

Similar to Introduction to c (20)

Tu1
Tu1Tu1
Tu1
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
Introduction to typescript
Introduction to typescriptIntroduction to typescript
Introduction to typescript
 
C++ theory
C++ theoryC++ theory
C++ theory
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
Day 1
Day 1Day 1
Day 1
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Fundamentals of Programming Constructs.pptx
Fundamentals of  Programming Constructs.pptxFundamentals of  Programming Constructs.pptx
Fundamentals of Programming Constructs.pptx
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
2 data and c
2 data and c2 data and c
2 data and c
 

More from Sayed Ahmed

Workplace, Data Analytics, and Ethics
Workplace, Data Analytics, and EthicsWorkplace, Data Analytics, and Ethics
Workplace, Data Analytics, and EthicsSayed Ahmed
 
Python py charm anaconda jupyter installation and basic commands
Python py charm anaconda jupyter   installation and basic commandsPython py charm anaconda jupyter   installation and basic commands
Python py charm anaconda jupyter installation and basic commandsSayed Ahmed
 
[not edited] Demo on mobile app development using ionic framework
[not edited] Demo on mobile app development using ionic framework[not edited] Demo on mobile app development using ionic framework
[not edited] Demo on mobile app development using ionic frameworkSayed Ahmed
 
Sap hana-ide-overview-nodev
Sap hana-ide-overview-nodevSap hana-ide-overview-nodev
Sap hana-ide-overview-nodevSayed Ahmed
 
Will be an introduction to
Will be an introduction toWill be an introduction to
Will be an introduction toSayed Ahmed
 
Whm and cpanel overview hosting control panel overview
Whm and cpanel overview   hosting control panel overviewWhm and cpanel overview   hosting control panel overview
Whm and cpanel overview hosting control panel overviewSayed Ahmed
 
Web application development using zend framework
Web application development using zend frameworkWeb application development using zend framework
Web application development using zend frameworkSayed Ahmed
 
Web design and_html_part_3
Web design and_html_part_3Web design and_html_part_3
Web design and_html_part_3Sayed Ahmed
 
Web design and_html_part_2
Web design and_html_part_2Web design and_html_part_2
Web design and_html_part_2Sayed Ahmed
 
Web design and_html
Web design and_htmlWeb design and_html
Web design and_htmlSayed Ahmed
 
Visual studio ide shortcuts
Visual studio ide shortcutsVisual studio ide shortcuts
Visual studio ide shortcutsSayed Ahmed
 
Unit tests in_symfony
Unit tests in_symfonyUnit tests in_symfony
Unit tests in_symfonySayed Ahmed
 
Telerik this is sayed
Telerik this is sayedTelerik this is sayed
Telerik this is sayedSayed Ahmed
 
System analysis and_design
System analysis and_designSystem analysis and_design
System analysis and_designSayed Ahmed
 
Story telling and_narrative
Story telling and_narrativeStory telling and_narrative
Story telling and_narrativeSayed Ahmed
 

More from Sayed Ahmed (20)

Workplace, Data Analytics, and Ethics
Workplace, Data Analytics, and EthicsWorkplace, Data Analytics, and Ethics
Workplace, Data Analytics, and Ethics
 
Python py charm anaconda jupyter installation and basic commands
Python py charm anaconda jupyter   installation and basic commandsPython py charm anaconda jupyter   installation and basic commands
Python py charm anaconda jupyter installation and basic commands
 
[not edited] Demo on mobile app development using ionic framework
[not edited] Demo on mobile app development using ionic framework[not edited] Demo on mobile app development using ionic framework
[not edited] Demo on mobile app development using ionic framework
 
Sap hana-ide-overview-nodev
Sap hana-ide-overview-nodevSap hana-ide-overview-nodev
Sap hana-ide-overview-nodev
 
Invest wisely
Invest wiselyInvest wisely
Invest wisely
 
Will be an introduction to
Will be an introduction toWill be an introduction to
Will be an introduction to
 
Whm and cpanel overview hosting control panel overview
Whm and cpanel overview   hosting control panel overviewWhm and cpanel overview   hosting control panel overview
Whm and cpanel overview hosting control panel overview
 
Web application development using zend framework
Web application development using zend frameworkWeb application development using zend framework
Web application development using zend framework
 
Web design and_html_part_3
Web design and_html_part_3Web design and_html_part_3
Web design and_html_part_3
 
Web design and_html_part_2
Web design and_html_part_2Web design and_html_part_2
Web design and_html_part_2
 
Web design and_html
Web design and_htmlWeb design and_html
Web design and_html
 
Visual studio ide shortcuts
Visual studio ide shortcutsVisual studio ide shortcuts
Visual studio ide shortcuts
 
Virtualization
VirtualizationVirtualization
Virtualization
 
User interfaces
User interfacesUser interfaces
User interfaces
 
Unreal
UnrealUnreal
Unreal
 
Unit tests in_symfony
Unit tests in_symfonyUnit tests in_symfony
Unit tests in_symfony
 
Telerik this is sayed
Telerik this is sayedTelerik this is sayed
Telerik this is sayed
 
System analysis and_design
System analysis and_designSystem analysis and_design
System analysis and_design
 
Symfony 2
Symfony 2Symfony 2
Symfony 2
 
Story telling and_narrative
Story telling and_narrativeStory telling and_narrative
Story telling and_narrative
 

Recently uploaded

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 

Recently uploaded (20)

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 

Introduction to c

  • 2.  Structure of C Program  Data Types, Operators, and Expressions  Control Flow  Functions and Program Structure  Pointers and Arrays  Structures  Input and Output
  • 3.  How does a C program look like? #include <stdio.h> main() { printf("hello, worldn"); }
  • 4.  What it does? ◦ Displays/prints/echos ‘hello World’  What do the different sections indicate? ◦ Header Files ◦ Code Blocks, functions ◦ Statements  How to compile ◦ Depends on the Operating System and IDE you use  How to Run? ◦ Depends on the Operating System and IDE you use
  • 6.  How to compile ◦ Depends on the Operating System and IDE you use ◦ Save the file as hello.c ◦ Linux/Unix: cc hello.c  How to Run? ◦ Depends on the Operating System and IDE you use ◦ Linux/Unix: a.out  Windows: ◦ Usually you will use an IDE ◦ IDE will have menu options to compile and run ◦ Commons are: f9, shift+f9, f5 are used to compile/run. ◦ Menus may have options such as compile, build, run, debug, build all, auto build
  • 7.  A program is nothing but a set of instructions to the computer hardware to perform some operations. ◦ Each line of instructions is called a statement ◦ A semicolon is placed at the end of each statement ◦ You can group a set of instructions by placing them in between { } ◦ You can assign a name to a code block inside { } – then the code block is called function/procedure/method ◦ You can define your own functions (code blocks) ◦ C also have many functions/code blocks already defined ◦ Hence, a program will consist some blocks of instructions defined by you, or will use instruction block as defined by C itself ◦ Remember, one function can call /use another function  As a statement ◦ While calling another function, some information can be passed to the other function – called Arguments
  • 8.  printf(“%st%s”, “Hello”, “World”);  printf(“%dt%f”, 100, 200.50);
  • 9.  Variables: ◦ Example: x, y, z ◦ x=10, y=100.5 ◦ printf(“%dt%f”,x,y); ◦ word=“Hello World”; ◦ printf(“%s”,word);  More correctly ◦ int x = 10, float y=100.5; ◦ char x[50] = “Hello World”; ◦ int intArr[5] = {1,2,3,4,5};
  • 10.  Note: ◦ t, b, n ◦ t: print a tab ◦ n: print a newline
  • 11.  Understanding by Examples ◦ int x=10, y=20, sum=0; ◦ sum = x + y; sum = (x + y); (you can think (x+y) as an expression) ◦ (x>y) is an expression ◦ ((x+y) > 20) is an expression  An expression is a statement that has a value. In C, C++ and C#, an expression is always inside brackets like these examples. [about.com] ◦ ( y > 4) ◦ ( x== 8) ◦ ( c = 1)
  • 12.  Write a program that will print 1 if today is holiday else it will print 0. ◦ If today is holiday printf(“%d”,1); ◦ else if today is not holiday printf(“0”);  Controlling what the program will do ◦ Control the flow of the instruction block  Which instruction will do something  Which instruction will sit idle  Decide if the same statements will run 100 times or not
  • 14. if (a > b) z = a; else z = b; ---- if (a > b) z = a; else if (b>a) z = b; else if (a==b) x=y; else printf(“nothing”);
  • 15.  Switch-case can be used instead of if then else sometimes (not always). ◦ If (1==a)  statement1 ◦ else if (2==a)  Statement2 ◦ else if (3==a)  Statement3 ◦ else  statement4
  • 16. switch(a){ ◦ Case 1:statement1;break; ◦ Case 2:statement2;break; ◦ Case 3:statement3;break; ◦ default:statement4;break; }  Fall through: break is required to stop the flow otherwise it will keep running until it sees a break.  Default: When no match is found.
  • 17. switch (expression) { case const-expr: statements case const-expr: statements default: statements }
  • 18.  Execute a set of instructions over and over ◦ For specific number of times ◦ As long as an expression is true/satisfied  For Loop ◦ Specific number of times  While, and Do-While ◦ As long as an expression is true/satisfied
  • 19. while (expression){ Statement } for (expr1; expr2; expr3){ Statement } expr1; while (expr2) { statement expr3; }
  • 20.  For ◦ int n=100,i=0; ◦ for (i = 0; i < n; i++){  printf(”%dn”,i); ◦ }  While ◦ int n=100,i=0; ◦ while(i <n){  printf(”%dn”,i);  i++; ◦ }
  • 21.  Do-While ◦ int n=100,i=0; ◦ do {  printf(”%dn”,i);  i++; ◦ } while(i <n);
  • 22.  Break ◦ Get out of the current loop  Continue ◦ Stop executing current iteration and go to next iteration
  • 23.  Syntax ◦ Go to Label  ……  ……… ◦ Label:  …….  Operation ◦ Jump to the labeled place from current place ◦ Not encouraged ◦ Rarely can be used to get out of deeply nested loops.
  • 24.  What are Pointers? ◦ Note: pointers usually lead to more compact and efficient code ◦ A pointer is a variable that contains the address of another variable ◦ If p is a pointer and c is a variable the statement ◦ p = &c; ◦ assigns the address of c to the variable p. & gives the address of a variable
  • 25.  The & operator only applies to objects in memory: variables and array elements.  Pointers cannot be applied to expressions, constants, or register variables  when * is applied to a pointer, it accesses the object the pointer points to
  • 26.  Examples ◦ int x = 1, y = 2, z[10]; ◦ int *ip; /* ip is a pointer to int */ ◦ ip = &x; /* ip now points to x */ ◦ y = *ip; /* y is now 1 */ ◦ *ip = 0; /* x is now 0 */ ◦ ip = &z[0]; /* ip now points to z[0] */
  • 27.  Every pointer points to a specific data type ◦ one exception:  a ``pointer to void'‘ (void *p) is used to hold any type of pointer  You cannot use dereference with void *  If p points to the integer x, then *ip can occur in any context where x could ◦ *ip = *ip + 10; ◦ y = *ip + 1 ◦ ++*ip ◦ (*ip)++
  • 28.  Call by reference applies here void swap(int *px, int *py) /* interchange *px and *py */ { int temp; temp = *px; *px = *py; *py = temp; } main() { swap(x, y); //The values of x and y will also get changed here }
  • 29.  Any operation that can be achieved by array subscripting can also be done with pointers ◦ int a[10]; int *pa; pa = &a[0]; ◦ x = *pa; is equivalent to x=a[0]; ◦ pa+1 points to the next element pointed by pa ◦ pa+i points i elements after pa ◦ and pa-i points i elements before ◦ Thus, if pa points to a[0]  *(pa+1) refers to the contents of a[1],  pa+i is the address of a[i], and  *(pa+i) is the contents of a[i]
  • 30.  Example: ◦ int a[10][20]; ◦ int *a[10]; ◦ char name[][50] = { "Illegal month", "Jan", "Feb", "Mar" }; ◦ char *name[] = { "Illegal month", "Jan", "Feb", "Mar" };
  • 31.  Applies only to char, short, int, and long whether signed or unsigned ◦ & bitwise AND ◦ | bitwise inclusive OR ◦ ^ bitwise exclusive OR ◦ << left shift ◦ >> right shift ◦ ~ one's complement (unary) (alternate bits)
  • 32.  Bitwise Operators ◦ & can be used to turn off some bits ◦ x= x & 031, turn off (0) all bits except last five ◦ | is used to turn bits on (1) ◦ ^ sets  a one in each bit position where its operands have different bits, and  zero where they are the same. ◦ << and >> perform left and right shifts of their left operand  x >> 2; y << 2;
  • 33.  x << 2 ◦ shifts the value of x by two positions ◦ fills vacated rightmost bits with zero ◦ One left shift multiplies the number by 2  Right shifting (Signed Number): The vacated left most bits ◦ will be filled with bit signs on some machines – arithmetic shift ◦ and with 0-bits on others - logical shift
  • 34.  int getchar(void)  int putchar(int)  int printf(char *format, arg1, arg2, ...);  printf("%.*s", max, s);  int scanf(char *format, ...) : Read from user/console  int sscanf(char *string, char *format, arg1, arg2, ...): from string
  • 35.  FILE *fp;  FILE *fopen(char *name, char *mode);  int fclose(FILE *fp)  char *fgets(char *line, int maxline, FILE *fp)  int fputs(char *line, FILE *fp)
  • 36.  String related functions as defined in string.h ◦ strcat(s,t) concatenate t to end of s ◦ strncat(s,t,n) concatenate n characters of t to end of s ◦ strcmp(s,t) return negative, zero, or positive for s < t, s == t, s > t ◦ strncmp(s,t,n) same as strcmp but only in first n characters ◦ strcpy(s,t) copy t to s ◦ strncpy(s,t,n) copy at most n characters of t to s ◦ strlen(s) return length of s ◦ strchr(s,c) return pointer to first c in s, or NULL if not present ◦ strrchr(s,c) return pointer to last c in s, or NULL if not present
  • 37.  What is a structure? ◦ It is just a data type/variable that has multiple variables under it  Why do we need it? ◦ For example, you are writing a student record management system. Now to define a student in your software, you have to keep many variables. Such as id, name, address, phone, email, major, minor, and some more. ◦ Yes, you can deal with all these variables for all students  Better if you had only one variable for each student but as we see many variables are required to represent a student  So why not create one variable for a student and put the other variables under that [to represent that particular student)  The top level variable is the structure type
  • 38.  struct student { ◦ int id; ◦ char *name; ◦ char * address; ◦ char *cell;  };  Struct defines a data type. Hence, student now is a data type. We can define variables of student type as follows ◦ student studentVar;
  • 39.  We can declare and assign at the same time ◦ student studentVar = {100,”smith” ,”winnipeg”,”999-0539”};  We can assign separately as well ◦ student studentVar; ◦ studentVar.id=100; ◦ studentVar.name=“smith”;
  • 40.  How to print structure variable data? ◦ printf("%d,%s", studentVar.id, studentVar.name);  Legal operations with structures ◦ Copy it as a unit ◦ Assign to it as a unit ◦ Take it’s address with & ◦ Access it’s members
  • 41.  struct student studentVar, *pp;  pp = &studentVar;  printf(“student details (%d,%s)n", (*pp).id, (*pp).name);  printf(“student details (%d,%s)n", pp->id, pp->name);  Notice the above two lines. How the member variables were accessed.  (*pp).id or pp->id
  • 42.  struct student{ ◦ int id; ◦ char *name; ◦ char *address; ◦ char *cell; } studentList = { {100,”name1”,”address1”}, {101,”name2”,”address2”}, {102,”name3”,”address3”}  };
  • 43.  Union declaration, assignment may look like pretty similar to structures  Unions are usually used to store data of different data types for the same concept ◦ All member variables points to the same memory location ◦ Actually, we are trying to store one value ◦ But as the data type of the value can differ, hence, we are keeping options ◦ Usually, the storage will be of the size of the largest data type ◦ The programmer is responsible to remember and extract the same data type he stored  For example, you want to store tags. However, tags can be of int, float, or string type based on context. Then you can declare a union as follows
  • 44.  union tag { ◦ int ival; ◦ float fval; ◦ char *sval;  } u;
  • 45.  enum DAY { saturday, sunday , monday, tuesday, wednesday, thursday, friday } workday;  enum DAY today = wednesday;
  • 46.  Misc ◦ int ungetc(int c, FILE *fp) ◦ system("date");  Storage Management  void *malloc(size_t n)  void *calloc(size_t n, size_t size)  int *ip;  ip = (int *) calloc(n, sizeof(int));
  • 47.  Mathematical Functions ◦ sin(x) x in radian ◦ cos(x) ◦ atan2(y,x) ◦ exp(x) ◦ log(x) (x>0) , natural ◦ log10(x) (x>0) , 10 based ◦ pow(x,y) x to the power y ◦ sqrt(x) ◦ fabs(x) absolute value of x
  • 48.  “The C Programming Language”: Kernighan and Ritchie  Internet