Fundamentals of C
Fundamentals of C
C Fundamentals
Badharudheen P
Assistant Professor,
Dept. of CSE, MESCE, Kuttippuram
What is C?
/* My first C Program */
#include <stdio.h>
void main()
{
printf("Hello World \n");
}
Structure of C Program
➢Line 3: {
➢Line 5: }
◼ Function Name
◼ For example:
◼ int age, rollno;
◼ float salary;
Identifiers
◼ float area
◼ int num2
◼ float _age
◼ int salary_1
➢We can derive any number of data types from these five.
Questions
c = 5;
printf(“%d\n”, c); 5
printf(“%d\n”, ++c); 6
printf(“%d\n”, c); 6
}
Conditional Operators
Operator Associativity
() left to right
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
= left to right
Questions
1. Program to check whether a given number is odd or even
using ternary operator
Input & Output Functions
◼ Reading input data, processing it and displaying the
results are the three tasks of any program.
printf()
scanf()
getchar() putchar()
gets() puts()
getc() putc()
The printf()Function
◼ This function sents the formatted output to the screen. The
syntax is:
printf(“format string”, var1, var2, … );
◼ putchar(char);
◼ This function is used for printing exactly one character to the
screen.
char ch;
ch = getchar(); /* input a character from kbd*/
putchar(ch); /* display it on the screen */
Unformatted Functions
◼ getc(*file);
◼ This function is similar to getchar() except the input can be
from the keyboard or a file.
char ch;
ch = getc(stdin);/* input from keyboard */
ch = getc(fileptr);/* input from a file */
◼ putc(char, *file);
◼ Similar to putchar() except the o/p can be to the screen or a file.
ch = getc (stdin); /* input from keyboard */
putc(ch, stdout); /* output to the screen */
putc(ch, outfileptr); /*output to a file */
CONTROL STATEMENTS IN C
if Statement
❑ General syntax: ❑ Example
The statement
p = 1;
for (n = 0; n < 50; n++)
{
}
can be rewritten as
for (p = 1, n = 0; n < 50; n++)
{
}
Additional features of for Statement
❑ Case 2: The test condition may have any compound relation and
the testing need not be limited only to the loop control variable.
m = 5;
for ( ; m ! = 100; )
{
printf(“%d\n”,m);
m = m + 5;
}
switch-case-break Statements
❑ It is a multi-way decision making method.
goto label;
Statement(s);
…………….
label: statement(s);
❑ Conditional goto
Unconditional goto Conditional goto
The control transfer from one block The control transfer from one block to
to another block without checking another by checking a test condition.
the test condition. void main()
{
Example: int a, b;
#include <stdio.h> printf (“Enter Two Values”);
scanf (“%d%d”, &a, &b);
void main() if (a > b)
{ goto output_1;
else
Start: goto output_2;
printf(“Welcome\n”); output_1:
printf (“A is Biggest”);
goto Start; goto Stop;
} output_2:
printf (“B is Biggest”);
goto Stop;
Stop:
}
Question