Basiscs of C
Basiscs of C
• Digits 0 1 2 3 4 5 6 7 8 9
, < > . _
( ) ; $ :
% [ ] # ?
' & { } "
^ ! * / |
- \ ~ +
Keywords
• Keywords are predefined, reserved words used in programming that
have special meanings to the compiler. Keywords are part of the syntax
and they cannot be used as an identifier.
• There is no rule on how long an identifier can be. However, you may run
into problems in some compilers if the identifier is longer than 31
characters.
• Names are case sensitive (myVar and myvar are different variables).
Names cannot contain whitespaces or special characters like !, #, %,
etc.
Variables
• In programming, a variable is a container (storage area) to hold data
and the value of the variable can be changed.
• The size of int is usually 4 bytes (32 bits). And, it can take 232 distinct
states from -2147483648 to 2147483647.
Integers
• There are three types of integer literals in C programming:
• decimal (base 10)
• octal (base 8)
• hexadecimal (base 16)
• For example:
Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc
• float salary;
• double price;
• The size of float (single precision float data type) is 4 bytes. And the
size of double (double precision float data type) is 8 bytes.
Float & Double
• if you print a floating point number, the output will show many digits
after the decimal point:
float myFloatNum = 3.5;
double myDoubleNum = 19.99;
• If you want to remove the extra zeros (set decimal precision), you can
use a dot (.) followed by a number that specifies how many digits that
should be shown after the decimal point:
Float & Double
float myFloatNum = 3.5;
printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point
printf("%.1f\n", myFloatNum); // Only show 1 digit
printf("%.2f\n", myFloatNum); // Only show 2 digits
printf("%.4f", myFloatNum); // Only show 4 digits
Character
• A character is created by enclosing a single character inside single
quotation marks. For example: 'a', 'm', 'F', '2', '}' etc.
• If you are sure, only a small integer ([−32,767, +32,767] range) will be
used, you can use short.
short d;
Short & Long
• You can always check the size of a variable using the sizeof() operator.
#include <stdio.h>
int main()
{
short a;
long b;
long long c;
long double d;
printf("size of short = %d bytes\n", sizeof(a));
printf("size of long = %d bytes\n", sizeof(b));
printf("size of long long = %d bytes\n", sizeof(c));
printf("size of long double= %d bytes\n", sizeof(d));
return 0;
}
Signed & Unsigned
• In C, signed and unsigned are type modifiers. You can alter the data
storage of a data type by using them:
signed - allows for storage of both positive and negative numbers
unsigned - allows for storage of only positive numbers
• For example,
// valid codes
unsigned int x = 35;
int y = -35; // signed int
int z = 36; // signed int
// invalid code: unsigned int cannot hold negative integers
• Here, the variables x can hold only zero and positive values because we
have used the unsigned modifier.
• Considering the size of int is 4 bytes, variable y can hold values from -
231 to 231-1, whereas variable x can hold values from 0 to 232-1.
Constants
• If you want to define a variable whose value cannot be changed, you
can use the const keyword. This will create a constant. For example,
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
Arithmetic Operators
Operator Name Description Example
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
== Equal to 5 == 3 is evaluated to 0
Greater than or
>= equal to
5 >= 3 is evaluated to 1
If c = 5 and d = 2 then,
Logical AND. True only
&& expression ((c==5) && (d>5))
if all operands are true
equals to 0.
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Input & Output Statements
• The printf() and scanf() functions are used for input and output in C
language. Both functions are inbuilt library functions, defined in stdio.h
(header file).
• The printf() function is used for output. It prints the given statement to
the console. The syntax of printf() function is
printf("format string",argument_list);
scanf("format string",argument_list);
Program 1
• Write a program to read and print a character, integer and floating point
value.
#include <stdio.h>
int main()
{
int a;
float b;
char c;
printf("\nEnter an integer: ");
scanf("%d", &a);
printf("\nEnter a float: ");
scanf("%f", &b);
printf("\nEnter a character: ");
scanf("%c", &c);
printf("%d %f %c ", a,b,c);
return 0;
}
Program 2
• Write a program to float into an integer and also limit number of
decimal points for a float.
#include <stdio.h>
int main()
{
float num1 = 5.678;
int num2 = (int) num1;
#include <stdio.h>
int main() {
int myInt;
float myFloat;
double myDouble;
char myChar;
printf("%d\n", sizeof(myInt));
printf("%d\n", sizeof(myFloat));
printf("%d\n", sizeof(myDouble));
printf("%d\n", sizeof(myChar));
return 0;
}
Program 4
• Write a program to print the ASCII value of a character entered by the
user.
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
return 0;
}
Program 5
• Write a program to swap two variables without using a third variable
Program 6
• Write a program to find the roots of a quadratic expression.