0% found this document useful (0 votes)
13 views

fundamentals-of-c

Uploaded by

Amit Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

fundamentals-of-c

Uploaded by

Amit Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

s

FUNDAMENTALS OF
C
Basics of C Programs
A C program basically consists of the following parts
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments

CODE: #include <stdio.h>


int main()
{
printf("Hello, World! \
n"); return 0;
}
Basics of C Programs
1.
(cont..)
The first line of the program #include <stdio.h> is a preprocessor command,
which tells a C compiler to include stdio.h file before going to actual
compilation.
2. The next line int main() is the main function where the program execution
begins.
3. The next line /*...*/ will be ignored by the compiler and it has been put to
add additional comments in the program. So such lines are called comments
in the program.
4. The next line printf(...) is another function available in C which causes the
message "Hello, World!" to be displayed on the screen.
5. The next line return 0; terminates the main() function and returns the value 0.
Basics of C Programs
(cont..)
Comments : A comment starts with a slash asterisk /* and ends with a asterisk
slash */ and can be anywhere in your program.
comment on a single line: // Author: TechOnTheNet.com

C header files : Header files are helping file of your C program which holds the
definitions of various functions and their associated variables that needs to be
imported into your C program with the help of pre processor #include statement.
All the header file have a '.h' an extension that contains C function declaration
and macro definitions.
The default header file that comes with the C compiler is the stdio.h.
Syntax:
#include <file>
Basics of C Programs
(cont..)
Variables: In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name
(identifier).
Example: int playerScore = 95;

Rules for naming a variable


1. A variable name can only have letters (both uppercase and lowercase letters),
digits and underscore.
2. The first letter of a variable should be either a letter or an underscore.
3. There is no rule on how long a variable name (identifier) can be. However,
you may run into problems in some compilers if the variable name is longer
than 31 characters.
C Data Types
In C programming, data types are declarations for variables. This determines the
type and size of data associated with variables.
Example:
int myVar;

Here, myVar is a variable of int (integer) type. The size of int is 4 bytes.
C Data Types (cont..)
Type Size (bytes) Format Specifier
int at least 2, usually 4 %d
char 1 %c
float 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %li
long long int at least 8 %lli
unsigned long int at least 4 %lu
unsigned long long int at least 8 %llu
signed char 1 %c
unsigned char 1 %c
long double at least 10, usually 12 or 16 %Lf
C Programming Operators
An operator is a symbol that operates on a value or a variable.
For example: + is an operator to perform addition.

C has a wide range of operators to perform various operations.


• Arithmetic operator
• Assignment Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Comma Operator
• sizeof operator
C Programming Operators
(cont..)
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values.
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* Multiplication
/ Division
remainder after division (modulo
%
division)
++ Increment
-- Decrement
C Programming Operators
(cont..)
An assignment operator is used for assigning a value to a variable. The most
common assignment operator is =.

Operator Example Same as


= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
C Programming Operators
(cont..)
A relational operator checks the relationship between two operands. If the
relation is true, it returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.

Operator Meaning of Operator Example


== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
C Programming Operators
(cont..)
Bitwise operators are used in C programming to perform bit-level operations.
During computation, mathematical operations like: addition, subtraction,
multiplication, division, etc are converted to bit-level which makes processing
faster and saves power.

Operators Meaning of operators


& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
C Programming Operators
(cont..)
An expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false. Logical operators are commonly used
in decision making in C programming.
Operator Meaning Example
If c = 5 and d = 2 then,
Logical AND. True only if
&& expression ((c==5) &&
all operands are true
(d>5)) equals to 0.
If c = 5 and d = 2 then,
Logical OR. True only if
|| expression ((c==5) || (d>5))
either one operand is true
equals to 1.
Logical NOT. True only if If c = 5 then, expression
!
the operand is 0 !(c==5) equals to 0.
C Programming Operators
(cont..)
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;

The sizeof operator


The sizeof is a unary operator that returns the size of data (constants, variables,
array, structure, etc).

Associativity of Operators
The associativity of operators determines the direction in which an expression is
evaluated. For example,
b = a;
C Input Output (I/O)
C Output
In C programming, printf() is one of the main output function. The function
sends formatted output to the screen. For example,

Example : C
Output #include
<stdio.h> int main()
{
printf("C Programming");
return 0;
}
C Input Output (I/O) (cont..)
C Input
In C programming, scanf() is one of the commonly used function to take input
from the user. The scanf() function reads formatted input from the standard
input such as keyboards.

Example: Integer Input/Output


#include <stdio.h>
int main()
{
int testInteger;
printf("Enter an integer: ");
scanf("%d", &testInteger);
printf("Number = %d",testInteger);
return 0;
}

You might also like