All posts by admin

C functions

Functions are easy to use. They allow complicated programs to be break  into small blocks, each of which is easier to write, read, and maintain. We have already used the function main and scanf, printf . where main is used-defined function and scanf and printf are library functions. main() is a function from there program execution starts.

The functions defined by user itself is known as user defined function.Function used from external files or library known as library functions. printf , scanf , getchar(), gets(), puts() are library functions.

 Function has following layout.

1. Function returning value

return-type   function-name( argument-list-if-you-want){

statements;

statements;

return return-value;

}

2. Function no return-value

void  function-name( arguments-list-if-you-want){

statements;

statements;

}

Return type :

Return type refers to the type of value which is returned by function.

eq. if function return integer value then return type should be int.

Example :

A function to add 2 integer values.

above code will print 8 as output.

 

 

C loops

If you have some situation where you have to executed some set of statements multiple time. There you need loop. In looping set of statements are executed untill the loop termination condition is satisfied.

A loop consist of two segments. One for body and other for condition.

if condition is true then body part of loop will be executed otherwise loop will be terminated.

C programming provides following loop statements:

1. for

2. while

3. do while

Loop Name

Syntax

Description

for

For( exp1;exp2 or condition;exp3){ 

Statements;

……;  }

exp1-> Variable initialization

exp2-> Conditon checking

exp3-> increment or decrement

while

While(condition){ 

Statements; }

relational operators are used to check conditions

do .. while

do { 

Statements;

}while(conditon);

do while loop executed atleast one time befor condition check.

Examples :

For loop : Printing 1 to 10 numbers

While loop: Adding 1 to 10 numbers

Do while loop: printing even nos between 1 to 10

 

C input output

When we say for input in programming language it is about getting data from user (command line ) or from file. C programming provides various function for inputting the data. We will talk about mostly used library <stdio.h> and the functions scanf printf.

Before we go for input and output we will learn about format specifier.

The below table shows the format specifier used by scanf and printf. The “Format specifier” is the sequence passed as the formatting string argument.

Format specifier Characters matched Argument type
%c any single character char
%d, %i integer integer type
%u integer unsigned
%o octal integer unsigned
%x, %X hex integer unsigned
%e, %E, %f, %g, %G floating point number floating type
%p address format void *
%s any sequence of non-whitespace characters,strings char

scanf function:

The int scanf(const char * format, …. ) function reads input from standard input stream stdin (keyboard) and stores the input according to format provided.

Example :

Q. Write a format specifier in scanf function to read two inputs from keyboard.

Ans :

&a , &b refers to the respective addresses.

 

printf Function:

The int scanf(const char * format, …. ) function writes output to the standard output stream stdout (screen) and prints output to the screen according to format provided.

Example:

Q. Write a printf format specifier to print two variables int and double values respectively.

Output:

a=10 b=11.10

C Conditional Statement

C programming supports following conditional statement and operators.

Statement Description
if () { } An if statement consist of a condition statement followed by one or more statement. Statements can be exexuted when condition is true.
If() { } else { } An if statement can be followed by an else statement which can be executed when condion is false.
If (){ } else if () { } …  else  { } An if else ladder . this can will execute only that statement whose condition is true  otherwise else condition will be executed default.
 Switch () { case .. :   … .. } A switch statement is used to check for equality of variable again set of case values.
? : Conditional operator

Example 1:

 

Output:

a has value as 10

Example 2:

output :

a  and b are not equal

Example 3:

output :

Monday

Example 4:

Output:

Friday

Default statement will be executed then only when no case is matched.

C operators and expressions

An operator is a symbol that tells computer to perform certain mathematical and logical operations on identifier. Operators are used in program to manipulate data and variables. C language provide following type of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operator
  • Increment and Decrement Operators
  • Conditional Operators
  • Misc Operators

We will go through all operators one by one.

All below examples are performed on integer variable, output may differ on float variable.

Arithmetic Operators :

Consider A=5 ,B=2 .

S.No. Operator Operation Example
1. + Addition A+B will give 7
2. Subtraction A-B will give 3
3 * Multiplication A*B will give 10
4 / Divide A/B will give 2
5 % Modulo division A%B will give 1

Relational Operators :

S.No. Operator Example Output
1. > A>B True
2. < A<B False
3. >= A>=B True
4. <= A<=B False
5. == A==B False
6. != A!=B True

Logical Operators :

