Introduction
"Hello World" in C

#include <stdio.h>
int main() {
char input[100];
// Read until a new line is received
scanf("%[^\n]%*c", input);
printf("Hello, World!");
printf("\n%s", input);
return 0
}
Playing with Characters

#include <stdio.h>
int main() {
char ch;
char s[100];
char sen[100];
scanf("%c", &ch);
scanf("%s", s);
// Reading the \n from the previous line
scanf("\n");
// Reading until a \n is received
scanf("%[^\n]%*c", sen);
printf("%c", ch);
printf("\n%s", s);
printf("\n%s", sen);
return 0
}
Sum and Difference of Two Numbers

#include <stdio.h>
int main()
{
int int_a, int_b;
float float_a, float_b;
scanf("%d %d", &int_a, &int_b);
scanf("%f %f", &float_a, &float_b);
printf("%d %d", int_a + int_b, int_a - int_b);
printf("\n%.1f %.1f", float_a + float_b, float_a - float_b);
return 0;
}
Functions in C

#include <stdio.h>
int max_of_four(int a, int b, int c, int d) {
int max = a;
if (max < b) max = b;
if (max < c) max = c;
if (max < d) max = d;
return max;
}
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
Pointers in C

#include <stdio.h>
void update(int *a,int *b) {
int temp = *a;
*a = *a + *b;
*b = abs(temp - *b);
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
Last updated
Was this helpful?