Você está na página 1de 37

C Programming

Data Types
Why Datatypes ? Data come in different forms integers(1,10,100),decimals(1.3,99.999),character s(@,%,$) Different are required In order to inform compiler to allocate memory for a variable. char -1 byte, int -2 bytes, float -4 bytes . Datatypes: char, int, float, unsigned int,signed int,long int,double

Integer variable
Datatype represented as int An int type variable consumes 2 bytes of memory. int a=25; a is stored as 0000 0000 0010 0101 in the memory denotes 2 bytes memory consumption.

Float variable
Datatype represented as float A float type variable consumes 4 bytes of memory. Float a=25.89; Basically used to store decimal numbers

Character variable
Represented as char A character variable consumes 1 byte of memory. char c=@ ; ASCII value of @ is 64 Correspondingly c is stored in memory as 0110 0100 denotes 1 byte memory consumption

Input Output functions


C defines few Input/Output functions in stdio.h file. scanf(),printf(),getchar(),putchar() are few i/o built in functions. scanf() function is used to read the input from keyboard. printf() function is used to write the output into the monitor/console.

Usage: int a,b; char d=$; scanf(%d%d,&a,&b); printf(a=%d b=%d,a,b); putchar(d);

Operators
Operator is a symbol that tells computer to perform some mathematical or logical operations. Example .. +,-,*,/ ,&&,|| etc Different categories of operators are: arithmetic,relational,logical,assignment,incre ment/decrement.

Arithmetic Operators
+, -, *, / are the arithmetic operators to perform addition, subtraction, multiplication and division respectively. Usage: int a=25,b=30,y; y=a+b; y=a/b; y=a*b;

Relational Operators
Used to make comparison between different data elements. >, >=, <, <=, ==, != are some of the relational operators used for comparing different variables. All the operators return true or false as the result of comparison.

Usage: int a=25,b=23; If(b>a) { play cricket; } If(a!=b) { Play football; }

Logical operators
&& , || , ! are the logical operators for and , or , not logics respectively. These operators are used to perform logical manipulations. All the operators boil down to true or false after the operation is complete.

Usage:

int a=25,b=100; if((a>b)&&(a==25)) { printf(Gone in 60 seconds); } If(!(a==b)||(a<b)) { printf(let us C); }

Assignment Operator
Used to assign some value to the variable. = is the assignment operator. The left hand side of = must be a variable and right hand side must be a value. Usage: int a=10,y; y=a*a/2

DECISION MAKING

IF/ELSE/ELSE IF
If /else statements are used for making simple decisions in C programming. The if statement has a boolean expression returning true or false. There can be a simple if statement or a if followed by else For multiple conditions else if statements can be used.

Usage: ef(a>b) { Action 1; } else { Action 2; }

If(a>b) { Action 1; } else if(a!=0) { Action2; } else { Action 3; }

LOOPING

While loop
Used to iterate/loop over the block of code through a condition. The looping continues until the condition mentioned is true. Control from the Loop exits when the condition mentioned becomes false. Can be thought as indeterminate loop.

Usage: while(a>b) { printf(inside a while loop); } the printf() executes until a>b is true.

Do while loop
Used to iterate/loop over the block of code through a condition in do statement. The looping continues until the condition mentioned is do statement true. The block of code inside do executes atleast once even if the condition is false.

do

{ ++a; } while(a!=b)
The loop runs until a!=b becomes false.

For loop
For loop is used when we want to iterate through for definite number of times. For loop contains variables to control the number of iterations. Control from the loop exits when the mentioned number in loop is matched. Can be thought as a determinate loop.

for(i=0;i<10;i++) { printf(inside for loop); } The printf statement executes for 10 times. The iterations can be determined by setting a limit to i value like i<10, i<100 etc

SWITCHING

Case statements
Switching mechanism in C is used to switch between different cases data elements. The cases can be for integer and character elements. There cannot be duplicate cases. Its recommended to place break statements after each block in a case .

int a=2; switch(a) { case 0: printf(case 0); break; case2: : printf(case 2); break; default:printf(case default); break; }

Arrays

Single dimension arrays


An array is a collection of elements of same data type. int a[10]; 10 is size of array and should be hard coded in the declaration. Size of an array should be a constant value. int b[3]={5,10,8}; declaration and assigning of values can happen at compile time. int c[3]={1.3,4.67,10} is illegal. Cannot have float elements for int declaration as stated in definition

2 dimensional arrays
The array elements are written in column and row format Ex: int array_demo[2][3]={{3,4,7},{6,7,9}}; The above array contains 2 rows 3 columns.

Strings

strings
A String is a collection of characters. max% == ,m, a, x ,%-; char datatype is used to represent the string as there is no built in datatype for strings in C char name*10+=maximum; A \0 character is inserted in the before the string ends to indicate the end of characters.

String manipulation functions


C provides many predefined functions to manipulate the string elements. strcmp(str1,str2)- to compare 2 strings strlen(str)- to calculate length of string strcat(str1,str2) concatenates str2 with str1 strcpy(str1,str2)-copy str2 to str1

Pointers
A pointer is a variable that points to other variable by holding the address of variable it is pointing to. int a=100; int *p= &a; a is a variable. p is the pointer pointing to a &a is the address of memory location where a lives, say &a equals 1000 (memory address) Now p equals 1000 and *p equals 100

int a =25; float *p=&a this is illegal. A float pointer cannot point to a integer variable. One can perform different manipulations using pointers. now int a=25; int *p=&a; int y=0; y= *p+100; now value of y is 125 *p=99; now value of a is 99

Structures
Is an array of elements of different datatypes struct book_bank { char title[20]; int pages; float price; };

Você também pode gostar