You are on page 1of 24

C Basics

Introduction
C programming is procedural oriented
programming
 Intially C is used for developing operating
sytems(System programming)
 It has features such as directly accessing
memory,controlling hardware etc.so it is
also called as low level programming
structure
preprocessor statements
function declaration
global variable declaration
int main(void)
{
local variable declaration
//body
}
function defintion
Preprocessor statements
Any statement which starts with # symbol is
preprocessor statement
Note:it should not use the termination
symbol(;) at the end of the statement.
example:
#include<stdio.h>
#define P printf
Role of Preprocessor
All the preprocessor statements are
processed by preprocessor.
The preprocessor takes the C program as
input and gives C program as output.
First statement in C program
#include<stdio.h>
(searches stdio.h in standard library
directory)
or
#include “stdio.h”
(searches stdio.h in current directory and if
not available then searches in standard
library directory)
why stdio.h?
stdio.h is a header file which contains the
declarations of functions printf(output
function) and scanf(input function)
when scanf and printf functions are used it is
mandatory to include stdio.h in C program
simple c program
#include<stdio.h>
int main(void)
{
printf(“welcome”);
return 0;
}
let us assume stdio.h contains
printf(char[10] string)
{
print(string);
}
what will be the output of the above
program after preprocessing
the stdio.h will be replaced such as
printf(char[10] string)
{
print(string);
}
int main(void)
{
printf(“welcome”);
return 0;
}
Note:the preprocessor statement is replaced by its
declarations and defintions
main() function
main() is the entry point of the program
only int main(void) is the standard not
void main() even though some compilers
accept it
there is also another standard
int main(int argc,char *argv[])
main() function is called by operating system
curly braces{}
The curly { } defines the scope of the
function
every function starts with the curly brace
{ and ends with curly brace } including
main()
printf function
printf is a standard library function to display
some thing in the standard output
printf returns an integer value based on the
number of characters it is printing
int count;
count=printf(“training”);
now count contains value 8.since “training”
has 8 characters
scanf function
scanf function is an standard input function
scanf returns the no of arguments it is getting from
the console

int a;
int no;
no=scanf(“%d”,&a);
since you are getting only one argument the value
of no is 1
return
The main() function should return a value
called as 0.0 is returned to the operating
system.0 indicates successful completion.
int main() vs int main(void)
int main(void) is better than int main()
when main() is left blank it can accept any number of arguments
example
#include<stdio.h>
int main()
{
static int i=5;
if(i--)
{
printf(“%d”,i);
main(3);
}
}

gives output 4 3 2 1 0
int main() vs int main(void)
#include<stdio.h>
int main(void)
{
static int i=5;
if(i--)
{
printf(“%d”,i);
main(3);
}
}
this program results in error because main does not accept
any parameter values
QUIZ
which is a valid c standard
a)void main(void)
b)int main()
c)int main(void)
d)main(void)
QUIZ
which are preprocessor statements
a)#define
b)#include
c)void fun()
d)none of the above
QUIZ
output of the program
#include<stdio.h>
void fun(){ printf(“wonder”);}
int main(void)
{
fun(10,20,30);
}
a)compile error
b)wonder
Quiz
what is the output of the program
#include<stdio.h>
int main(void)
{
printf(“%d”,printf(“welcome”));
return 0;
}
a)1 welcome
b)welcome 4
c)welcome 7
d)error
quiz
what is the output of the program
#include<stdio.h>
int main(void)
{
printf(“%d”,printf(“%d”,printf(“welcome to c”)));
return 0;
}
a)welcome to c 12 1
b)12 1 welcome to c
c)welcome to c 12 2
d)error
Quiz
which statement searches conio.h in current
directory and standard library
1)#include<conio.h>
2)#include “conio.h”
3)none
Quiz
what is the output if the program executes successfully
#include<stdio.h>
int main(void)
{
int a,b,c;
c=scanf(“%d %d”,&a,&b);//assume you give 10 and 20
printf(“%d”,c);
return 0;
}
a)1
b)2
c)error
d)3
Assignment
1)check a character is a alphabet or
not(avoid built in function)
2)check whether a given character is vowel
or consonant
3)find the largest among three numbers
a,b,c
4)count the number of digits in a number
5)calculate the sum of N numbers

You might also like