Let A=1,B=0

S.No. Operator Description Example
1. && AND operator A && B will give False (0)
2. || OR Operator A||B will give True (1)
3. ! Not Operator !A will give False (0)

Bitwise Operators:

 x

y

 x|y

x & y

x ^ y

 Operator  Explanation

0

0

0

0

0

&  Bitwise AND

0

1

1

0

1

|  Bitwise OR

1

0

1

0

1

~  Bitwise NOT

1

1

1

1

0

^  XOR
<<  Left Shift
>>  Right Shift

Assignment Operators :

Operator
Example
 Explanation 
 
Simple assignment operator
 
 =
sum = 10
Value 10 is assigned to variable sum
Compound assignment operator 
+=
sum+=10
This is same as  sum = sum+10
 -=
sum – = 10
This is same as sum = sum – 10
 *=
sum*=10
This is same as sum = sum*10
 /=
sum/=10
This is same as sum = sum/10
%=
sum%=10
This is same as sum = sum%10
&=
sum&=10
This is same as sum = sum&10
^=
sum^=10
This is same as sum = sum^10

 

Increment and Decrement Operator :

Increment and Decrement operator are used to increment and decrement variable value by one .

Operators are :

Post increment : varname ++

Pre increment : ++varname

Post decrement: varname–

Pre decrement: –varname

example:

i=1;

i++; // This means i=i+1 ;

i=1;

i–; //This means i=i-1;

 

Conditional Operator :

_?_:_;

Conditional operator returns value depending on truth of condition

eg.

i=9;

a=(i>10?10:11);

in this example i=9 which is less than 10 so condition is false so value assigned to a will be 11;if condition is true than the assigned value will be 10;

Misc Operators:

 S.No
 Operators 
Description
1
&
This is used to get the address of the variable.
Example:        &a will give address of a.
2
*
This is used as pointer to a variable.
Example: * a  where, * is pointer to the variable a.
3
Size of ()
This gives the size of the variable.
Example: size of (char) will give us 1.

C storage class

Every variable declaration has two property associated with it. storage class and data type. storage class defines that where to store a variable.

 

Syntax : storage_class datatype variable_name;

 

There are following storage classes in C.

auto,  register,  static,   extern

S.No. Storage Specifier Storage Place
1 auto CPU Memory
2 extern CPU Memory
3 static CPU Memory
4 Register Register Memory

 

auto storage class is a default storage class for all local variable. auto storage class variable has local scope.

Both declaration of variable a and b are same. Default storage is taken as auto storage.

extern storage class is used to give reference to the global variable of current file or other files. extern storage variable cannot be initialized because it refers to the global variable of current or other file.

If we deal with multiple files and we want to use global variable or function of other file in current file then we can access the gloabl variable or function of other file using storage keyword and define the variable name same as in other files.

Example:

file: 1.c

file: 2.c

 

Compile: gcc 1.c 2.c

output: ./a.out

10

 

Static storage class is default storage class for global variable and static variables.Static variables are initialized only once.

static variable can be defined as below :

The below example will explains more about static variable.

Output :

1 2 3

 

register storage class is similar to local variable. The only difference is the value stored in microprocessor register instead of RAM. This class is faster than other classes. We should not define many register variables other wise the processing will go slower.

Register variable declaration.

 

C overview

In computing, C is a general-purpose programming language initially developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs. Like most imperative languages in the ALGOL tradition, C has facilities for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. Its design provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, most notably system software like the Unix computer operating system

C is one of the most widely used programming languages of all time, and C compilers are available for the majority of available computer architectures and operating systems.

 

for more detail refere to http://en.wikipedia.org/wiki/C_programming

C Data types

In the C programming language, data types refers to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage.

C compiler supports 5 fundamental data types int (integer),float (floating point) , double (double precision floating point)  , char (character) , void 

 

C Constants

Constant in C refers to fixed values that do not change during the life of a program.

Several type of Constants are in C.

Integer Constant

Real Constant

Character Constant

String Constant

 

Backslash character constants used in formating output.

Constant Meaning
‘a’ Audible Alert (Bell)
‘b’ Back Space
‘f’ Form Feed
‘n’ New Line
‘r’ Carriage Return
‘t’ Horizontal Tab
‘v’ Vertical Tab
''’ Single Quote
‘”‘ Double Quote
‘?’ Question Mark
‘\’ Backslash
‘′ Null