0% found this document useful (1 vote)
3K views23 pages

C Basics

The document provides an introduction to the C programming language including its basic structure, variables types, operators, functions, arrays, pointers, and structures. It explains that a C program consists of preprocessor directives, global declarations, functions with local variables and statements. Common data types include int, char, float, and double. Operators allow mathematical operations and comparisons. Functions are blocks of code that perform tasks. Arrays and pointers allow working with memory locations and addresses. Structures group related data types.

Uploaded by

Shweta Mahajan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
3K views23 pages

C Basics

The document provides an introduction to the C programming language including its basic structure, variables types, operators, functions, arrays, pointers, and structures. It explains that a C program consists of preprocessor directives, global declarations, functions with local variables and statements. Common data types include int, char, float, and double. Operators allow mathematical operations and comparisons. Functions are blocks of code that perform tasks. Arrays and pointers allow working with memory locations and addresses. Structures group related data types.

Uploaded by

Shweta Mahajan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Intro to C

Structure of a ‘C’ program


pre-processor directives
global declarations
function prototypes

main()
{
local variables to function main ;
statements associated with function main ;
}
f1()
{
local variables to function 1 ;
statements associated with function 1 ;
}
f2()
{
local variables to function f2 ;
statements associated with function 2 ;
}
Case Sensitive
• Case matters in C.
A is not a
Structure of my first ‘C’ program
/* This is
a comment */
// This is a one-line comment

# include <stdio.h> /* includes header files */

main() /* Must have a main function. First function executed */


{
printf ("Hello World!"); /* stdio functions */
}
Nice to have
• Add plenty of comments (/* */ or //)
• Good layout, not:
main(){printf("Hello World\n");}

• Use meaningful variable names


• Initialize your variables
• Use parentheses to avoid confusion:
a=(10.0 + 2.0) * (5.0 - 6.0) / 2.0
Common variable types
• int - stores integers (-32767 to +32768)
• unsigned int – 0 to 65535
• char – holds 1 byte of data (-127 to 128)
• unsigned char – holds 1 byte (0 to +255)
• long – usually double int (signed)
• unsigned long – positive double int
• float – floating point variable
• double – twice of a floating point variable
• Note: local, global and static variables
printf
• printf (“%d”,i);

Usual variable type Display


%c char single character
%d (%i) int signed integer
%e (%E) float or double exponential format
%f float or double signed decimal
%g (%G) float or double use %f or %e as required
%o int unsigned octal value
%p pointer address stored in pointer
%s array of char sequence of characters
%u int unsigned decimal
%x (%X) int unsigned hex value
Operators
+ addition
- subtraction
* multiplication
/ division
% mod or remainder (e.g., 2%3 is 2), also called 'modulo'
<< left-shift (e.g., i<<j is i shifted to the left by j bits)
>> right-shift
& bitwise AND
| bitwise OR
^ bitwise exclusive-OR
&& logical AND (returns 1 if both operands are non-zero; else 0)
|| logical OR (returns 1 if either operand is non-zero; else 0)
< less than (e.g., i<j returns 1 if i is less than j)
> greater than
<= less than or equal
>= greater than or equal
== equals
!= does not equal
Operators (contd.)
Increment and Decrement Operators
• ++ Increment operator
• -- Decrement Operator
• k++ or k-- (Post-increment/decrement)
k = 5;
x = k++; // sets x to 5, then increments k to 6

• ++k or --k (Pre-increment/decrement)


k = 5;
x = ++k; // increments k to 6 and then sets x to the
// resulting value, i.e., to 6
Functions and Prototypes
• Building blocks of code
• Group relevant and repetitive actions into
functions
• Variables are usually local (they exist only inside
the function)

type FunctionName(type declared


parameter list)
{
statements that make up the function
}
Example function
#include <stdio.h>
int add1(int);

void main()
{
int x;
x=5;
x=add1(x);
printf("%d“,x);
Returns an integer result
}
Expects an integer parameter
int add1(int i)
{
int y;
y=i+1;
return (y);
}
Bitwise Operators
<< left shift
>> right shift
| bitwise OR
& bitwise AND
^ bitwise XOR
~ bitwise NOT
Bitwise operators (contd.)
AND: z = x & y OR: z = x | y
x 0 1 0 1 x 0 1 0 1
y 0 0 1 1 y 0 0 1 1
z 0 0 0 1 z 0 1 1 1

NOT: z = ~x XOR: z = x ^ y
x 0 1 x 0 1 0 1
z 1 0 y 0 0 1 1
z 0 1 1 0
Shift operators
Right shift by 5 unsigned

Right shift by 5 signed


Conditional clauses
if (expression) statement
else statement

if (x==1)
{
printf(“Hello”);
printf (“hi\n”);
}
else
{
printf(“World’);
}
Conditional Loops
while (expression) statement

while (x<1000) x++;

while (x<1000)
{
x++;
y=y+10;
}
Conditional Loops
do statement while (expression)

x = 0;
do
{
x++;
} while (x<1000);
Unconditional Loops
for( initialization; expression; increment )
statement

for (i=0;i<100;i++)
{
printf("Hi.");
}
The switch statement
The C switch allows multiple choice of a selection of items
at one level of a conditional where it is a far neater way
of writing multiple if statements

switch (expression)
{ case item1:
statement1;
break;
case item2:
statement2;
break;
case itemn:
statementn;
break;
default:
statement;
break;
}
Arrays
• Declaration:
int x[10];
double y[15];
x[5]=10;
char c[15];
• Note arrays start with 0 element
Pointers
• A pointer in C is the address of something
• The unary operator `&' is used to produce
the address of an object
int a, *b, c; // b is a pointer
b = &a; // Store in b the address of a
c = *b; // Store in c the value at
// address b (i.e., a)
• Pointers to pointers
• Pointers to functions
Character Pointers & Arrays
char *y;
char x[100];
y = &x[0];
y = x; // Does the same as the line above
*(y+1) gives x[1]
*(y+i) gives x[i]
Structures
• User defined ‘data type’

struct emprec
{
char name[25];
int age;
int pay;
};

struct emprec employee;


employee.age=32;

You might also like